A Git Cheat Sheet


This cheat sheet tells how to use Git , a very popular version control system. Git is used in many open-source projects. We will cover the following

  • How to install and set up Git on a Windows computer.
  • Basic commands for working with files and directories on the UNIX/Linux command line.
  • Basic Git commands for making and working with a local repository.
  • Working with a remote repository in a team on both GitHub and BitBucket.
  • Resources you can consult if you want more detailed information.

Installing Git

To install Git, first download it from http://git-scm.com/download/win. The download will start automatically. Once the file has finished downloading on your computer, run it. I recommend that you let the installer create a desktop shortcut to the Git Bash shell for you. This way, the Git command line environment will start for you, when you want to work with Git. I also recommend that you create a local folder to hold your local repositories that you will use to house the changes you work on when you work with teams, and any repositories that you work with on your own. I also recommend that you point the shortcut to the local folder where you keep your repositories. To do that, find the shortcut on your desktop and press ALT+ENTER. Tab into the ""Start in"" box and type the path to the local repository.

Basic Unix Commands For Directories and Files

Below are some basic commands that will allow you to work with the command line, since when you run Git, you will be using a UNIX command line.


List the Contents of the Current Directory


List files and directories, one per line.

ls -1

Change To a Directory

cd <directory>

Where is the absolute or relative path of a directory.

Note: The absolute path to a directory starts at the root. The relative path of a directory starts at the current directoory.

Move Up Toward The Root One Level

cd ..

Create a Directory

mkdir <directory>

Where is the name of the directory to create. You can create a directory by specifying either an absolute or a relative directory name.

Workflow In Brief For Contributing to a GitHub or BitBucket project

This is a list of the activities you will go through as you work on a project on Github or BitBucket.

  • Set up Git on your local computer.
  • Sign up for an account at http://www.github.com/ or http://www.bitbucket.com/.
  • Log onto Github or BitBucket.
  • Find the repository you want to work with.
  • Click Fork to get your own copy of the repository under your account.
  • Copy the URL for your new repository to the clipboard using the control for that purpose.
  • Open Git on your local computer.
  • Clone your new repository to your local machine using the URL you copied to the clipboard. Tip: On BitBucket, the entire Git command will be on the clipboard. In GitHub, the URL only will be in the clipboard. With the Git Bash command line open, you can get the contents of the clipboard onto the command line by pressing ALT+SPACE, E, then P. You can then use arrow keys, HOME, END, BACKSPACE, and DELETE to edit the command line.
  • Create and work on a branch.
  • Push the branch back up to your GitHub repository.
  • Sync your remote repository to the team repository, if necessary.
  • Create a pull request on GitHub or BitBucket for the project owner to review.
  • Continue to work on the current branch or make other branches to work on.
  • Delete local branch or branches when finished.

Workflow Details

Below are more details about the tasks you will perform to make changes to a repository. We pick up after you have decided on a project to which you want to contribute and have forked the repository. Keep in mind that there are other variations on the commands I show here that can make your job easier by combining steps, but I present things this way, so that you see every step in the process so that you know exactly how it works. I trust that if you want to shorten the process once you know how it works, that you can use the git help command or consult "Pro Git" to explore making this process more efficient.

Clone Your Forked repository

Use the following command to put a local copy of your repository onto your computer.

git clone <repository>

is the URL that you copied to the clipboard when you were finished forking the project and would have have gotten the URL for the new repository it on GitHub or BitBucket.

Checking The Status of Your Local Repository

Any time you want to check the status of your local repository, use this command.

git status

Tipp: run the status command after each other command to get a feel for the kinds of information Git keeps track of.

Create A Branch

Create a branch to keep yourself from accidentally altering the original project. Git always creates a branch called master when you clone the repository. Note: If a project has more than one branch, you can do the same for that branch. You just have to know the name of the branch you want to work with. In this example, we assume that you want to change the branch called "master."

git branch <branch>

is the name of the branch in which you want to put your changes.

