Git Command Reference

It has been about a year since using Git regularly and I have since forgotten a lot of the commands. This page is a non-exhaustive and growing reference to help me remember them. Note: Do not include the < > brackets around the actual branch names, etc., they are used in the examples below to indicate a place holder.

Amend

# combine any staged changes with the contents of the previous commit
git commit --amend

# same as above and change the commit message
git commit --amend -am "my new commit message"

Branch

# list local branches
git branch

# list remote and local branches
git branch -a

# list remote branches
git branch -r

# rename a local branch
git branch -m <oldname> <newname>

# push a new local branch to remote and track
git push -u origin <branchname>

Clone

Cloning is the first step when getting a repository from the server.

# download the repository into the current directory
git clone <url>

# download the repository into the current directory with a sub directory name you supply
git clone <url> <directory>

Checkout

# create and checkout in one step
git checkout -b <branchname>

# copy file from another branch
git checkout <branchname> -- <filename>

Delete

# delete local branch
git branch -d <branchname>

# delete remote branch
git push origin :<branchname>

# Git 1.7+
git push origin --delete <branchname>

Diff

# diff between two local branches
git diff branch_a..branch_b

# write the diff to a file
git diff branch_a..branch_b > C:\diff.txt

# common ancestor diff
git diff branch_a...branch_b

Notes

  • Press Q to exit diff
  • You can also add a file or folder name after the commands

Fetch

#checkout a remote branch that does not exist on local
git fetch origin
git checkout --track origin/<remote branch name>

# checkout a remote branch to a new local branch
git fetch origin <remote branch name>:<local branch name>

Log

# shows the commit logs.
git log

# not in origin
git log origin..HEAD

Notes

  • git log outputs with the less program, making it scrollable.
    Type q to exit. Type h for help.
  • origin..HEAD specifies all the commits reachable from the current commit (i.e. HEAD), but not from origin.

Pull

# get rid of 'your branch is ahead of origin <branchname> by X commits
git pull origin <branchname>

git pull origin

Rebase

# reapply commits on top of another base tip
git rebase

# squash four commits into one
git rebase -i HEAD~4

Notes

  • Squash info: Git Book Chapter 6.4: Git Tools – Rewriting History
  • Editor: press i to enter inline insert mode. Use the delete key to erase from left to right, press esc to exit insert mode, then type :x (now the cursor is at the bottom) and hit enter to save and exit.
  • You can edit your last commit message before pushing with git commit --amend

If you’ve already pushed commits to GitHub, and then squash them locally, you will have to force the push to your branch.

git push origin <branchname> --force

Reset

# reset to previous state
git reset --hard

Notes

  • git reflog to show what has been done with git session

Stash

# stash changes in a dirty working directory
git-stash

# remove and apply stash
git-stash pop

# list stashes
git stash list

# apply stash
git-stash apply

Tag

# add a lightweight tag to by specifying the
# commit checksum (or part of it) at the end of the command
git tag sometagname ed40a9deaa0
git push origin sometagname

# delete a tag
git tag -d sometagname
git push origin :refs/tags/sometagname

Additional Resources


Published by

Jim Frenette

Web Developer – views here are my own except those taken from people more clever than me.

Loading Disqus Comments ...
Loading Facebook Comments ...