[英]How do I fix my local repo so I can save work from commits that haven’t been pushed to the master git repo?
I could probably find a way to do the following, but I'd prefer to do this the “correct” way.我可能会找到一种方法来执行以下操作,但我更愿意以“正确”的方式执行此操作。
Here is the output of branch and status:这是分支和状态的output:
docker@noakland:~/stacks$ git branch
* master
docker@noakland:~/stacks$
docker@noakland:~/stacks$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
docker@noakland:~/stacks$
git tree = git log --graph --decorate --pretty=format:'%C(bold yellow)%h %Cred%cr %C(bold blue)(%an)%C(bold cyan)%d%Creset %s' --abbrev-commit --all git 树 = git 日志 --graph --decorate --pretty=format:'%C(加粗黄色)%h %Cred%cr %C(加粗蓝色)(%an)%C(加粗青色)%d%Creset %s' --abbrev-commit --all
git tree yields: git 树产生:
* f3d7b51 65 minutes ago (Mike) (HEAD -> master) feeder
* 12778e1 79 minutes ago (Mike) feeder test
| * d829803 3 days ago (Mike) (origin/master, origin/HEAD) tmp
|/
* 7365aca 5 days ago (Mike) restore snapshot image
Commits 12778e1
and f3d7b51
have not been pushed to the central repo.提交
12778e1
和f3d7b51
尚未推送到中央仓库。
I want to update to d829803
and apply the changes in commits 12778e1
and f3d7b51
as a new commit appearing on top of d829803
.我想更新到
d829803
并将提交12778e1
和f3d7b51
中的更改应用为出现在d829803
之上的新提交。 There are only 3 files I'm interested in keeping, so I'll undo/revert changes to those files from d829803
.我只需要保留 3 个文件,因此我将从
d829803
撤消/恢复对这些文件的更改。
I think I want my tree to look something like this (Though not sure about the branch names)我想我希望我的树看起来像这样(虽然不确定分支名称)
* abcdef9 10 seconds ago (Mike) (HEAD -> master) feeder
* d829803 3 days ago (Mike) (origin/master, origin/HEAD) tmp
* 7365aca 5 days ago (Mike) restore snapshot image
So how can I update to d829803
, cherry-pick the changes from the un-pushed commits, manually revert changes to file abc.txt
@ d829803
and then get rid of 12778e1
and f3d7b51
so they won't get pushed to the master repo, nor exist in the local repo?那么我如何更新到
d829803
,从未推送的提交中挑选更改,手动恢复对文件abc.txt
@ d829803
的更改,然后删除12778e1
和f3d7b51
,这样它们就不会被推送到主仓库,也不存在于本地仓库中?
With hg this would be simple:使用 hg 这很简单:
hg update d829803
hg revert -r 12778e1 —-all
hg revert -r f3d7b51 —-all
hg revert -r d829803 abc.txt
hg commit -m “manually cherry picked”
hg strip 12778e1
hg strip f3d7b51
hg push
So, I'll give you the way I'd do it.所以,我会给你我会做的方式。 The operation you are willing to make is called a rebase, and it's very usefull in situations you need to rewrite history, specially when you want a branch to have it's origins changed.
您愿意进行的操作称为变基,它在您需要重写历史记录的情况下非常有用,特别是当您希望分支更改其起源时。
For your operation, I'll give you a way to do that, but keep in mind that conflicts may happen on the way.对于您的操作,我会为您提供一种方法,但请记住,途中可能会发生冲突。 More on that at the end.
最后还有更多。
I'll also assume you won't upload the new states of those branches until the end.我还假设您直到最后都不会上传这些分支的新状态。 That's helpfull because it's much simpler to do a git reset and start over than to fix the upstream repo.
这很有帮助,因为执行 git 重置并重新开始比修复上游存储库要简单得多。
master
pointing at f3d7b51
master
指向f3d7b51
origin/master
pointing at d829803
d829803
的origin/master
d829803
serving as the root where both diverged. d829803
作为两者分歧的根。 It's important to know the new root when moving branches here and there. You wish, as I understood, to fuse f3d7b51
and 12778e1
so they appear as a single commit.据我了解,您希望融合
f3d7b51
和12778e1
以便它们显示为单个提交。 This operation is called a squash, inside the rebase.此操作在变基内部称为 squash。 We'll this info in a bit.
我们稍后会提供这些信息。
To do the rebase, I'll recomend it's interactive version.要进行变基,我会推荐它的交互式版本。 At the terminal, do:
在终端,执行:
git checkout master
git rebase -i origin/master #this is commit d829803
Your terminal will open your text editor or ask you which one you want to use.您的终端将打开您的文本编辑器或询问您要使用哪一个。 This is a simple text editing we will be doing, so pick whatever you're confortable with.
这是我们将要进行的简单文本编辑,因此请选择您喜欢的任何内容。
Once the editor opens, It should show you a sequence of commits.编辑器打开后,它应该会显示一系列提交。 proposed by the rebase operation.
由 rebase 操作提出。 If nothing is altered, it's going to apply the changes on top of
d829803
, doing a new commit for each one.如果没有任何更改,它将在
d829803
之上应用更改,为每个更改执行一次新提交。 You really are moving the commits around, changing their parent commit with this.你真的在移动提交,用这个改变他们的父提交。
Remember the squash?还记得南瓜吗? If my assumption is correct, you need to change the "command" beside the commit hash in this editing operation, replacing "pick" with "squash".
如果我的假设是正确的,您需要在此编辑操作中更改提交 hash 旁边的“命令”,将“pick”替换为“squash”。 This fuses both commits, as if they where a single one.
这融合了两个提交,就好像它们只有一个。 The order of changes is also respected (if you don't move lines up and down)
更改的顺序也受到尊重(如果您不上下移动线条)
So, your rebase should initially look like this:因此,您的 rebase 最初应该如下所示:
pick 12778e1 feeder test
pick f3d7b51 feeder
# operation hash commit message
You can, then fuse the commits and even alter the message:您可以,然后融合提交,甚至更改消息:
pick 12778e1 implement feeder
squash f3d7b51 feeder
This is what is going to happen when you save the file and exit editing:这是保存文件并退出编辑时会发生的情况:
12778e1
and then f3d7b51
, at this order, creating a single commit with a different hash and the message implement feeder
12778e1
然后f3d7b51
,按此顺序,使用不同的 hash 和消息implement feeder
纸器创建单个提交* ab46d2a 65 minutes ago (Mike) (HEAD -> master) implement feeder
|
* d829803 3 days ago (Mike) (origin/master, origin/HEAD) tmp
|
* 7365aca 5 days ago (Mike) restore snapshot image
If you wish to remove 12778e1
, just remove it's line when editing.如果您想删除
12778e1
,只需在编辑时删除它的行。
If you have a conflict, the operations will pause, git will warn you about the conflicted files.如果您有冲突,操作将暂停,git 将警告您有关冲突的文件。
In this scenario you can resolve them or abort.在这种情况下,您可以解决它们或中止。 To abort simply use
git rebase --abord
.要中止,只需使用
git rebase --abord
。
Solving is merelly going to those files, merging them to get the changes you need from the conflicted commits, executing a git add and git commit.解决只是去那些文件,合并它们以从冲突的提交中获取您需要的更改,执行 git add 和 git 提交。 To continue the rebase, use
git rebase --continue
要继续 rebase,请使用
git rebase --continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.