Guide to Git and Github
#1
Note: This was a guide written for another forum by me but I moved it here.
Why I'm Writing This
Lots of people here program and lots of people here share what they create.  Unfortunately the way they share it is via Pastebin, random site download, etc which is bad for a few reasons...
  • It's shady as all hell (Sweet zip file bro)
  • It's easy to loose changes (rm -rf *)
  • It's hard to collaborate (Wait, you made changes?)
  • Etc
Using a tool like Git allows you to fix all of those things.  You can monitor changes, you can fork things out into branches (I'll explain), you can easily and safely share plain text code.
What is Git
"Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." -- git-scm.com
What is GitHub
"GitHub is a Git repository web-based hosting service which offers all of the functionality of Git as well as adding many of its own features. Unlike Git which is strictly a command-line tool, Github provides a web-based graphical interface and desktop as well as mobile integration." -- Wikipedia
Git Basics
Now that we have explained all of that let's jump right in...
I'm not going to cover installation of Git but you can read it here How to install git or if you're on Ubuntu..
Code:
~$ sudo apt-get install git
Language
  • Repository (Repo) - Essentially your whole project including branches, remotes, etc.
  • Branch - A version of your current repo common branch names would include Master, Dev, Staging, Upgrade, and DontTouchThisBranchItBreaksShit
  • Remote - This is a remote git repo that your local repo can exchange updates and branches (Like your github account)
  • Origin - The remote that your repo originated from (applies to cloned repos only)
  • Checkout (Branches) - Change the active branch
  • Checkout (File) - Remove any changes since the last commit
  • Commit(s) - This is how you submit changes to a branch
  • Log - The log of all the branch's commits
  • Push - The act of push code from one repo to another (or to a remote)
  • Pull - Requesting all changes from a specific repo's branch
  • Merge - Merging two local branch's code changes (Most common case is merging dev changes into master)
How to create a repository
There are two methods of creating a repository, git init or git clone.
If starting a new project from scratch you need to use git init.  If you are downloading/cloning a project you would use git clone.
Example:
This is an example of cloning a repo from GitHub, after the commands we have the full code base for the lulzlabs "AirChat" program.
Code:
~$ git clone git@github.com:lulzlabs/AirChat.git
This is an example of creating a file and then building a git repo around that file.  After this we have a git repo around a single file called "file.php" with the contents "echo 'test';".
Code:
~$ mkdir coolscript && cd coolscript
~$ touch file.php
~$ echo "echo 'test';" > file.php
~$ git init
~$ git add file.php
~$ git commit -am "Creating a new git repo for my hax0r project"
Adding a new file
To add a new file to a git repo you must first have a repo and a file(s) to add.  After that is confirmed you can use the command "git add" to add those files.  This must be followed by a commit.
Example:
This example shows creating a new empty file and then adding it to the git repo.
Code:
~$ touch newfile
~$ git add newfile
~$ git commit -am "Adding a new file to the repo"
Committing Changes to Branch (Editing Files)
Whether you are editing a file, adding a new file, or changing permissions(if you git config says to record that) you must commit your changes before you can push your branch to another repo.  The best way to see if you have changes would be to run the command "git status" this will show files that aren't in the git repo as well as any edits.
With every commit you need a commit message this will allow you to keep track of everything.  I do that by adding -m "MY MESSAGE HERE" in my git commit call.  I also like to to use -a so I don't have to manually confirm my changes.
Example:
In this example we edit "existingfile" and commit our change.
Code:
~$ echo "Add to file" > existingfile
~$ git status #this is optional
~$ git commit -am "Editing existingfile to have new message."
In this example we edit "existingfile" and commit our change (Without the -a in our git commit).
Code:
~$ echo "Add to file" > existingfile
~$ git status #this is optional
~$ git add existingfile
~$ git commit -m "Editing existingfile to have new message."
Reverting Changes
Mistakes happen and that's why we have checkout.  Using the command "git checkout FILENAMEHERE" will allow you to revert that file to the last git commit.
Example:
This shows editing a file, a commit, then an incorrect edit, and finally reverting that file.  I use "cat" to show the file changes.
Code:
~$ echo "Edit to file" > existingfile
~$ git commit -am "Changing file"
~$ echo "wrong edit to file" > existingfile
~$ cat existingfile #this will output "wrong edit to file"
~$ git checkout existingfile
~$ cat existingfile #this will output "Edit to file"

Branches
A branch is an individual line of development(like a fork) that allows you to keep versions of your code separate.  This is useful mainly for keeping stable versions and development versions segregated.  I personally use ticket systems for big projects so I can have a branch per ticket.  So I would create a branch, do my edits, commit, test, and merge into master.

Example:
In this example we create a dev branch and check it out (Make dev the active branch).  NOTE: the * shows the currently checkout branch.
Code:
~$ git branch
   * master
~$ git branch dev
~$ git branch
     dev
   * master
~$ git checkout dev
Switched to branch 'dev'

Delete Branches
To delete a branch you can do one of two things.  The safe delete and unsafe delete.  Safe delete exits if you have unmerged chages unsafe does it with or without checking for unmerged changes.  To safe delete "git branch -d BRANCHNAME" to unsafe delete "git branch -D BRANCHNAME"

Code:
~$ git branch newbranch
~$ git branch -D newbranch


Git Remotes
As explained in the language section a git remote is a remote git repo that you have access to.  But git remotes can be a monster of their own.

View Remotes
This command lets you list all remotes you have added to your repo.
Code:
~$ git remote

Add A Remote
To add a remote you use the following..
Code:
~$ git remote add <name> <url>

An example could be (You can use both http and ssh urls)
Code:
~$ git remote add origin ssh://user@host/path/to/repo.git

Remove a Remote
Doesn't really need explaining
Code:
~$ git remote rm <name>

Fetching Branches from a repo
It you want a specific branch from a remote repo you want to fetch.
Code:
~$ git fetch <remote> <branch>

Example:
Code:
~$ git fetch origin someRandomBranch

If you want ALL branches from a repo just exclude the branch name

Example:
Code:
~$ git fetch origin

Pulling Changes From Repo
Before pushing to a branch you want to make sure your local branch is up to date OR if you just want to update your local branch you would...
Code:
~$ git pull <remote> <branch>

Example:
Code:
~$ git pull origin dev


Pushing Changes From Repo
If you want to push your commits(changes) to a remote repo's branch you would use git push.

Code:
~$ git push <remote> <branch>

Example:
Code:
~$ git push origin dev


Using GitHub With Git
Before playing with GitHub you need a GitHub account and to install Git.

Sign up for GitHub here

After that you need to create your project repo.  That can be done here.  All you do is give it a name.

After this you can create your repo locally or use an existing repo.

Example:
This is creating a new 100% empty project.
Code:
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:USERNAME/REPONAME.git

This is using a preexisting local repo
Code:
git remote add origin git@github.com:USERNAME/REPONAME.git
git push -u origin master
[/code]

Now you can push to your projects on GitHub!


In Closing

You can now use local repos as well as push to remote GitHub Projects.  If you need any help don't be afraid to ask.  Git is an amazing tool and VERY powerful, which can be daunting, but after some use it is all very natural.


more info
- https://help.github.com/articles/set-up-git
- http://git-scm.com/docs/gittutorial
- ~$ man git
Reply
#2
Great thread, I'm glad you're moving it over here. I will sticky this, it's important for every developer to know if they want to reach the open source audience.
Reply
#3
Sorry to bump old threads BUT I have exciting news!  Codecademy is now doing Git courses!

If anyone here does work on servers or websites or any personal projects learn git please.  You will never regret it, it has saved my work and my job multiple times.

Reply
#4
That's pretty neat, I'm going to suggest this to my programming teacher. Good way to teach the whole class.
Reply
#5
(12-01-2015, 09:43 PM)NO-OP Wrote: Sorry to bump old threads BUT I have exciting news!  Codecademy is now doing Git courses!

If anyone here does work on servers or websites or any personal projects learn git please.  You will never regret it, it has saved my work and my job multiple times.


Saw the thread title and just came to say this exact thing lol. Look forward to trying this course out.
Reply
#6
(12-01-2015, 10:10 PM)MuddyBucket Wrote:
(12-01-2015, 09:43 PM)NO-OP Wrote: Sorry to bump old threads BUT I have exciting news!  Codecademy is now doing Git courses!

If anyone here does work on servers or websites or any personal projects learn git please.  You will never regret it, it has saved my work and my job multiple times.


Saw the thread title and just came to say this exact thing lol. Look forward to trying this course out.

Yeah explaining git is a bastard of task and a lot of people use it wrong.

reminds me of this comic by XKCD.

[Image: git.png]
alt text = "If that doesn't fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutes of 'It's really pretty simple, just think of branches as...' and eventually you'll learn the commands that will fix everything."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is this a realistic and/or good plan to learn programming? QMark 2 15,589 02-27-2018, 11:16 PM
Last Post: ekultek
  ASM question - Push and Pop functions Ahmed 2 14,964 12-08-2017, 03:25 AM
Last Post: Ahmed
  How do I stay focused and get serious with learning a language? lunorian 11 33,862 12-07-2017, 11:24 PM
Last Post: lunorian
  Data Structures and Algorithms NO-OP 5 16,700 12-08-2016, 09:06 AM
Last Post: NO-OP