GIT Cheat sheet
This was basically ripped from this site. I have refomated a few things to make it easier to read and so on.
Setup
clone the repository specified by ; this is similar to "checkout" in some other version control systems such as Subversion and CVS
Add colors to your ~/.gitconfig file:
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
Highlight whitespace in diffs
[color]
ui = true
[color "diff"]
whitespace = red reverse
[core]
whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
Add aliases to your ~/.gitconfig file:
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
dc = diff --cached
lg = log -p
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
ls = ls-files
# Show files ignored by git:
ign = ls-files -o -i --exclude-standard
Configuration
edit the .git/config [or ~/.gitconfig] file in your $EDITOR
sets your name and email for commit messages
tells git-branch and git-checkout to setup new branches so that git-pull(1) will appropriately merge from that remote branch. Recommended. Without this, you will have to add --track to your branch command or manually merge remote tracking branches with "fetch" and then "merge".
This setting tells git to convert the newlines to the system's standard when checking out files, and to LF newlines when committing in
To view all options
To ignore whitespace
You can add "--global" after "git config" to any of these commands to make it apply to all git repos (writes to ~/.gitconfig).
Info
Use this to recover from *major* mess 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 -- " 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
show commits between the specified range. Useful for seeing changes from
remotes:
show the changeset (diff) of a commit specified by , which can be any SHA1 commit ID, branch name, or tag (shows the last commit (HEAD) by default)
also to show the contents of a file at a specific revision, use
this is similar to cat-file but much simpler syntax.
show only the names of the files that changed, no diff information.
show who authored each line in
show who authored each line in as of (allows blame to go back in time)
really nice GUI interface to git blame
show only the commits which affected listing the most recent first
E.g. view all changes made to a file on a branch:
this could be combined with git remote show to find all changes on
all branches to a particular file.
show the diff between a file on the current branch and potentially another branch
shows diff for staged (git-add'ed) files (which includes uncommitted git cherry-pick'ed files)
list all files in the index and under version control.
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
add , , etc... to the project
add all files under directory to the project, including subdirectories
add all files under the current directory to the project
*WARNING*: including untracked files.
remove , , etc... from the project
remove all deleted files from the project
commits absence of , , etc... from the project
Ignoring
Option 1:
Edit $GIT_DIR/info/exclude. See Environment Variables below for explanation on $GIT_DIR.
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.
Staging
add changes in , ... to the staging area (to be included in the next commit
interactively walk through the current changes (hunks) in the working tree, and decide which changes to add to the staging area.
interactively add files/changes to the staging area. For a simpler mode (no menu), try 'git add --patch' (above)
Unstaging
remove the specified files from the next commit
Committing
commit , , etc..., optionally using commit message , otherwise opening your editor to let you type a commit message
commit all files changed since your last commit (does not include new (untracked) files)
commit verbosely, i.e. includes the diff of the contents being committed in the commit message screen
edit the commit message of the most recent commit
redo previous commit, including changes made to , , etc...
Branching
list all local branches
list all remote branches
list all local and remote branches
create a new branch named , referencing the same point in history as
the current branch
create a new branch named , referencing , which may be specified any way you like, including using a branch name or a tag name
create a new remote branch named , referencing on the remote. Repo is the name of the remote.
Example:
Example:
Example:
create a tracking branch. Will push/pull changes to/from another repository.
Example: git branch --track experimental origin/experimental
(As of Git 1.7.0)
Make an existing branch track a remote branch
Example: git branch --set-upstream foo origin/foo
delete the branch ; if the branch you are deleting points to a commit which is not reachable from the current branch, this command will fail with a warning.
delete a remote-tracking branch.
Example:
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 branch.
make the current branch , updating the working directory to reflect the version referenced by
create a new branch referencing , and check it out.
removes a branch from a remote repository.
Example:
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.
Example:
Eg.
show the contents of a file that was created on another branch and that does not exist on the current branch.
Show the contents of a file at the specific revision. Note: path has to be absolute within the repo.
Merging
merge branch into the current branch; this command is idempotent and can be run as many times as needed to keep the current branch up-to-date with changes in
merge branch into the current branch, but do not autocommit the result; allows you to make further tweaks
merge branch into the current branch, but drops any changes in , using the current tree as the new tree
Cherry-Picking
selectively merge a single commit from another local branch
Example:
get the blob of some file whether it is in a repository or not
Find the commit in the repository that contains the file blob:
Squashing
WARNING: "git rebase" changes history. Be careful. Google it.
(then change all but the first "pick" to "squash") squash the last 10 commits into one big commit
Conflicts
work through conflicted files by opening them in your mergetool (opendiff, kdiff3, etc.) and choosing left/right chunks. The merged result is staged for commit.
For binary files or if mergetool won't do, resolve the conflict(s) manually
and then do:
Once all conflicts are resolved and staged, commit the pending merge with:
Sharing
update the remote-tracking branches for (defaults to "origin"). Does not initiate a merge into the current branch (see "git pull" below).
fetch changes from the server, and merge them into the current branch.
Note: .git/config must have a [branch "some_name"] section for the current branch, to know which remote-tracking branch to merge into the current branch. Git 1.5.3 and above adds this automatically.
update the server with your commits across all branches that are *COMMON* between your local copy and the server. Local branches that were never pushed to the server in the first place are not shared.
update the server with your commits made to since your last push. This is always *required* for new branches that you wish to share. After the first explicit push, "git push" by itself is sufficient.
Example:
Which, in fact, is the same as git push origin but a little more obvious what is happening.
Reverting
reverse commit specified by and commit the result. This does *not* do the same thing as similarly named commands in other VCS's such as "svn revert" or "bzr revert", see below
re-checkout , overwriting any local changes
re-checkout all files, overwriting any local changes. This is most similar to "svn revert" if you're used to Subversion commands
Fix mistakes / Undo
abandon everything since your last commit; this command can be DANGEROUS. If merging has resulted in conflicts and you'd like to just forget about the merge, this command will do that.
undo your most recent *successful* merge *and* any changes that occurred after. Useful for forgetting about the merge you just did. If there are conflicts (the merge was not successful), use "git reset --hard" (above) instead.
forgot something in your last commit? That's easy to fix. Undo your last commit, but keep the changes in the staging area for editing.
redo previous commit, including changes you've staged in the meantime. Also used to edit commit message of previous commit.
Plumbing
determine if merging sha1-B into sha1-A is achievable as a fast forward; non-zero exit status is false.
Stashing
save your local modifications to a new stash (so you can for example
"git svn rebase" or "git pull")
restore the changes recorded in the stash on top of the current working tree state
restore the changes from the most recent stash, and remove it from the stack of stashed changes
list all current stashes
show the contents of a stash - accepts all diff args
delete the stash
delete all current stashes
Remotes
adds a remote repository to your git config. Can be then fetched locally.
Example:
delete a branch in a remote repository
create a branch on a remote repository
Example:
replace a branch with <new_remote>
think twice before do this
Example:
prune deleted remote-tracking branches from "git branch -r" listing
add a remote and track its master
show information about the remote server.
Eg.:
Track a remote branch as a local branch. It seems that somtimes an extra 'remotes/' is required, to see the exact branch name, 'git branch -a'.
For branches that are remotely tracked (via git push) but that complain about non-fast forward commits when doing a git push. The pull synchronizes local and remote, and if all goes well, the result is pushable.
Retrieves all branches from the remote repository. After this 'git branch --track ...' can be used to track a branch from the new remote.
Submodules
add the given repository at the given path. The addition will be part of the next commit.
Update the registered submodules (clone missing submodules, and checkout the commit specified by the super-repo). --init is needed the first time.
Executes the given command within each checked out submodule.
Removing submodules
- Delete the relevant line from the .gitmodules file.
- Delete the relevant section from .git/config.
- Run git rm --cached path_to_submodule (no trailing slash).
- Commit and delete the now untracked submodule files.
Updating submodules
To update a submodule to a new commit:
1. update submodule:
2. commit the new version of submodule:
3. check that the submodule has the correct version
If the update in the submodule is not committed in the main repository, it is lost and doing git submodule update will revert to the previous version.
Patches
Generate the last commit as a patch that can be applied on another clone (or branch) using 'git am'. Format patch can also generate a patch for all commits using 'git format-patch HEAD^ HEAD'
All page files will be enumerated with a prefix, e.g. 0001 is the first patch.
Generate a patch for a single commit. E.g.
Revision does not need to be fully specified.
Applies the patch file generated by format-patch.
Generates a patch file that can be applied using patch:
Useful for sharing changes without generating a git commit.
Tags
Will list all tags defined in the repository.
Will checkout the code for a particular tag. After this you'll probably want to do: 'git co -b ' to define a branch. Any changes you now make can be committed to that branch and later merged.
Archive
Will export expanded tree as tar archive at given path
Will export archive as bz2
Will export as zip
Git Instaweb
Environment Variables
Your full name to be recorded in any newly created commits. Overrides user.name in .git/config
Your email address to be recorded in any newly created commits. Overrides user.email in .git/config
Location of the repository to use (for out of working directory repositories)
Location of the Working Directory - use with GIT_DIR to specifiy the working directory root or to work without being in the working directory at all.
Changing history
Change author for all commits with given name