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. I have categorized the list by task/problem type. 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.
If you have any suggestions or corrections for my list, please let me know.
Get out of trouble – Undo commits – Restart with a Clean Slate
git reset ‐‐hard
- Resets tree and index to HEAD, discards changes to tracked files. SEE DOC
git reset --hard origin/[branchName]
- This one is great. If you’ve made some bad commits or merges and just want to start over with a clean version from the origin, this will do it for you.
git reset ‐‐soft HEAD~
- Undo a commit but leave existing changes staged. SEE DOC
git revert filename.php
- This reverts local changes in file, back to the original version that was committed.
Start a New Project
- Create a new git repository in the directory you are already in. SEE DOC for recipe for making a new repo from existing code.
git config ‐‐global user.email "[email protected]"
- 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 clone [email protected]: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.
The Daily Grind – Committing, Merging, Updating, etc.
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 pull --no-commit
- Same as above, but does not commit the merge result that happened from the pull.
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.
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 clean -df
- Removes untracked files from the working tree. SEE DOC
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.
git branch -D branchname
- deletes that branch. Capital -D causes the branch to be deleted irrespective of its merged status.
- 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 -v
- Tells you what branch you are on, the hash of the last commit, and that commit’s message.
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.
The github team also maintains a similarly organized cheatsheat which you may find handy: this documentation put together by the github team.