2018-02-05 02:27:07 +00:00
|
|
|
# Branching
|
2019-11-23 16:05:04 +00:00
|
|
|
```bash
|
2023-04-01 19:48:24 +00:00
|
|
|
# Create a new branch. Stay in the current branch.
|
2018-02-05 02:27:07 +00:00
|
|
|
git branch <branchName>
|
|
|
|
|
2019-02-19 19:22:04 +00:00
|
|
|
# Create a new branch at a specific commit, and switch to it.
|
|
|
|
git checkout -b <branchName> 3f4308df1e0b7f663d851108128e8e53deb1b5fb
|
|
|
|
|
2018-02-05 02:27:07 +00:00
|
|
|
# Switch to an existing branch.
|
|
|
|
git checkout <branchName>
|
|
|
|
git checkout master
|
|
|
|
|
2021-08-07 20:34:42 +00:00
|
|
|
# Switch to the previous branch.
|
|
|
|
git switch -
|
|
|
|
git switch @{-1}
|
|
|
|
# For use in PowerShell.
|
|
|
|
git switch '@{-1}'
|
|
|
|
git checkout -
|
|
|
|
git checkout @{-1}
|
|
|
|
|
2018-02-05 02:27:07 +00:00
|
|
|
# Checkout and switch to a new branch.
|
|
|
|
git checkout -b my_new_branch
|
2023-05-05 09:46:15 +00:00
|
|
|
git switch -c my_new_branch
|
2018-02-05 02:27:07 +00:00
|
|
|
|
|
|
|
# Push the new branch to a remote.
|
|
|
|
git push -u origin my_new_branch
|
|
|
|
|
|
|
|
# Alternative way to push a branch to a remote, without permanently setting the upstream.
|
|
|
|
git push origin my_new_branch
|
|
|
|
|
2023-01-08 03:29:54 +00:00
|
|
|
# Push to a remote, but only up to a certain commit. This helps if the full push is too large.
|
|
|
|
git push origin c5831166a7189823919923c73a0e6346cbee71d6:main
|
|
|
|
# If the branch hasn't been created on the remote yet.
|
|
|
|
git push origin c5831166a7189823919923c73a0e6346cbee71d6:refs/heads/main
|
|
|
|
|
2018-02-05 02:27:07 +00:00
|
|
|
# Checkout a remote branch.
|
|
|
|
git checkout --track origin/<branchName>
|
|
|
|
|
|
|
|
# Merge changes from master into a branch.
|
|
|
|
git checkout <branchName>
|
|
|
|
git merge master
|
|
|
|
|
|
|
|
# Abort a merge (such as if there's conflicts that need to be resolved differently).
|
|
|
|
git merge --abort
|
|
|
|
|
2018-07-24 17:58:41 +00:00
|
|
|
# Merge two local branches, without fast-forwarding, with default editor opening to modify the message.
|
|
|
|
git merge <branchName> --no-ff --edit
|
|
|
|
|
2018-02-05 02:27:07 +00:00
|
|
|
# Merge two local branches, without fast-forwarding, and including messages for the last X (20 here) commits.
|
|
|
|
# According to Linus Torvalds, the better way to do merges.
|
|
|
|
git merge <branchName> --log=20 --no-ff
|
2018-02-07 16:56:43 +00:00
|
|
|
|
2019-02-19 19:14:18 +00:00
|
|
|
# While on a different branch, update the master branch from remote.
|
2019-02-19 19:22:04 +00:00
|
|
|
# See https://stackoverflow.com/a/42902058/11912
|
2019-02-19 19:14:18 +00:00
|
|
|
git fetch origin master:master
|
|
|
|
|
2019-02-19 19:22:04 +00:00
|
|
|
# Put recent local commits in master into a new branch.
|
2018-02-07 16:56:43 +00:00
|
|
|
# X is the number of commits to rollback.
|
|
|
|
git branch <branchName>
|
|
|
|
git reset --hard HEAD~X
|
|
|
|
git checkout <branchName>
|
2019-05-29 13:53:09 +00:00
|
|
|
|
2021-06-21 20:37:25 +00:00
|
|
|
# Add a specific commit on top of the current branch.
|
2022-09-14 18:23:57 +00:00
|
|
|
git cherry-pick <commit-id>
|
2021-07-11 17:03:40 +00:00
|
|
|
|
2022-10-28 18:23:56 +00:00
|
|
|
# Add a specific commit on top of the current branch, but don't automatically commit it. Allows you to manually tweak what's getting pulled in.
|
|
|
|
git cherry-pick -n <commitId>
|
|
|
|
|
2021-07-11 17:03:40 +00:00
|
|
|
# Rename the current branch
|
|
|
|
git branch -m <newBranchName>
|
2021-08-07 20:34:42 +00:00
|
|
|
|
2023-04-01 19:48:24 +00:00
|
|
|
# Show all local branches with last commit message.
|
|
|
|
git branch -v
|
|
|
|
|
|
|
|
# Above, plus corresponding remote branch name. Also includes worktree path, if applicable.
|
|
|
|
git branch -vv
|
|
|
|
```
|
|
|
|
|
|
|
|
## Navigating
|
|
|
|
```bash
|
2021-08-07 20:34:42 +00:00
|
|
|
# Get the name of the previous branch.
|
|
|
|
git rev-parse --symbolic-full-name @{-1}
|
|
|
|
# For PowerShell.
|
|
|
|
git rev-parse --symbolic-full-name '@{-1}'
|
2019-05-29 13:53:09 +00:00
|
|
|
```
|
2022-07-02 23:03:13 +00:00
|
|
|
|
2022-09-14 18:23:57 +00:00
|
|
|
## Management
|
|
|
|
```bash
|
2023-04-01 19:48:24 +00:00
|
|
|
# Delete a branch.
|
|
|
|
git branch -d my_new_branch
|
2022-09-14 18:23:57 +00:00
|
|
|
|
2023-04-01 19:48:24 +00:00
|
|
|
# Delete a branch on the remote.
|
|
|
|
git push origin :my_new_branch
|
2022-09-14 18:23:57 +00:00
|
|
|
|
|
|
|
# Do a dry run of pruning local branches that no longer exist on a remote.
|
|
|
|
git remote prune origin --dry-run
|
|
|
|
```
|