Git Command CheatSheet


Apart from brief forays into SVN and Perforce, I have spent most of my career using MS source control solutions: SourceSafe, TFS and VSTS.
So whenever I have the opportunity to use Git, I have to refresh my mind a little.  While the Git documentation (https://git-scm.com/book/en/v1/Getting-Started) is excellent, sometimes I just need a quick reference for the basics.  That is what I have compiled here, mostly for my own reference, but hopefully it will be helpful for you too.

Installation on Windows

Download the installer exe file from the GitHub page, and run it:
http://msysgit.github.io
After it’s installed, you have both a command-line version (including an SSH client that will come in handy later) and the standard GUI.

NB: If you need to use the native Windows shell / command line console:
  • Use double quotes instead of single quotes (for parameters with spaces in them)
  • Quote the parameters ending with the circumflex accent (^) if they are last on the line.

Basic Git workflow

  1. You modify files in your working directory. 
  2. You stage the files, adding snapshots of them to your staging area. 
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory. 
  4. You push the changes in the local Git directory to the central(shared) repository.

Help

Getting help on Git commands
$ git help <command name>

Git Repositories

There are two options:

1. Import an existing project into Git

Go to the project directory (where you want to start tracking changes) in the Git bash shell and type
$ git init  // creates .git subdirectory and repository skeleton
$ git add <file types> // starts tracking these files
$ git add README
$ git commit -m 'created repo with initial version' // commits the changes

OR

2. Clone an existing repository

Create a destination directory to host the code then open a Git bash shell from this directory and type
$ git clone <url of git repository> // creates the target repository inside your host directory.

Modifying files

Files can be in these states
  • Tracked ($ git add has been run on them)
    • Unmodified
    • Modified
    • Staged
    • Committed (in local repository but not shared)
    • Pushed (shared to remote git repository)
  • Untracked
To show the state of the files in the working directory
(does not distinguish between committed and pushed)
$ git status
To start track changes to a file $ git add <filename>
To stage a modified file
(same command to stage and add)
NB: If you modify a file after you run 'git add',
then you have to run 'git add' again to stage your most recent changes.
$ git add <filename>
To start track changes to the current directory $ git add .
To see what has been changed, but not yet staged $ git diff
To see only what you have staged $ git diff -- cached
To commit the staged changes

BUT you then have to add a commit comment.
So it is better to use
$ git commit

$ git commit -m "Comment goes here"
To automatically commit all changes
(without having to stage them)
$ git commit -a -m "Comment goes here"
To remove a file
It needs to be removed from tracking then delete it
(once you have executed the commit command)
$ git rm <filename>
To move a file
(Git treats this like a rename)
$ git mv <from filename> <to filename>
To see the commit history $ git log
To see the diffs per commit $ git log -p
To see only the last 2 diffs per commit $ git log -p -2
To discard the changes in the current working directory $ git checkout --<filename>
To pull from a remote repository
(This just pulls into the repository.
It does not merge into the current working directory)
$ git fetch <remote name>
To get the name of the current remote repository $ git remote
To pull and merge into your current branch $ git pull
To push to the current remote repository
Use this  to share your current staged files
(similar to a commit in VSTS)
$ git push <remote name> <branch name>
To tag (label) a commit $ git -a <tagname> -m <tag message or comment >
To view all tags $ git tag
To undo changes to a file $ git checkout <filename>
NB: This permanently erases all changes to the file by copying another file over it.
To undo a single commit $ git revert <commit id>
Obtain the commit id by $ git log -n 1
To remove files not under source control from the directory.
e.g. files in /bin/, /obj/, /packages/
$ git clean -fdx

Branching in Git

To create a new branch$ git branch <branch name>
To move to a branch$ git checkout <branch name>
To create a new branch AND move to it$ git checkout -b <branch name>
To merge back to the original branch
(e.g. master (the target branch))
$ git checkout <master or name of the target branch>
$ git merge <source branch name>
To delete a local branch$ git branch -d <local branch name>
To rebase$ git rebase <source branch name>
(when in destination branch)
To merge commits in local branch, before pushing. $ git reset --soft HEAD~<no. of commits>
$ git commit -m "<message>

A better option is:
$ git rebase -i HEAD~<no. of commits>



Comments