git:将当前分支中当前未提交的正在进行的工作提交到新分支

发布于 2024-12-10 15:33:45 字数 236 浏览 0 评论 0原文

有时我会发现自己处于以下情况: 我正在分支(比如主分支)上工作,并且正在编辑一些文件,但是当需要提交时,我意识到我更愿意提交到一个新分支(比如实验分支)。换句话说,旧分支(主分支)必须保持不变,并且应该创建一个新分支,如下所示:

.. -- master -- new commit

最简单的方法是什么?

目前,我提交旧分支,然后创建新分支,最后重置旧分支。但这真的很难看。

Occasionally I find my self in the following situation:
I am working on branch (say master) and I am editing some files, but when it comes the time for a commit I realize that I would prefer to commit to a new branch (say experimental). In other words, the old branch (master) must remain intact and a new branch should be created that will look like:

.. -- master -- new commit

What is the easiest way to do that?

Currently, I commit to old branch, then create the new branch and finally reset the old branch. But that's really ugly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月流歌 2024-12-17 15:33:45
git checkout -b branch       # create new branch out of current head
git add <files>              # the changes you had done in your working directory will be carried over
git commit -m "message"      # commit!
git checkout -b branch       # create new branch out of current head
git add <files>              # the changes you had done in your working directory will be carried over
git commit -m "message"      # commit!
吖咩 2024-12-17 15:33:45

如果您先创建新分支会稍微容易一些:

$ git checkout -b new_branch
$ git commit

It's slightly easier if you create the new branch first:

$ git checkout -b new_branch
$ git commit
不甘平庸 2024-12-17 15:33:45

另一种可能性是您继续将它们提交到 master 分支,然后从新的 HEAD 中切断另一个分支(例如 feature) 。完成此操作后,您可以将 master 重置到提交之前的位置。

Another possibility is for you to go ahead and commit them to the master branch, then cut off another branch from the new HEAD (say feature). Once this is done, you can git reset master to the position before the commits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文