Use this to recover from major fuck ups! It’s basically a log of the
last few actions and you might have luck and find old commits that
have been lost by doing a complex merge.
Show a diff of the changes made since your last commit
to diff one file:
git diff -- \<filename>
to show a diff between staging area and HEAD:
git diff --cached
Show files added to the staging area, files with changes, and untracked files
Show recent commits, most recent on top. Useful options:
- –color with color
- –graph with an ASCII-art commit graph on the left
- –decorate with branch and tag names on appropriate commits
- –stat with stats (files changed, insertions, and deletions)
- -p with full diffs
- –author=foo only by a certain author
- –after=”MMM DD YYYY” ex. (“Jun 20 2008”) only commits after a certain date
- –before=”MMM DD YYYY” only commits that occur before a certain date
- –merge only the commits involved in the current merge conflicts
git log HEAD..origin/master #
Useful for seeing changes from remotes after git remote update
git show <rev>
Show the changeset (diff) of a commit specified by \
SHA1 commit ID, branch name, or tag (shows the last commit (HEAD) by default)
git show --name-only <rev>
Show only the names of the files that changed, no diff information.
git blame <file>
Show who authored each line in “file”
git blame <file> <rev>
Show who authored each line in \
git gui blame
Really nice GUI interface to git blame
git whatchanged <file>
Show only the commits which affected \
E.g. view all changes made to a file on a branch:
This could be combined with git remote show \
all branches to a particular file.
git diff <commit> head path/to/fubar
Show the diff between a file on the current branch and potentially another
git diff head -- <file>
Use this form when doing git diff on cherry-pick’ed (but not committed) changes somehow changes are not shown when using just git diff.
List all files in the index and under version control.
git ls-remote <remote> [HEAD]
Show the current version on the remote repo. This can be used to check whether
a local is required by comparing the local head revision.
Adding / Deleting
git add <file1> <file2> ...
git add <dir>
Add all files under directory \
git add .
Add all files under the current directory to the project
WARNING: including untracked files.
git rm <file1> <file2> ...
git rm $(git ls-files --deleted)
Remove all deleted files from the project
git rm --cached <file1> <file2> ...
Commits absence of \
Option 1: Edit $GIT_DIR/info/exclude. See Environment Variables below for explanation on
Option 2: Add a file .gitignore to the root of your project. This file will be checked in.
Either way you need to add patterns to exclude to these files.
git add <file1> <file2> ... git stage <file1> <file2> ...
Add changes in \
git add -p git stage --patch
Interactively walk through the current changes (hunks) in the working tree, and decide which changes to add to the staging area.
git add -i git stage --interactive
Interactively add files/changes to the staging area. For a simpler mode (no menu), try
git add --patch (above)
git reset HEAD <file1> <file2> ...
Remove the specified files from the next commit
git commit <file1> <file2> ... [-m <msg>]
git commit -a
Commit all files changed since your last commit
(does not include new (untracked) files)
git commit -v
Commit verbosely, i.e. includes the diff of the contents being committed in the commit message screen
git commit --amend
Edit the commit message of the most recent commit
git commit --amend <file1> <file2> ...
Redo previous commit, including changes made to \
List all local branches
git branch -r
List all remote branches
git branch -a
List all local and remote branches
git branch <branch>
Create a new branch named \
git branch <branch> <start-point>
Create a new branch named \
specified any way you like, including using a branch name or a tag name
git push <repo> <start-point>:refs/heads/<branch>
Create a new remote branch named
git push origin origin:refs/heads/branch-1
git push origin origin/branch-1:refs/heads/branch-2
git branch --track <branch> <remote-branch>
Create a tracking branch. Will push/pull changes to/from another repository.
git branch --track experimental origin/experimental
git branch -d <branch>
Delete the branch \
commit which is not reachable from the current branch, this command
will fail with a warning.
git branch -r -d <remote-branch>
Delete a remote-tracking branch.
git branch -r -d wycats/master
git branch -D <branch>
Even if the branch points to a commit not reachable from the current branch,
you may know that that commit is still reachable from some other branch or
tag. In that case it is safe to use this command to force git to delete the
git checkout <branch>
Make the current branch \
the version referenced by \
git checkout -b <new> <start-point>
Create a new branch
git push <repository> :<branch>
Removes a branch from a remote repository.
git push origin :old_branch_to_be_deleted
git co <branch> <path to new file>
Checkout a file from another branch and add it to this branch. File
will still need to be added to the git branch, but it’s present.
git co remote_at_origin__tick702_antifraud_blocking
git show <branch> -- <path to file that does not exist>
git show remote_tick702 -- path/to/fubar.txt
Show the contents of a file that was created on another branch and that
does not exist on the current branch.
git show <rev>:<repo path to file>
Show the contents of a file at the specific revision.
Note: path has to be absolute within the repo.
git merge <branch>
Merge branch \
and can be run as many times as needed to keep the current branch
up-to-date with changes in
git merge <branch> --no-commit
Merge branch \
result; allows you to make further tweaks
git merge <branch> -s ours
Merge branch \