惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
IT之家
IT之家
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - Franky
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
J
Java Code Geeks
腾讯CDC
罗磊的独立博客
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
B
Blog
V
Visual Studio Blog
F
Fortinet All Blogs

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
A List of Common Git-branch Operations
2020-09-03 · via jdhao's digital space

Update log
  • 2022-08-20: Add how to remove remote tracking branches that does not exist anymore

This post summarizes some commonly used Git commands related to branch operations.

Create a new branch (locally and remotely)#

Create a branch locally#

We can use the following command to create a new branch

At this time, we are still in original branch. To switch to new branch, use the following command:

git checkout <new_branch>

The two steps can be combined with the following command:

git checkout -b <new_branch>

Create a branch remotely#

To create a remote branch, use the following command:

git push -u origin <local_branch>

The -u option sets a upstream branch of the same name for tracking, i.e., now our local branch corresponds to a remote branch of the same name.

Ref:

Create a git branch from a specific commit#

Sometimes, we may need to fix a flaw in a specific commit, but current work has not been finished. We may create a new branch from that commit, and after that, merge that branch to current branch.

This is how we do this using git:

git branch fix_bug <commit_id>
git checkout fix_bug

<commit_id> is the hash of that commit as shown in the git log output.

The above command can also be simplified using -b for checkout:

git checkout -b fix_bug <commit_id>

After fixing the issue in branch fix_bug, we can then merge the change into the original branch, say, master branch:

git checkout master
git merge fix_bug

Ref:

Show branches#

  • show all branches (local and remote): git branch -a
  • show all local branches: git branch -l
  • show all remote branches: git branch -r

Delete a branch#

  • Delete a local branch: git branch -d <branch_name>.
    Note that we can not delete a local branch when we are on it! We need first to checkout to another branch.
  • Delete a remote branch: git push <remote_name> -d <remote_branch>

If you are using GitHub and delete a remote tracking branch via the GitHub GUI interface, when you run git branch -a locally, they are still shown. To remove the remote tracking branches locally, we can do git remote prune origin.

Ref:

Rename a branch locally and remotely#

Suppose we want to rename the branch foo in local and remote repo to name bar, first we need to rename the local branch:

git branch -m foo bar
git branch --unset-upstream foo

Then we push the bar branch to the remote:

# create a remote branch of the same name and track it.
git push -u origin bar

If the original remote branch foo is the default branch, we need to set other branch as the default. Otherwise, we can not delete remote branch foo. In GitHub, go to Settings --> Branches and change the default branch. After making sure that foo is not the default remote branch, run the following command to delete it from remote:

# suppose that origin is the remote name
git push origin -d foo

Ref:

Create a local branch from a remote branch?#

I have a remote branch and I would like to create a local branch based on it. First, we need to fetch the remote changes:

Then we can create a local branch from the remote branch:

git checkout -b local_branch_name remote_name/remote_branch

If we do not want to change the remote name, we can also also -t:

git checkout -t <name of remote>/<remote branch name>

It will create a local branch of the same name.

Ref:

Find remote tracking branch for local branch#

If we want to find the remote branch that a local branch is tracking, we can use the following command:

We can also use the git remote command to show remote tracking branch:

git remote show <remote name>

Ref: