简体   繁体   English

如何修复我的本地存储库,以便我可以保存尚未推送到主 git 存储库的提交的工作?

[英]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.提交12778e1f3d7b51尚未推送到中央仓库。

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并将提交12778e1f3d7b51中的更改应用为出现在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的更改,然后删除12778e1f3d7b51 ,这样它们就不会被推送到主仓库,也不存在于本地仓库中?

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 重置并重新开始比修复上游存储库要简单得多。

  1. First, understanding the state.首先,了解 state。 Your initial state is:您最初的 state 是:
  • master pointing at f3d7b51 master指向f3d7b51
  • origin/master pointing at d829803指向d829803origin/master
  • d829803 serving as the root where both diverged. d829803作为两者分歧的根。 It's important to know the new root when moving branches here and there.在这里和那里移动分支时,了解新根很重要。
  1. You wish, as I understood, to fuse f3d7b51 and 12778e1 so they appear as a single commit.据我了解,您希望融合f3d7b5112778e1以便它们显示为单个提交。 This operation is called a squash, inside the rebase.此操作在变基内部称为 squash。 We'll this info in a bit.我们稍后会提供这些信息。

  2. 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:这是保存文件并退出编辑时会发生的情况:

  • git will rewind and go to origin/master (aka d829803) git 将倒带和 go 到原点/母版(又名 d829803)
  • the differente be presented at the editor, for you to edit the sequence, change message, squash, or even remove commits from history.在编辑器上显示不同的内容,以便您编辑序列、更改消息、压缩,甚至从历史记录中删除提交。
  • when you save and exit, git will apply 12778e1 and then f3d7b51 , at this order, creating a single commit with a different hash and the message implement feeder当您保存并退出时, git 将应用12778e1然后f3d7b51 ,按此顺序,使用不同的 hash 和消息implement feeder纸器创建单个提交
  • Your tree will look like this:您的树将如下所示:
* 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 ,只需在编辑时删除它的行。

Conflicts冲突

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

References参考

暂无
暂无

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

相关问题 如何撤消尚未推送的提交? - How do I undo commits that haven't been pushed yet? 我如何将提交推送到已经从本地存储库推送到 git 的 heroku? - How could i push a commit to heroku that has already been pushed to git from local repo? 如何删除 git repo 中的所有文件并从本地 git repo 更新/推送? - How can I remove all files in my git repo and update/push from my local git repo? 如何查看尚未推送的提交列表? - How do I view the list of commits that haven't been pushed yet? 如果我将工作合并到主数据库但没有推送后,有人将新提交提交到git存储库的主数据库,该怎么办? - What shall I do if someone pushed new commits to master of git repository after I merged my work to master but didn't push? 如何在执行提交时确保git config文件被推送到仓库? - How can I make sure git config file gets pushed to repo when doing commits? 如果还没有与我的本地存储库链接的远程存储库,我如何 go 到克隆的存储库并签入 git bash? - How do I go to the cloned repo and check in git bash if there isn't already a remote repo linked with my local repo? GIT:如何压缩已被推送到远程仓库的几个提交? - GIT: how to squash several commits that have been pushed to a remote repo? 如何将 git fork 的更改合并到我的 repo 的 master 中? - How do I merge changes of a git fork into master of my repo? 我本地分支上已删除的提交是否也通过git推送到我的远程仓库中? - Do deleted commits on my local branch also get pushed to my remote repo with git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM