Michał Urbanek
PS1='\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:
\[\e[0;34m\]\w\[\e[0;31m\]$(git_prompt)\[\e[0m\]\$ '
git_prompt ()
{
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
echo " [$git_branch]"
}
# git completion
source ~/.git-completion.bash
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.visual "!gitk"
# add file.txt to staging area
git add file.txt
# display changes scheduled for committing
git diff --cached
# add only patch of given file
git add -p
# git stash with description MESSAGE
git stash save MESSAGE
# list all stashes
git stash list
# show diff from stash STASH
git stash show -p STASH
# apply and remove last stash
git stash pop
# apply and remove stash STASH
git stash pop STASH
# custom alias to actually make a commit
git save
alias.save !git add -A && git commit -m 'SAVEPOINT'
# see commits with last changes file.txt in lines 5-10
git blame -L5,10 file.txt
# see commits with last changes in file.txt before commit <COMMIT-ID>
git blame COMMIT-ID -- file.txt
# get all commits which diff contained TEXT
git log -S"TEXT"
# start bisect between GOOD and BAD commits
git bisect start BAD GOOD
# mark commit as correct
git bisect good
# mark commit as invalid
git bisect bad
# run command "test.sh" automatically for each commit
git bisect run test.sh
Are git commits immutable?
A DAG of immutable commit objects where branches are named mutable pointers into it
git merge master feature
git checkout feature
git rebase master
git checkout feature
git rebase -i master
People can (and probably should) rebase their private trees (their own work). That's a cleanup.
But never other peoples code. That's a "destroy history".
Linus Torvalds
The reflog is Git’s safety net. It records almost every change you make in your repository, regardless of whether you committed a snapshot or not.
git reflog
# add submodule from repository at <MODULE-URL>
git submodule add <MODULE-URL>
# see detailed submodule changes
git diff --submodule
# init submodule after clone
git submodule init
# populate submodule
git submodule update
# update submodule to newest version from submodules repo
git submodule update --remote
# update submodule and rebase local changes (if any)
git submodule update --remote --rebase
# push changes together with all local submodule commits
git push --recurse-submodules=on-demand
# useful aliases
git config alias.sdiff '!'"git diff && git submodule foreach 'git diff'"
git config alias.spush 'push --recurse-submodules=on-demand'
git config alias.supdate 'submodule update --remote --merge'
Command | Scope | Common use cases |
---|---|---|
git reset | Commit-level | Discard commits in a private branch or throw away uncommited changes |
git reset | File-level | Unstage a file |
git checkout | Commit-level | Switch between branches or inspect old snapshots |
git checkout | File-level | Discard changes in the working directory |
git revert | Commit-level | Undo commits in a public branch |
git revert | File-level | (N/A) |