Skip to content

Instantly share code, notes, and snippets.

@fstorr
Last active August 29, 2015 14:07
Show Gist options
  • Save fstorr/17ade9adb5002bd44712 to your computer and use it in GitHub Desktop.
Save fstorr/17ade9adb5002bd44712 to your computer and use it in GitHub Desktop.
Helpful git commands
# Useful Git commands
## Config Options
git config --global user.name "First Last"
git config --global user.email "emailaddress"
git config user.email "emailaddress" : sets email address for a repo instead of globally
git config user.email : shows which email address it is using in the repo you're currently in
git config --global core.editor emacs : should you want to specifiy an editor for interactive commands See [associating text editors with Git](https://help.github.com/articles/associating-text-editors-with-git/) for more options
git config --global merge.tool opendiff : specify a merge tool (OSX only)
git config --list : lists the config options you have set
git config --global alias.mylog "log --pretty=format:'%h %s [%an] --graph'" : create an alias called mylog that runs git log with your choice of flags
git config --global alias.co checkout : create an alias called co that calls git checkout
## Diffs
git diff filename : the diff of the differences between the **unstaged** file and the last commit
git diff --staged filename : the diff of the differences between the **staged** file and the last commit
git diff HEAD : the same as git diff
git diff HEAD^ : the parent of the latest commit
git diff HEAD^ ..HEAD : the second most recent commit vs the most recent
git diff HEAD^^ : the grandparent of the latest commit
git diff HEAD~5 : five commits ago
git diff SHA .. SHA : the diff between two SHAs
git diff brancha branchb : the diff between two branches
You can also use the same time ranges as in git log (see below)
## Staging
git reset HEAD filename : remove file from staging
git checkout -- filename : remove all the changes from the file and revert to its state in the last commit
## Committing
git commit -a -m "message" : commit all currently tracked files
git commit --amend -m "message" : use altering a file and wanting to amend it to the last commit. **Don't do after a push**
git reset --soft HEAD^ : undo everything from the last commit and move it back into staging (^^ == last two commits, etc). **Don't do after a push**
git reset --hard HEAD^ : completely remove the last commit and all changes (^^ == last two commits, etc). **Don't do after a push**
## Branches
git checkout -b branchname : create a new branch and switch branches
git checkout branchname : switch branches
git merge branchname : merge branches (make sure to be on the master to merge a branch into it)
git branch -d branchname : delete a branch
git branch -r : show all remote branches **note: doesn't check for new branches**
git remote show origin: show remote branches and whether they're tracked or not, the local branches and which remote branches they merge with, and also the local branches and where they're configured when a git push happens. This command goes to the remote and tells you if any local branches are out of date.
git fetch : get remote branches but don't auto merge them
git fetch -t : get remote tags
git remote prune origin : prune (i.e. clean up) "stale" (i.e. no longer there) references to remote branches
## Pushing
git push origin :branchname : deletes remote branch
git push origin branchname : push to remote branch and start tracking it
git push origin localbranchname:remotebranchname : push to origin and connect the local branch name to a differently named remote branch.
## Tags
git tag: list all tags
git tag -a tagname -m "tag description" : add a tag name (e.g. git tag -a v1.0 -m "deployed version 1.0")
git push --tags : push tags to remote. If you don't do this, the tags remain local.
git checkout tagname : checkout the code at that commit
## git log
git config --global color.ui=true : will add colour to git's command line output
git log --oneline -p : the p (i.e. patch) flag shows line-by-line what was added and removed
git log --oneline --stat : shows how many insertions and deletions were made for each file in each commit
git log --oneline --graph : show a visual representation of the branches and the commits on them
git log --pretty=oneline : show the commit SHA on the same line as the commit message
git log --pretty=format:"placeholders go here"
| Placeholder | Replaced With |
|---------------|
| %ad | author date |
| %ae | author email |
| %an | author name |
| %h | SHA hash |
| %s | subject |
| %d | ref names |
So, you can do something like this: git log --pretty=format:"%h %ad- %s [%an]"
There are a lot more options via git help log
### Log dates
git log --until=1.minute.ago
git log --since=1.day.ago
git log --since=1.hour.ago
git log --since=1.month.ago --until=2.weeks.ago
git log --since=2000-01-01 --until=2012.12.21
## git blame
git blame filename --date short : gives you the commit hash, author, date, line number, and content of who changed what on a file
## Excluding things
Open up .git/info/exclude (e.g., subl .git/info/exclude) and add the name of a file or folder you want to want to work on but don't want to have it as part of the shared repo.
## Untracking tracked things
git rm --cached filname : this will delete git's record for this thing. It won't be deleted from your machine, only from git.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment