Getting Started with Git

Quick reference guide to get you up and running in a few minutes

Bahadir Balban

Buzz Founder,@gettingstartedwithgit38547

Download and Install Git

Use these links for Mac OS X, Linux, or Windows.

Create a new Repository

cd mydir
git init

Check out a Repository

git clone <url>

For example:

git clone https://github.com/twbs/bootstrap.git

Add all files & make your first commit

cd mydir

git add .

This added all of your files to be commited.

git commit -m "Your commit message"

This commits all files to your local repository.

Send your changes to the remote repository

git push origin master 

Define a new remote repository to push:

git remote add <origin_repo_name> <repo_url>

For example:

git remote add my-new-origin git@github.com/my-project

Then you can push:

git push my-new-origin master

How Git Works:

There are 3 distinct places where your project state is maintained:

Your actual files, git’s internal index, and the git commits.

Git first adds changes in your files to the index. It then commits the changes recorded in the index.

That means unless you add changes to the index first, new changes will not be included in a commit.

Add a single file and commit

git add myfile.txt

git commit

Adding all changes and commit:

If you type:

git commit -a

This would do it all at once. It moves all your edits in your files to the index, and then create a new commit.

If you created new files also, you would need to remember to use git add for those before running git commit -a.

Checking commit history

git log

One commit per line:

git log --oneline

Viewing the contents of a particular commit

The top commit:

git show

1 below the top:

git show HEAD^

and you can go further down as such:

git show HEAD^^^

will show you the 4th commit from the top.

Use the commit’s SHAID shown with a number such as e947995881fd7dbfcaf09b6b45965197deb9c769

For example:

git show 5afebb0

Branching

Branching means you create a stream of commits under a new label, and the starting point is the point when you create and check out that branch.

How branching works

Creating a new branch

git branch staging

Checking out a previous branch

git checkout master

Creating a new branch and check out

git checkout -b staging

Seeing the list of all branches

git branch -a

Updating local repository with latest changes

git pull origin master

This will pull all updates that has happened in the origin repo (usually a remote repository).

Merging

Merging is the reverse operation of branching. Merging means you would append the series of commits (commit history) in one branch on top of all the commits in another branch. Typically the branch being merged from has a common ancestor as the branch being merged into.

Git merges the history of master and staging branches

You can merge a branch as follows:

git merge staging

or

git pull . staging

Git uses an algorithm to merge the distinct commit histories of 2 branches. It also adds a merge commit at the top to represent the operations it just did to merge those branches.

There is no guarantee that commits from each branch will end up in a sequential order.

Fast forward: Appending one branch to another

A Fast forward is a simple way to append changes that happened in another branch with no merging. The condition is that the current branch has no updates since the branch off, and only the branch we pull from has additional commits to append.

A fast forward sequentially appends all the commits in the 2nd branch since the common ancestor, to the current branch.

git checkout -b staging touch newfile.txt
git add newfile.txt
git commit -m "new file in staging"

At this point we have 1 new commit in staging. Master has no new commits different. Switch to master:

git checkout master

Merge the staging branch with a fast-forward:

git pull . staging

This pulls all changes in staging into master with a fast-forward.

Simple fast forward, requires no merge commit.




Join The Discussion