git flow init # setup project to use git-flow
git flow feature start <feature_name> # creates a new feature branch called <feature_name>
git flow feature finish <feature_name> # merge feature back into develop branch
git flow release start <version> # merge develop to release
https://danielkummer.github.io/git-flow-cheatsheet/
https://skoch.github.io/Git-Workflow/
https://git-scm.com/book/es/v1/Ramificaciones-en-Git-Reorganizando-el-trabajo-realizado
https://es.atlassian.com/git/tutorials/rewriting-history/git-rebase
git checkout git rebase master
- Minimal prettified output
git log --oneline --graph
- Show files modified in log messages:
git log --name-only
- Show what changed in file in each commit
git log -p filename
- Show evolution of a file
git log -- filename
-
Other useful log options
--author="..."Only show commits made by that author--reverseShow commits in reverse order (Oldest commit first)--after="..."Show all commits that happened after certain date--before="..."Show all commits that happened before certain data
-
Display commits in one branch not present in the other.
- Example: display commits
developbranch that are not present inmaster
- Example: display commits
git log master..develop
- conversely,
git log develop..mastershows commits inmasternot merged indevelop. - This is useful to show what you're about to push to a remote:
git fetch origin
git log origin/master..HEAD
- Show what happened in a commit
git show <commit>
- Example: To see the commit object two revs before the latest revision
git show HEAD~2
# or git show HEAD@{2}
- undo add with
git resetto unstage added files - revert changed to edited files using
git reset --hard HEADor alternatelygit checkout . - return to a particular point in history using
git reset --hard <commit-hash>. All changes made after this commit are discarded. - return to a particular point in history using
git reset <commit-hash>. Rungit add .andgit committo add the changes back to git. - return to a particular point in history using
git reset --soft <commit-hash>. All changes made after this commit are automatically added, i.e. staged for commit, so you only need to rungit committo add them back in. - To revert specific files, use
git checkout <commit-hash> <filename>orgit checkout <branchname> <filename>
- Use
git difforgit difftoolfor a more visual diff git difftool -dwhen used with tools like meld or winmerge which allow full directory diffs- To diff current changes:
git diff HEAD
- To diff against a branch
git diff branchname
- To view only changed filenames
git diff --stat
or
git diff --no-commit-id --name-only
- To run git diff after a git add:
git diff --staged
- Show file in another branch
git show branchname:filename - To remove tracking branches that are not present on the remote repo
git remote prune origin
- Remove local branches that have been merged
# list merged branches
git branch --merged
...
# Remove these branches
git branch -d ...
-
List unmerged branches
git branch --no-merged -
Show tracking branches
git branch -vv
- Use
git revert -n <commit> - Commit can be HEAD, HEAD
1, HEAD2, ... to shift to previous revisions - This will undo a previous commit or two, allow you to look at the changes, and see which change might have caused a problem.
git revertwithout-nwill automatically re-commit reverted files, prompting you to write a new commit message. The-nflag tells git to not commit, since all we want to do is look.
git add -iallows interactive staginggit add -pallows interactive patching for added filesgit stash -pallows interactive stashing
git stash
git stash branch branchname
- Undo last commit, but only from the repo, not the local working copy, so your latest changes are still preserved using:
git reset HEAD~ --soft
- Now stash to a new branch:
git stash
git stash branch branchname
- If the branch already exists:
git stash
git checkout branchname
git stash pop
git branch newbranchname
git reset HEAD~n --hard # n is the number of commits to undo
git checkout newbranchname # this contains the previous commits from master
git checkout your-branch-name
git cherry-pick master
git checkout master
git reset HEAD~n --hard # n is the number of commits to undo
git commit -a
NOTE Files not tracked will not be added
- Fix an incorrect commit message
git commit --amend
- Add files that should've been there in the previous commit:
git add ...
git commit --amend --no-edit # reuse previous commit msg
- If you make a new commit to fix a previous bad commit, you can use
# git add/rm/mv ...
git commit --fixup <badcommit>
git rebase -i --autosquash <badcommit>^
git log -S <whatever> --source --all
- Use
git grepinstead ofgrep -Rto search a git tree. - To show function names containing a pattern, use
-p - Eg:
git grep -p basename *.sh - Use
--breakand--headingto make the output more readable - To search for more than one pattern:
-e pattern1 --and -e pattern2 - To search for more than one pattern ('--or' implied):
-e pattern1 -e pattern2 - To negate a pattern:
--not -e pattern1
git push [remote-name] [branch-or-tag-to-push]- Eg:
git push origin master - Eg:
git push origin v1.0.2
git remote show <remotename>- Eg:
git remote show origin
git show <tagname>
-
Checkout branch and track remote
git checkout -b feature/project --track origin/feature/project -
Show current branch name
git rev-parse --abbrev-ref HEAD -
Show all local branches with your starred:
git branch -
Are we up to date with remote
git status -uno -
Add submodule
git submodule add <PATH> -
Create and push branch
git checkout -b develop
git push -u origin develop
-
Pull a repo
git pull --recurse-submodules --no-commit --rebase -
Recursive update of submodules
git submodule update --init --recursive -
Delete a tag from origin. git tag -d 0.1.3 git push origin :refs/tags/0.1.3
-
Replace entire folder with one from another branch. git rm -r /path/to/folder git checkout OTHER_BRANCH -- /path/to/folder
-
Move a submodule git mv old/submod new/submod
-
List submodules grep path .gitmodules | sed 's/.*= //'
-
Show contents of a stash git stash show -p
-
Show file from commit/branch git show 8bbd06c:app/models/user.rb
git fetch git branch --all
for branch in $(git branch -vv | grep ‘: gone]’ | awk ‘{print $1}’); do git branch -D “$branch”; done
- Change branch parent git rebase --onto
git archive HEAD --format=zip > archive.zip
git whatchanged --since '12/01/2016' --oneline --name-only --pretty=format: | sort | uniq
git log --since="4 day ago" --name-only --pretty=format: | sort -u
git branch -av
f files, d directories, X ignored, x ignored and non-ignored
git clean -f
git clean -fd
git clean -fx
git archive HEAD --format=zip > archive.zip
git config --global user.email "[email protected]"```
Set up core editor
for Atom, use ```git config --global core.editor "atom --wait"```
for SublimeText use ```git config --global core.editor "/path/to/subl --wait --new-window"```
for VSCode use ```git config --global core.editor "/path/to/code --wait"```
```git config --global diff.tool meld ```#used by git difftool, use vimdiff if meld is not available
## crlf policy
###Linux:
```git config --global core.autocrlf input```
### Windows
```git config --global core.autocrlf true```
Long filenames on Windows
To overcome the 260 char path limit on Windows
```git config --system core.longpaths true```
or alternately
```git config --global core.longpaths true```
## Alias
```git config --global alias.ci commit```
```git config --global alias.st "status --short"```
## Display commits in one branch not present in the other.
Example: display commits develop branch that are not present in master
```git log master..develop```
conversely, git log develop..master shows commits in master not merged in develop.
## Show what happened in a commit
```git show <commit>```
Note
git config --globalmodifies $HOME/.gitconfig
git config --systemmodifies the systemwide /etc/gitconfig
git config --file <filename>creates/modifies the named config file