Once you have created the new branch, you have to switch to it, so you can work on the project branch. Git does not switch to a branch when you use the branch command unless you use a command argument. To switch to the new branch, run this command.

git checkout <branch>

is the branch you created in the previous step.

Git is now pointing to the new branch. Any changes you make will go into this branch.

Change a File

To keep it simple, we’re going to say that your going to either change one file, or add one to your new branch. So we pick up after you’ve changed or added a file. Once you have saved the file, you would go to the Git command line. At this point, your change is not in the new branch.

Type git status to see what’s going on.

Adding Your Change To The New Branch

git add <file>

is the path, beginning at the root directory of your project, to the file you have changed.

Now run git status again to see what just happened.

Note: if you need to make another change to your file, you need to run git add <file> again.

Add The File To The Branch of the Local Repository

Your file is not yet in your repository. To do that, run this command.

git commit -m "Message"

"Message" is a description of the change, so that you remember what happened.

Once again, run git status to see what happened.

Reviewing What happened

Remember that git status shows the state of your local repository. You can also run git status --short to get more simplified output.

The command git diff gives you detailed information about what changed.

The command git diff --staged compares the staged files to your last commit. The term "staged" means files you have used the git add command on, but which you have not put into your local repository by running git commit -m"message".

git log --oneline --decorate --graph --all  

This shows you the commits you have made as you’ve been working on a local repository.

Merge Your Branch Into Your Local Repository

You would do this when you are satisfied with the changes you want to make. To get your new branch into your local repository, use the following two commands.

git checkout master
git merge <branch>

The first command makes Git point to the branch called master, so that you can put your changes into the repository.

is the name of the branch you were just working on.

The second command puts the new changes into your repository.

Deleting A Branch

For whatever reason, you might want to delete a branch. You may decide that you don’t want to merge the branch into your project afterall, or you might simply be done working on that branch and want to clean up. To delete a branch, type this command.

git branch -d <branch>

is the name of the branch you want to delete.

Put Your Changes Up On GitHub or BitBucket

Now that you’ve made changes to your local repository, you will want to put those changes on your repository on GitHub or BitBucket. If you are working alone, chances are that you would want to simply put your changes up on the server and move onto the next changes. This guide assumes that you are working on a team, however, and that you want your repository to stay in sync with the one you forked it from. This section tells how to get your remote repository in sync with the original.

Syncing Your Remote Repository To The Original

A remote is a repository on a server. In otherwords, not on your computer. The one on your computer is local. When you first clone or create the local repository, Git gives the remote repository the name "origin by default."

List Remote Repositories

git remote -v

At this point, git should only show your forked repository, and its name should be "origin."

Specify The Original Repository

Note: we’ll name the new remote repository "upstream," just to emphasize that origin, the one we created to hold the changes gets the new material from the repository we cloned.

git remote add upstream <url>

is the URL of the repository we forked before we cloned our fork to our local computer. Note: If you lose the name of the repository you forked from, you can jjust go to it and pretend that you want to clone it, get the URL onto your clipboard, and use it in the git remote add command by editing the command line.

List Remote Repositories Again

Now you can verify the new upstream repository you’ve specified.

git remote -v

This time you see the names of both the repository you forked and the upstream repository.

From this point forward, you no longer have to perform this step.

Sync Your Forked Repository With The Upstream Repository

It’s a good idea to sync your forked repository with the upstream one before you start working on new changes, in case you went to bed and someone in another part of the world worked on it. So before you start making changes, use the below commands.

git fetch upstream
git fetch origin

Now, to start working on your changes locally, check out your fork’s local master branch.

git checkout master

Merge the changes from upstream/master into your local master branch. This brings your fork’s master branch into sync with the upstream repository, without losing your local changes.

git merge upstream/master

Push Your Changes Up To The Server

When you are done making changes, push your local changes up to your git repository on the server by typing the below command.

git push origin

Pull Requests

When you are done working on local changes, and you want to let the team know that you have made changes and would like them to put them into the team repository, you would go to GitHub or BitBucket and create the pull request.


For more information on using Git and git hub click here