如何在不撤消所有更改的情况下拉取 git 分支?
How to pull git branch without undoing all my changes?
我有一个 git 分支机构在工作,但我把它搞砸了。我做了一堆本地更改,我试图将这些更改和此提交放入 PR,但如果不从分支更新我就不能这样做,因为它们以某种方式不同步。所以每当我 运行 git status
我得到
Your branch and 'origin/myChanges' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
我用 git 拉动尝试的一切都只是吹走我的所有更改,但我需要在允许将提交发送到 PR 之前从分支更新。我也尝试过 运行 强制推送以使分支与我的分支相同(这只是我为此功能创建的一个小分支,所以风险很小)。我不知道如何 git 以这样一种方式拉动我的更改不会被破坏和吹走,我想保留本地的所有内容并且我不关心分支上的任何内容。
git stash
在这种情况下可能会有帮助
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
仔细查看 git stash
的文档。如果我理解正确,您首先将本地更改隐藏起来以获得干净的状态。然后你可以 git pull 合并分支的远程更改。然后用 git stash pop
再次添加 stash
Remove a single stashed state from the stash list and apply it on top of the current working tree state.
在高层次上,你在这种情况下的步骤应该是这样的:
- Stash 您本地未提交的更改。
- 运行
git fetch
以确保您的本地克隆知道远程上的引用。
- Rebase 您的本地分支到
origin/myChanges
。
- Fix up any merge conflicts 发生了。
- 弹出你的 stash.
- 修复存储弹出时发生的任何合并冲突。
- Force-push with lease 您对
origin/myChanges
的更改。如果你有多个开发人员在同一个分支上工作(你真的 really 不应该这样做,因为它会导致这些情况),告诉他们你已经强制推送了。
- 继续进行更改,然后在准备就绪时提交并推送。
我有一个 git 分支机构在工作,但我把它搞砸了。我做了一堆本地更改,我试图将这些更改和此提交放入 PR,但如果不从分支更新我就不能这样做,因为它们以某种方式不同步。所以每当我 运行 git status
我得到
Your branch and 'origin/myChanges' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
我用 git 拉动尝试的一切都只是吹走我的所有更改,但我需要在允许将提交发送到 PR 之前从分支更新。我也尝试过 运行 强制推送以使分支与我的分支相同(这只是我为此功能创建的一个小分支,所以风险很小)。我不知道如何 git 以这样一种方式拉动我的更改不会被破坏和吹走,我想保留本地的所有内容并且我不关心分支上的任何内容。
git stash
在这种情况下可能会有帮助
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
仔细查看 git stash
的文档。如果我理解正确,您首先将本地更改隐藏起来以获得干净的状态。然后你可以 git pull 合并分支的远程更改。然后用 git stash pop
Remove a single stashed state from the stash list and apply it on top of the current working tree state.
在高层次上,你在这种情况下的步骤应该是这样的:
- Stash 您本地未提交的更改。
- 运行
git fetch
以确保您的本地克隆知道远程上的引用。 - Rebase 您的本地分支到
origin/myChanges
。 - Fix up any merge conflicts 发生了。
- 弹出你的 stash.
- 修复存储弹出时发生的任何合并冲突。
- Force-push with lease 您对
origin/myChanges
的更改。如果你有多个开发人员在同一个分支上工作(你真的 really 不应该这样做,因为它会导致这些情况),告诉他们你已经强制推送了。 - 继续进行更改,然后在准备就绪时提交并推送。