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.
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>
# list local branches $ git branch # list remote and local branches $ git branch -a # list remote branches $ git branch -r # create and checkout in one step $ git checkout -b <branchname> # rename a local branch $ git branch -m <oldname> <newname>
# 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
- Press Q to exit diff
- You can also add a file or folder name after the commands
#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>
# get rid of 'your branch is ahead of origin <branchname> by X commits $ git pull origin <branchname> $ git pull origin
# 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"
# 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
Discard changes / commits
# discard local changes for all unstaged file $ git checkout -- . # discard local commits $ git reset --hard origin/<branchname>
# delete local branch $ git branch -d <branchname> # delete remote branch $ git push origin :<branchname> # Git 1.7+ $ git push origin --delete <branchname>
# reset to previous state $ git reset --hard
git reflogto show what has been done with git session