# Set your identity. git config --global user.name "John Doe" git config --global user.email johndoe@example.com # Set your editor. git config --global core.editor emacs # Enable color support for commands like `git diff`. Disable with `never` or # partially disable -- unless otherwise applied -- with `false`. git config --global color.ui true # Stage all changes for commit. git add [--all|-A] # Stash changes locally. This will keep the changes in a separate changelist, - # called 'stash', and the working directory is cleaned. You can apply changes # from the stash at any time. git stash # Stash changes with a message. git stash save "message" # List all the stashed changes. git stash list # Apply the most recent change and remove the stash from the stash list. git stash pop # Apply stash from the stash list, but does not remove the stash from the list. git stash apply stash@{6} # Commit staged changes. git commit -m "Your commit message" # Edit previous commit message. git commit --amend # Commit in the past. Newer versions of Git allow `--date="2 days ago"` usage. git commit --date="`date --date='2 day ago'`" git commit --date="Jun 13 18:30:25 IST 2015" # Change the date of an existing commit. git filter-branch --env-filter \ 'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ] then export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800" export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700" fi' # Remove staged and working directory changes. git reset --hard # Go 2 commits back. git reset --hard HEAD~2 # Remove untracked files. git clean -f -d # Remove untracked and ignored files. git clean -f -d -x # Push to the tracked master branch. git push origin master # Push to a specified repository. git push git@github.com:[USER_NAME]/[REPO_NAME].git # Delete the branch "branch_name". git branch -D [BRANCH] # Make an existing branch track a remote branch. git branch -u upstream/foo # List all local and remote branches. git branch -a # See who committed which line in a file. git blame [FILE] # Sync a fork with the master repo. git remote add upstream git@github.com:name/repo.git # <-- Set a new repo. git remote -v # <-- Confirm new remote repo. git fetch upstream # <-- Get branches. git branch -va # <-- List local - remote branches. git checkout master # <-- Checkout local master branch. git checkout -b new_branch # <-- Create and checkout a new branch. git merge upstream/master # <-- Merge remote into local repo. git show 83fb499 # <-- Show what a commit did. git show 83fb499:path/to/file.ext # <-- Show the file as it was in 83fb499. git diff branch_1 branch_2 # <-- Check difference between branches. git log # <-- Show all of the commits. git status # <-- Show the changes from the last commit. # Display the commit history of a set of files. git log --pretty=email --patch-with-stat --reverse --full-index -- Admin\*.py > Sripts.patch # Import commits from another repo. git --git-dir=../some_other_repo/.git format-patch -k -1 --stdout | git am -3 -k # View commits which would be pushed. git log @{u}.. # View changes which are new on a feature branch. git log -p feature --not master git diff master...feature # Interactive rebase for the last 7 commits. git rebase -i @~7 # Show changes to files WITHOUT considering them a part of git. This can be # used to diff files which are not part of a git repo! git diff --no-index path/to/file/A path/to/file/B # Pull changes, while overwriting any local commits. git fetch --all git reset --hard origin/master # Update all submodules. git submodule update --init --recursive # Perform a shallow clone, to only get the latest commits, which helps to save # data (good for limited data connections) when cloning large repos. git clone --depth 1 # Unshallow a clone. git pull --unshallow # Create a bare branch; without any commits. git checkout --orphan branch_name # Checkout a new branch from a different starting point. git checkout -b master upstream/master # Reset local branch to upstream branch, then checkout it. git checkout -B master upstream/master # Remove all stale branches; ones that have been deleted on remote. So if you # have a lot of useless branches, delete them on GitHub and then run this. git remote prune origin # Prune all remotes at once. git remote prune $(git remote | tr '\n' ' ') # Revisions can also be identified with `:/text`. So, this will show the first # commit that has the string "cool" in its message body. git show :/cool # Undo parts of the last commit in a specific file. git checkout -p HEAD^ -- /path/to/file # Revert a commit, but keep the history of the event as a separate commit. git revert # Apply only the changes made within a given commit. This is different to the # `merge` command, as it would otherwise apply all commits from a branch. git cherry-pick [HASH] # Undo last commit. If you want to nuke commit C to never see it again: # (F) # A-B-C # ↑ # master git reset --hard HEAD~1 # Undo last commit. If you want to undo the commit, but keep your changes: # (F) # A-B-C # ↑ # master git reset HEAD~1 # List files changed in a given commit. git diff-tree --no-commit-id --name-only -r [HASH] # Porcelain-ly List files changed in a given commit; user-facing approach. git show --pretty="" --name-only bd61ad98 # See everything you have done, across branches, in a glance, then go to the # place right before you broke everything. git reflog git reset HEAD@{hash} # Move your most recent commit from one branch, to stage it on [BRANCH]. git reset HEAD~ --soft git stash git checkout [BRANCH] git stash pop git add .