HINT:
To avoid conflicts, cherry pick with the same order of the original commits
(i.e. use Unix tac
command to reverse sort the git log)
git cherry-pick -X ignore-all-space <commit_hash>
e.g.
git cherry-pick -X ignore-all-space 123f2fs
git format-patch -1
e.g.
git format-path -1 ccc4752
git format-patch master --stdout > <patch_file_name>
e.g.
# Get Latest Changes
git pull
# Create and switch to new bug fix branch
git checkout -b bug-fix-branch
# Perform bug fixes and commit them
# Spit out the patch file
git format-patch master --stdout > bug-fix-1.patch
git apply --stat <patch_file_name>
git apply --check <patch_file_name>
git am < <patch_file_name>
e.g.
# Review the changes
git apply --stat bug-fix-1.patch
# Simulate if git can apply the change
git apply --check bug-fix-1.patch
# Apply the patch
git am < bug-fix-1.patch
# Patch is applied and committed. Ready to push!
git clone <user@server:path/to/repo.git>
e.g.
git clone [email protected]:TomyJaya/git-real.git
git clone
e.g.
git clone https://github.com/TomyJaya/git-real.git
Status
git status
git add <file_name>
e.g.
git add README.md
git add .
git add -A
See: http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add
git diff-tree --no-commit-id --name-status -r <commit_id>
e.g.
git show --pretty="format:" --name-status 53571fc
git --no-pager show --pretty="format:" --name-status 53571fc
git diff
git diff --cached
git diff --staged
Hint: cached and staged are synonymous.
git diff <start_commit>..<end_commit> -- <path/to/file>
e.g.
git diff abcsdef..HEAD --ivysettings.xml # between now and abcdef commit
git diff HEAD~2..HEAD --ivysettings.xml # between now and two commmits back
git commit -m ""
e.g.
git commit -m "Initial Commit"
git commit -am ''
e.g.
git commit -am 'added new benchmarks'
git rm
e.g.
git rm PROJECTS.md
git rm --cached
e.g.
git rm --cached README
git mv <file_from> <file_to>
e.g.
git mv README.md README
git log
git --no-pager log -n 20 --pretty=oneline --abbrev-commit
git log --abbrev-commit --pretty=oneline ### Get abbreviated commit
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph
git log --graph --oneline
git log --since=2.weeks
# Another really helpful filter is the -S option which takes a string and only shows the commits that introduced a change to the code that added or removed that string.
git log -Sfunction_name
git commit -m 'initial commit'
git add forgotten_file.txt
git commit --amend
# You end up with a single commit & the second commit replaces the results of the first.
WARNING: Don't amend your last commit if you've already pushed it!
git reset HEAD <file_name>
e.g.
git reset HEAD CONTRIBUTING.md
git reset --hard HEAD~1
Note: Undo commit/ local commit and nuke last commit and never see it again.
git reset HEAD~1
git reset --soft HEAD~1
Hint: Be careful of using this command as you can't get the modification back
git reset --hard
Hint: Be careful of using this command as you can't get the untracked files back
git clean -fd
git checkout --
git checkout HEAD
e.g.
git checkout -- CONTRIBUTING.md
git checkout HEAD CONTRIBUTING.md
Note: http://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes
WARNING: It's important to understand that git checkout -- [file] is a dangerous command. Any changes you made to that file are gone & you just copied another file over it. Don't ever use this command unless you absolutely know that you don't want the file.
DANGEROUS COMMAND:
# The below will discard all unstaged changes!!!
git checkout -- .
Note: if you accidentally made a wrong change to a file and committed it, you can get back the last but one checked in file:
git checkout HEAD~1 <file>
git remote
git remote -v ### shows URL
Note: remote repositories are versions of your project that are hosted on the Internet or network somewhere.
git remote add
e.g.
git remote add pb https://github.com/paulboone/ticgit
IMPORTANT: http://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
- If you clone a repository, the command automatically adds that remote repository under the name ìoriginî. So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It's important to note that the git fetch command pulls the data to your local repository & it doesn't automatically merge it with any of your work or modify what youíre currently working on. You have to merge it manually into your work when youíre ready.
- If you have a branch set up to track a remote branch, you can use the git pull command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the git clone command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on.
git fetch
e.g.
git fetch origin
git push origin master
git remote show origin
git remote rename pb paul
git remote rm paul
git tag
git tag -l 'v1.8.5*' ### search with a particular pattern
git tag -a <version_name> -m '<tag_message>'
e.g.
git tag -a v1.0 -m 'my version 1.0'
git show v1.0
git tag v1.4-lw
git tag -a <version_name> <abbreviated_index_checksum>
e.g.
git tag -a v1.2 9fceb02
git push origin
e.g.
git push origin v1.5
git push origin --tags ### transfer all of your tags to the remote server that are not already there
Note: You can't really do that, instead, put a version of the repository in your working directory that looks like a specific tag.
### Create a new branch version2 which has contents as of tag v2.0.0
git checkout -b version2 v2.0.0
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
### Can do git unstage file instead of git reset HEAD file
git config --global alias.unstage 'reset HEAD --'
### see the last commit easily
git config --global alias.last 'log -1 HEAD'
git branch
git branch -v ### with last commit on each branch
git branch --merged ### only merged branch
git branch --nomerged ### only unmerged branch
git branch <new_branch_name>
git checkout <branch_name>
e.g.
git checkout branch123 or git checkout master
git checkout -b <branch_name>
git branch -d <branch_name>
Note: will delete even if there's unmerged changes
git branch -D <branch_name>
e.g. serverfix
git fetch origin
git merge origin/serverfix
Note: If you want your own serverfix branch that you can work on, you can base it off your remote branch
git checkout -b serverfix origin/serverfix
Note: Checking out a local branch from a remote branch automatically creates what is called a "tracking branch" (or sometimes an "upstream branch"). Tracking branches are local branches that have a direct relationship to a remote branch. If youíre on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.
git checkout --track origin/serverfix
git branch -u origin/serverfix
git branch -vv
git push origin --delete serverfix
Note: However, there is another way: you can take the patch of the change that was introduced in C4 and reapply it on top of C3. In Git, this is called rebasing. With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.
git checkout experiment
git rebase master
git checkout master
git merge experiment
# Check out the client branch, figure out the patches from the common ancestor of the client and server branches, and then replay them onto master
git rebase --onto master server client
# fast-forward your master branch
git checkout master; git merge client
### You can rebase the server branch onto the master branch without having to check it out first by running git rebase [basebranch] [topicbranch] which checks out the topic branch (in this case, server) for you and replays it onto the base branch (master):
git rebase master server
### fast-forward the base branch (master):
git checkout master; git merge server
git stash
git stash save
git stash -u
### Git will also stash any untracked files you have created
git stash --include-untracked
git stash list
git stash apply
git stash apply stash@{2}
git stash drop stash@{0}
git stash pop
git clean
git clean -d -n ### -n option, which means ìdo a dry run and tell me what you would have removedî.
git stash branch testchanges
git stash --all
git diff --check
git reflog
git show master@{yesterday}
git log master..experiment
git log origin/master..HEAD
git log refA refB ^refC
git log refA refB --not refC
git log --left-right master...experiment
git grep -n <phrase> ### will show line number
git grep --count <phrase> ### will show count
git grep -p <phrase> ### will find method
git grep --break --heading -n <phrase> ### more readable format