Useful Git Commands

Words like $file should be replaced with the actual filename (or whatever) you care about.

Getting help
git help                       # list of available commands git help $command              # get help for `git $command` - complete, but usually terse and short on examples

Finding out what's going on
git status                     # list modified, added, deleted and untracked files git diff                       # list all the uncommitted changes in the working copy git diff --cached              # show the current contents of the staging area git diff $commit-id            # show changes between the current commit and $commit-id git diff $file                 # show changes to $file

Renaming and deleting files
git mv $oldname $newname       # rename the file $oldname to $newname git rm $file                   # remove $file from the working copy, and mark it as deleted in the staging area git rm --cached $file          # remove the changes to $file from the staging area, but leave them in the working area

In the first two cases, the deletion or renaming will be recorded as part of your next commit.

Committing changes
git add $file1 $file2...       # add the current version of $file1, $file2... to the staging area git add -p                     # interactively pick sets of changed lines to add to the staging area git commit                     # create a new commit containing the staged changes git commit -a                  # stage all changes in the working copy, then commit them git commit --amend             # replace the last commit with the contents of the staging area # (useful for fixing errors)

Oops! You made a mistake
git checkout $file             # throw away changes to $file (loses those changes permanently!) git reset --hard HEAD          # throw away all uncommitted changes to the working copy, permanently

Temporarily stashing changes
For when you want to stop what you're working on, but might want it back later.

git stash                      # stash away all uncommitted changes somewhere they can be retrieved later git stash apply                # restore stashed changes

Branching and merging
git checkout $branch           # change to branch $branch git checkout -b $branch        # create a new branch called $branch, and change to it    git branch                      # see a list of branches in the current repository git merge $branch1 $branch2... # create a new commit merging the changes from $branch1, $branch2... git merge -s recursive -Xours $branch # merge $branch, choosing the current branch's version whenever there's a conflict git merge -s recursive -Xtheirs $branch # merge $branch, choosing $branch's version whenever there's a conflict

(Have you added your current git branch to your shell prompt yet?)

Rewriting history
git rebase $branch             # rewrite your recent changes so they apply on top of $branch instead git rebase --interactive $commit-id # interactively reorder, combine and delete changes since $commit-id git cherry-pick $commit-id     # apply the changes in $commit-id to the current branch, creating a new commit git reflog                     # see a list of recently-visited commits; useful for recovering "lost" history

Finding out what's been happening and tracking down particular changes
git log                        # see a list of commits ancestral to the current commit git log -p                     # see the contents of commits ancestral to the current commit git log -S$string              # Look for commits that introduce or remove an instance of $string. git bisect                     # find the commit in which a bug was introduced

Interacting with remote repositories
git remote add $name $url      # add a "remote", allowing you to refer to the repo at $url by $name git fetch $remote              # fetch new commits from the repository $remote git pull $remote               # fetch new commits from $remote and merge them git push $remote               # send your new commits to $remote (you must have write access on $remote) git push -f $remote            # send your new commits to $remote, even if they're not descendants of the # current HEAD commit on $remote git push -u $remote $branch    # create a new branch on $remote called $branch, containing the contents of                                    # your local $branch git branch $name -t $remote/$remote_name # Create a local branch $name that tracks the branch $remote_name on $remote

Moving the current branch pointer
git reset $commit-id           # set the current branch to point to $commit-id, leaving the working # copy unchanged git reset --hard $commit-id    # move the current branch to $commit-id, updating the working # copy to that commit