简体   繁体   English

如何在Git中不预先合并远程跟踪提交的情况下创建新分支?

[英]How to create a new branch without pre-merge remote-tracking commits in Git?

Whenever I create a new branch (git checkout -b <newBranchName>) , I use to get many commit logs: 每当我创建一个新分支(git checkout -b <newBranchName>) ,我都会使用许多提交日志:

Merge remote-tracking branch 'upstream/master' into develop    e53c044

Merge remote-tracking branch 'upstream/develop' into develop   feda328

Merge remote-tracking branch 'upstream/develop' into develop   4bbd301

Merge remote-tracking branch 'upstream/develop' into develop   4dfcfcb

Merge remote-tracking branch 'upstream/develop' into develop   bfaccfb

Merge remote-tracking branch 'upstream/develop' into develop   754c0ab

Merge remote-tracking branch 'upstream/develop' into develop   b0454b0

Merge remote-tracking branch 'upstream/develop' into develop   f96182f

图片

How can I create a branch without such logs? 如何创建没有此类日志的分支?

Maybe orphan parameter of git checkout could help you: 也许git checkout orphan参数可以帮助您:

git checkout --orphan NEW_BRANCH

This will create a new branch named NEW_BRANCH with zero commits on it. 这将创建一个名为NEW_BRANCH的新分支,其上的提交为零。 But now all files stay in cache area, you could use "git rm --cached ..." to unstage: 但是现在所有文件都保留在缓存区域中,您可以使用“ git rm --cached ...”取消登台:

git rm -r --cached .

-r here could unstage files recursively. -r在这里可以递归取消暂存文件。

The commits for the new created branches are based on the parent branch you created from. 新创建的分支的提交基于您从其创建的父分支。

Such as if you create a new branch new from develop branch as below graph: 这样,如果你创建一个新的分支作为newdevelop分支,如下图:

A---B---C   developer
         \
            new

When you show the logs for new branch, you will find the commits A , B and C are showed. 当显示new分支的日志时,您会发现显示了提交ABC

If you want to create the new branch without showing the parent commits , you can create an orphan branch (execute on develop branch): 如果要创建new分支而不显示父提交 ,则可以创建一个孤立分支(在develop分支上执行):

git checkout --orphan new

Then there will have no commits on new branch. 这样就不会在new分支上提交任何内容。 the commit history will as below: 提交历史如下:

A---B---C   developer

            new

If you want to create the new branch with only part of the commits for it's parent branch develop , you can use: 如果你想创建new ,只有在提交的部分分支为它的父分支develop ,你可以使用:

git checkout -b new <last commit you want to contain in new branch>

Such as if you want to contain only commits A and B in new branch, you can use the command git checkout -b new <commit id for B> , then the commit history will be: 例如,如果您只想在new分支中包含提交AB ,则可以使用命令git checkout -b new <commit id for B> ,那么提交历史将为:

A---B---C   developer
     \
       new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建git Remote-Tracking分支 - How to create git Remote-Tracking Branch git log,只提交在分支预合并时发生的提交 - git log, only commits that happened on branch pre-merge Git:如何获取单个远程分支并自动创建远程跟踪分支? - Git: How to fetch a single remote branch and create a remote-tracking branch of it automatically? Git:如何将“合并远程跟踪分支”改造并压缩到后来的提交中? - Git: How to rebase and squash “Merge remote-tracking branch” to the later commit? 如何合并远程跟踪none github分支? - How to merge remote-tracking a none github branch? 如何将远程跟踪分支“ caffe / master”合并到HEAD中? - how to Merge remote-tracking branch 'caffe/master' into HEAD ? “合并远程跟踪分支&#39;xxx / master&#39;”是否会给Pull Request带来麻烦? - Are “Merge remote-tracking branch 'xxx/master'” commits a trouble for Pull Request? 通过一个Git远程跟踪分支的名字如何找到本地分支跟踪它? - By given name of a Git remote-tracking branch how to find which local branch tracks it? Git - 推送到远程存储库中的远程跟踪分支 - Git - push to a remote-tracking branch in the remote repository 当使用git删除相应的本地分支时,如何自动删除远程跟踪分支 - How to automatically remove remote-tracking branch when the corresponding local branch is removed using git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM