Git cheat sheet
Some commands I document to recall when necessary.
- Move most recent commits to new branch Stackoverflow
# Note: Any changes not committed will be lost.
git branch newbranch # Create a new branch, saving the desired commits
git reset --hard HEAD~3 # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch # Go to the new branch that still has the desired commits
- Delete branches
git push -d <remote> <branch>
git branch -d <branch>
git fetch --all --prune # to get rid of branches on other machines. Note that this only removes the tracking and doesn't actually delete the branches locally
git branch -a # to view all branches
git branch -r # to view remote branches
git remote show origin
# the following deletes all local branches which aren't present. Use -D if it doesn't work. Usually that's because you have squashes and merges that's not present on local.
git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
- see tracking branches
git branch -vv
git remote show origin
- Squash commits
git rebase -i HEAD~3 # to rebase 3 commits. See the devroom link for explanation
git rebase -i <after-this-commit> # pick/squash commits
git checkout <commit>
git checkout <branch>
Hope that helps