将更改的文件移动到另一个分支以进行签入

发布于 2024-12-01 21:18:27 字数 101 浏览 0 评论 0原文

这经常发生在我身上:我编写一些代码,去检查我的更改,然后意识到我不在正确的分支中检查这些更改。但是,如果不恢复更改,我就无法切换到另一个分支。有没有办法将更改移动到另一个分支以在那里签入?

This often happens to me: I write some code, go to check in my changes, and then realize I'm not in the proper branch to check in those changes. However I can't switch to another branch without my changes reverting. Is there a way to move changes to another branch to be checked in there?

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

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

发布评论

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

评论(6

白况 2024-12-08 21:18:27

更新的答案

不需要使用 stash 命令。未提交的更改不属于任何分支,因此只需使用 git checkout -b


原始答案

git stash 是你的朋友。

如果您尚未提交,只需运行 git stash 即可。这将保存您的所有更改。

切换到您想要进行更改的分支并运行 git stash pop 。

git stash 有很多用途。这当然是更有用的原因之一。

一个例子:

# work on some code
git stash
git checkout correct-branch
git stash pop

Updated Answer

No need to use stash command. uncommitted changes do not belong to any branch so just use git checkout -b <new-branch>


Orginal Answer

git stash is your friend.

If you have not made the commit yet, just run git stash. This will save away all of your changes.

Switch to the branch you want the changes on and run git stash pop.

There are lots of uses for git stash. This is certainly one of the more useful reasons.

An example:

# work on some code
git stash
git checkout correct-branch
git stash pop
北座城市 2024-12-08 21:18:27

如果您尚未提交更改,只需使用 git checkout 移动到新分支,然后正常提交它们 - 对文件的更改不会绑定到特定分支直到你提交它们。

如果您已经已经提交了更改:

  1. 输入git log并记住您要移动的提交的 SHA。
  2. 查看您要将提交移至的分支。
  3. 输入 gitcherry-pick SHA 替换上面的 SHA。
  4. 切换回原来的分支。
  5. 使用 git reset HEAD~1 来重置错误分支提交之前的状态。

Cherry-pick 接受给定的提交并将其应用到当前签出的头,从而允许您将提交复制到新分支。

If you haven't already committed your changes, just use git checkout to move to the new branch and then commit them normally - changes to files are not tied to a particular branch until you commit them.

If you have already committed your changes:

  1. Type git log and remember the SHA of the commit you want to move.
  2. Check out the branch you want to move the commit to.
  3. Type git cherry-pick SHA substituting the SHA from above.
  4. Switch back to your original branch.
  5. Use git reset HEAD~1 to reset back before your wrong-branch commit.

cherry-pick takes a given commit and applies it to the currently checked-out head, thus allowing you to copy the commit over to a new branch.

泛泛之交 2024-12-08 21:18:27

可悲的是,这种情况也经常发生在我身上,如果我在 git commit 之前意识到自己的错误,我会使用 git stash ,否则使用 gitcherry-pick ,这两个命令在其他答案中都得到了很好的解释,

我想为 git checkout targetBranch 添加说明:如果 targetBranch 与您的历史记录相同,则此命令只会保留您的工作目录和暂存快照当前分支

如果您尚未提交更改,只需使用 git checkout
移动到新分支,然后正常提交

@Amber的说法不假,当你移动到newBranch时,git checkout -b newBranch,创建一个新指针,它指向与当前分支完全相同的提交。
事实上,如果您碰巧有另一个分支与当前分支共享历史记录(两者都指向同一个提交),您可以通过 git checkout targetBranch 来“移动您的更改”

但是,通常不同的分支意味着不同的历史记录,Git 不允许你在这些具有脏工作目录或暂存区域的分支之间切换。在这种情况下,您可以执行 git checkout -f targetBranch (干净和一次性更改)或 git stage + git checkout targetBranch (干净和 < strong>保存更改),只需运行git checkout targetBranch就会给出错误:

错误:您对以下文件的本地更改将被覆盖
通过结帐:
...
请在切换分支之前提交您的更改或隐藏它们。正在中止

Sadly this happens to me quite regularly as well and I use git stash if I realized my mistake before git commit and use git cherry-pick otherwise, both commands are explained pretty well in other answers

I want to add a clarification for git checkout targetBranch: this command will only preserve your working directory and staged snapshot if targetBranch has the same history as your current branch

If you haven't already committed your changes, just use git checkout
to move to the new branch and then commit them normally

@Amber's statement is not false, when you move to a newBranch,git checkout -b newBranch, a new pointer is created and it is pointing to the exact same commit as your current branch.
In fact, if you happened to have an another branch that shares history with your current branch (both point at the same commit) you can "move your changes" by git checkout targetBranch

However, usually different branches means different history, and Git will not allow you to switch between these branches with a dirty working directory or staging area. in which case you can either do git checkout -f targetBranch (clean and throwaway changes) or git stage + git checkout targetBranch (clean and save changes), simply running git checkout targetBranch will give an error:

error: Your local changes to the following files would be overwritten
by checkout:
...
Please commit your changes or stash them before you switch branches. Aborting

爱*していゐ 2024-12-08 21:18:27

软 git 重置会将提交的更改放回到索引中。接下来,检查您打算提交的分支。然后使用新的提交消息git commit

  1. git reset --soft

  2. git checkout;

  3. git commit -m “提交消息在此处”

来自 git 文档

git reset [] [] 这种形式重置当前分支头
并可能更新索引(将其重置为树
的 ) 和工作树取决于 .如果是
省略,默认为--mixed。必须是以下之一:

--soft 根本不触及索引文件或工作树(但将头重置为 ,就像所有模式一样)。这使得所有
您更改的文件“要提交的更改”,如 git status 所示
它。

A soft git reset will put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commit with a new commit message.

  1. git reset --soft <commit>

  2. git checkout <branch>

  3. git commit -m "Commit message goes here"

From git docs:

git reset [<mode>] [<commit>] This form resets the current branch head
to and possibly updates the index (resetting it to the tree
of ) and the working tree depending on . If is
omitted, defaults to --mixed. The must be one of the following:

--soft Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all
your changed files "Changes to be committed", as git status would put
it.

荒人说梦 2024-12-08 21:18:27

如果您创建了新文件,您可以执行以下操作:

git checkout main
git checkout -b branch-b
git checkout branch-a :rel/path/to/yourchangedfiles
git commit -m "w"
git checkout branch-a

git checkout main :rel/path/to/yourchangedfiles

# if this happens:
error: pathspec ':rel/path/to/yourchangedfiles' did not match any file(s) known to git

# then just rm the folder
trash-put rel/path/to/yourchangedfiles

If you created new files you can do this:

git checkout main
git checkout -b branch-b
git checkout branch-a :rel/path/to/yourchangedfiles
git commit -m "w"
git checkout branch-a

git checkout main :rel/path/to/yourchangedfiles

# if this happens:
error: pathspec ':rel/path/to/yourchangedfiles' did not match any file(s) known to git

# then just rm the folder
trash-put rel/path/to/yourchangedfiles
画中仙 2024-12-08 21:18:27

更现代的答案

由于 checkout 可能是 Git 最不直观的瓷器命令,并且负责太多的功能,因此请使用更容易记住、更直观的 switch (在2019 年作为 Git 的一部分2.23):

git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>

如果您的分支彼此不同步,第一个 git switch 命令会给您一个如下错误:

error: Your local changes to the following files would be overwritten by checkout:
        file1.html
        file2.js
        file3.php
Please commit your changes or stash them before you switch branches.
Aborting

此时您将不得不使用替代的 git stash 方法:

git stash save
git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>
git stash pop

不要忘记将 NEW BRANCH 替换为您要切换到的分支,并将 ORIGINAL_BRANCH 替换为您原来所在的分支(通常 <代码>主控或main)。要查看存储库所有分支的列表,请运行 gitbranch -l 。

A more modern answer

Since checkout is probably Git's least intuitive porcelain command and responsible for way too much functionality, use the easier-to-remember, more intuitive switch instead (introduced in 2019 as part of Git 2.23):

git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>

If your branches are not in sync with each other the first git switch command will give you an error like this:

error: Your local changes to the following files would be overwritten by checkout:
        file1.html
        file2.js
        file3.php
Please commit your changes or stash them before you switch branches.
Aborting

At this point you will have to use the alternative git stash method:

git stash save
git switch <NEW BRANCH> # Add -c to create the branch if it doesn't exist
git add <FILES TO COMMIT>
git commit -m "Commit changed files to new branch"
git switch <ORIGINAL BRANCH>
git stash pop

Don't forget to replace NEW BRANCH with the branch you want to switch to and ORIGINAL_BRANCH with the branch you were originally in (usually master or main). To see a list of all your repository's branches, run git branch -l.

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