2015-06-19 19:41:48 +02:00
|
|
|
# To set your identity:
|
2013-08-11 21:37:11 +02:00
|
|
|
git config --global user.name "John Doe"
|
|
|
|
git config --global user.email johndoe@example.com
|
|
|
|
|
2013-09-03 13:25:16 +02:00
|
|
|
# To set your editor:
|
|
|
|
git config --global core.editor emacs
|
|
|
|
|
2013-08-22 03:34:11 +02:00
|
|
|
# To enable color:
|
2013-08-11 21:37:11 +02:00
|
|
|
git config --global color.ui true
|
2013-08-27 02:34:36 +02:00
|
|
|
|
|
|
|
# To stage all changes for commit:
|
|
|
|
git add --all
|
|
|
|
|
2016-08-28 04:43:08 +02:00
|
|
|
# To 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 anytime
|
|
|
|
git stash
|
|
|
|
|
|
|
|
# To stash changes with a message
|
|
|
|
git stash save "message"
|
|
|
|
|
|
|
|
# To list all the stashed changes
|
|
|
|
git stash list
|
|
|
|
|
|
|
|
# To apply the most recent change and remove the stash from the stash list
|
|
|
|
git stash pop
|
|
|
|
|
|
|
|
# To apply any stash from the list of stashes. This does not remove the stash
|
|
|
|
# from the stash list
|
|
|
|
git stash apply stash@{6}
|
|
|
|
|
2013-08-27 02:34:36 +02:00
|
|
|
# To commit staged changes
|
|
|
|
git commit -m "Your commit message"
|
|
|
|
|
2013-10-24 06:01:42 +02:00
|
|
|
# To edit previous commit message
|
|
|
|
git commit --amend
|
|
|
|
|
2015-06-19 19:41:48 +02:00
|
|
|
# Git commit in the past
|
|
|
|
git commit --date="`date --date='2 day ago'`"
|
|
|
|
git commit --date="Jun 13 18:30:25 IST 2015"
|
2015-07-10 00:41:00 +02:00
|
|
|
# more recent versions of Git also support --date="2 days ago" directly
|
2015-06-19 19:41:48 +02:00
|
|
|
|
2015-08-03 14:14:58 +02:00
|
|
|
# To 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'
|
|
|
|
|
2014-04-25 00:08:58 +02:00
|
|
|
# To removed staged and working directory changes
|
|
|
|
git reset --hard
|
|
|
|
|
2015-06-19 19:41:48 +02:00
|
|
|
# To go 2 commits back
|
|
|
|
git reset --hard HEAD~2
|
|
|
|
|
2014-04-25 00:08:58 +02:00
|
|
|
# To remove untracked files
|
|
|
|
git clean -f -d
|
|
|
|
|
|
|
|
# To remove untracked and ignored files
|
|
|
|
git clean -f -d -x
|
|
|
|
|
2013-08-27 02:34:36 +02:00
|
|
|
# To push to the tracked master branch:
|
|
|
|
git push origin master
|
|
|
|
|
|
|
|
# To push to a specified repository:
|
|
|
|
git push git@github.com:username/project.git
|
2013-08-27 10:56:53 +02:00
|
|
|
|
2013-10-24 04:06:48 +02:00
|
|
|
# To delete the branch "branch_name"
|
|
|
|
git branch -D branch_name
|
|
|
|
|
2015-06-19 19:41:48 +02:00
|
|
|
# To make an exisiting branch track a remote branch
|
|
|
|
git branch -u upstream/foo
|
|
|
|
|
2013-12-03 21:20:11 +01:00
|
|
|
# To see who commited which line in a file
|
|
|
|
git blame filename
|
|
|
|
|
2013-08-27 10:56:53 +02:00
|
|
|
# To sync a fork with the master repo:
|
2013-08-28 01:43:24 +02:00
|
|
|
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
|
2013-08-28 09:30:37 +02:00
|
|
|
git checkout master # Checkout local master branch
|
2013-10-24 04:06:48 +02:00
|
|
|
git checkout -b new_branch # Create and checkout a new branch
|
2013-08-28 01:43:24 +02:00
|
|
|
git merge upstream/master # Merge remote into local repo
|
2013-09-01 15:05:38 +02:00
|
|
|
git show 83fb499 # Show what a commit did.
|
2014-01-07 11:33:36 +01:00
|
|
|
git show 83fb499:path/fo/file.ext # Shows the file as it appeared at 83fb499.
|
2013-10-24 06:01:42 +02:00
|
|
|
git diff branch_1 branch_2 # Check difference between branches
|
2013-10-19 09:31:02 +02:00
|
|
|
git log # Show all the commits
|
|
|
|
git status # Show the changes from last commit
|
2015-05-19 20:57:14 +02:00
|
|
|
|
|
|
|
# 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 <commit SHA> | git am -3 -k
|
2015-06-19 19:41:48 +02:00
|
|
|
|
|
|
|
# View commits that will be pushed
|
|
|
|
git log @{u}..
|
|
|
|
|
|
|
|
# View changes that 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
|
|
|
|
|
|
|
|
# Diff files WITHOUT considering them a part of git
|
|
|
|
# This can be used to diff files that are not in a git repo!
|
|
|
|
git diff --no-index path/to/file/A path/to/file/B
|
|
|
|
|
|
|
|
# To pull changes while overwriting any local commits
|
|
|
|
git fetch --all
|
|
|
|
git reset --hard origin/master
|
2015-08-03 14:15:38 +02:00
|
|
|
|
|
|
|
# Update all your submodules
|
|
|
|
git submodule update --init --recursive
|
|
|
|
|
|
|
|
# Perform a shallow clone to only get latest commits
|
|
|
|
# (helps save data when cloning large repos)
|
|
|
|
git clone --depth 1 <remote-url>
|
|
|
|
|
|
|
|
# To unshallow a clone
|
|
|
|
git pull --unshallow
|
2016-07-16 14:49:35 +02:00
|
|
|
|
|
|
|
# Create a bare branch (one that has no commits on it)
|
|
|
|
git checkout --orphan branch_name
|
|
|
|
|
|
|
|
# Checkout a new branch from a different starting point
|
|
|
|
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
|
|
|
|
|
|
|
|
# The following can be used to 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 "cool" in their message body
|
|
|
|
git show :/cool
|
2016-07-20 11:04:29 +02:00
|
|
|
|
|
|
|
# Undo parts of last commit in a specific file
|
|
|
|
git checkout -p HEAD^ -- /path/to/file
|