Skip to content

Instantly share code, notes, and snippets.

@magical
Created April 18, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magical/ef2b13dbbcc5a4359ebdf64b87dbde78 to your computer and use it in GitHub Desktop.
Save magical/ef2b13dbbcc5a4359ebdf64b87dbde78 to your computer and use it in GitHub Desktop.
git tips & tricks
git tips & tricks
0. cloning
git clone ./repo ./clone # do a local clone
1. the index / patch mode
git add -p
git diff --staged / git diff --cached
git reset -p
git checkout -p
1b. aliases
[alias]
dc = diff --cached
ci = commit
co = checkout
di = diff
st = status
2. stash
git checkout feature-branch
# error: local changes
git stash
git checkout feature-branch
git stash pop
git stash show -p stash@{1}
3. ammending
git commit --amend
4. rebasing
git rebase
git rebase -i
git reflog # recover from mistakes
git cherry-pick
5. color
[color]
branch = auto
diff = auto
grep = auto
interactive = auto
status = auto
6. pretty logs
git log --graph --decorate --oneline --all
git log --all --graph --pretty=format:'[%C(cyan)%h%Creset] %s%C(bold cyan)%d%Creset'
git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
gitk
7. gitignore
three places:
.gitignore # repo ignores
.git/info/exclude # local repo ignores
~/.config/git/ignore # user ignores
git config core.excludesFile '~/.gitignore'
8. push & pull tricks
git push --force-with-lease # force push if local branch matches remote
git pull --rebase origin master # do a rebase instead of a merge when pulling
git fetch origin foo:foo # fast-forward a local branch without checking it out
branch.autosetuprebase = always
9. misc
http://caiustheory.com/git-git-git-git-git/
[alias]
git = !git
https://stackoverflow.com/questions/11543593/grep-for-stuff-in-multiple-git-repositories
function ggrep() {
find . -type d -name .git |
while read line; do
(
cd $line/..
cwd=$(pwd)
echo "$(tput setaf 2)$cwd$(tput sgr0)"
git grep -n "$@"
)
done
}
git diff --patience
git config merge.conflictstyle diff3
https://github.com/garybernhardt/dotfiles/tree/master/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment