GIT Commands Cheat Sheet
This is a collection of git commands (for use in terminal windows like Terminal, xterm, putty, etc) that I use and need to keep handy. Some I use more frequently than others, so I have categorized the list by usage frequency. I may eventually categorize it by task, as well. This list does not pretend to be complete or authoritative. If you are looking for the complete reference and manual, you should go to: the official documentation or this documentation put together by the github team.
If you have any suggestions or corrections for my list, please let me know.
git commit -am'message'
- This commits all changes (-a) with a message (m).
git add .
- This adds all new files, and any new changes to existing tracked files, to the repo for tracking.
- This pulls the latest changes from my remote repository into my local machine repository.
git checkout branchname
- This switches your local repository from one branch to the branch called branchname.
git checkout -b newbranchname
- This creates a new local branch from your current branch, and switches to it.
git merge branchname
- This merges branchname into your current branch. If the merge goes well, the changes are automatically committed to your current branch.
git push origin newbranchname
- This pushes your new branch that you created only on your local machine up to your origin repository.
- This diffs your local branch from its committed state. Useful to remember what you've changed.
- Among other things, this tells you what new files are not yet tracked.
git branch -D branchname
- deletes that branch. Capital -D causes the branch to be deleted irrespective of its merged status.
Least Used, but Very Important
- Create a new repository. New repo can be empty or made from existing source code. SEE DOC for recipe for making a new repo from existing code.
git clone email@example.com:path/on/remote/server/from/homedir/to/repo/
- This copies your remote repo to your local dev machine. This is what you do to get started on local development.
git revert filename.php
- This reverts local changes in file, back to the original version that was committed.
git config ‐‐global user.email "firstname.lastname@example.org"
- This is used once at the beginning when setting up your local repo, git seems to need it to push changes back to a remote repo.
git config ‐‐global user.name "John Doe"
- ditto above
git reset ‐‐hard
- Resets tree and index to HEAD, discards changes to tracked files. SEE DOC
git clean -df
- Removes untracked files from the working tree. SEE DOC
git branch -v
- Tells you what branch you are on, the hash of the last commit, and that commit's message.
git checkout -m 339c91e filename
- If you do git fetch first, and then git branch -v on the remote branch you just fetched from to learn the hash of the latest commit, you can then run this command, which updates just a single file (filename) with the version that is in the commit hash.
git checkout-index -a -f --prefix=/full/path/to/new/directory/
- This is what you use to export your repo to a new location. This command is very useful to deploy flat files from qa to prod, for example. It takes all of the files in the index (-a) and puts them into the new directory. -f is used to overwrite same-named files in the new directory with the new files coming in from the export. Be sure to put the slash at the end of your path. If you don't, that 'directory' will become a prefix on all of your files. Also, don't use the ~ shorthand for your home directory. Use your full path from root.
git rm --cached dirname/filename.ext
- This command is used to remove a file from the git index. The scenario that I used it for was to remove a file from git that was being tracked, even though I had added the filename to the .gitignore list. I had first mistakenly added the file to the git index. I then realized that I did not want it tracked. So I added the file to the .gitignore list. But then it was still being tracked. So I had to run this command on the file.