List all branches -avv more info in list (also shows hidden branches)
| git branch
git branch -avv
|
| |
Create a new branch and checkout at the same time | git checkout -b <BRANCH_NAME>
|
Change to a branch switch also works for changing branches
| git checkout <BRANCH_NAME>
git switch <BRANCH_NAME>
|
Checkout a specific commit rather than a whole branch This moves HEAD to the specific commit
| git checkout <COMMIT_HASH>
|
Relative references Move up ancestors from a specific reference point ^ moves to the first parent of the ref e.g.main^ and ^^ to the second parent, etc.
^2 moves to the second parent if the current commit has two or more parents to move back towards
Use HEAD^ to travel backwards as the relative reference moves with you ~ moves a specified number of commits backwards e.g. HEAD~4
I think ~ without a number is the same as ^ The modifiers can also be chained together in a single command!
| git checkout <REF>^
git checkout HEAD^
git checkout HEAD^2
git checkout HEAD~4
git checkout HEAD~2^2~2
|
Moving a branch (Branch forcing) to a new location -f forces the command to happen
| git branch -f <BRANCH_TO_MOVE> <LOCATION_TO_MOVE>
|
Merge two branches The branch name used should be the one you are not currently in
| |
| git branch -d <BRANCH_NAME>
|
| git push <REMOTE_NAME> --delete <BRANCH_NAME>
|
List of changes that will be merged into the current branch | |
Formatting branch names Branch naming should be clear and use slashes e.g. routing/campground So you can easily search for all branches of that type
| git branch --list "routing/*"
|
Remote tracking You can make any arbitrary branch track o/main, and if you do so, that branch will have the same implied push destination and merge target as main This means you can run git push on a branch named totallyNotMain and have your work pushed to the main branch on the remote! There are two ways to set this property. The first is to checkout a new branch by using a remote branch as the specified ref Another way to set remote tracking on a branch is to simply use the git branch -u option <BRANCH_NAME> is not needed if you are already on the branch you want to use as the remote tracking reference
| git checkout -b <NEW_BRANCH_NAME> <REMOTE_REF>
git checkout -b totallyNotMain origin/main
git branch -u <REMOTE_REF> <BRANCH_NAME>
git branch -u origin/main foo
|
Deleting an ambiguous local branch If you've made a mistake and somehow created a local branch with the same name as a remote branch, you can either rename the local branch or delete it in the local file system
| rm -r .git/refs/heads/<BRANCH_TO_DELETE>
|