简体   繁体   English

git pull 后保留本地更改以重做?

[英]Preserve local changes to redo after git pull?

My mistake was to overwrite commits on my local repository that were already on my remote.我的错误是覆盖了我的远程存储库中已经存在的提交。 I have like 3 commits that are not yet pushed to remote.我有 3 个尚未推送到远程的提交。 Is there a way I can preserve these changes, so that when I do git pull, I can parse the commits and go back to normal?有没有办法可以保留这些更改,以便在执行 git pull 时可以解析提交并恢复正常?

I'd need the commands step by step because I'm afraid of doing something wrong.我需要一步一步的命令,因为我害怕做错事。

I have like 3 commits that are not yet pushed to remote.我有 3 个尚未推送到远程的提交。 Is there a way I can preserve these changes, so that when I do git pull, I can parse the commits and go back to normal?有没有办法可以保留这些更改,以便在执行 git pull 时可以解析提交并恢复正常?

There are a few options here.这里有几个选项。

1) You don't have to do anything. 1) 你什么都不用做。

As the documentation says :正如文档所说

git pull is shorthand for git fetch followed by git merge FETCH_HEAD git pullgit fetch后跟git merge FETCH_HEAD

So, if your current history looks like this:因此,如果您当前的历史记录如下所示:

12bcdef345 my third commit
8765fedcba my second commit
abcdef1234 my first commit

Then after a git pull your history would look something like this:然后在git pull您的历史记录将如下所示:

98aabbcc76 merge from server
12bcdef345 my third commit
8765fedcba my second commit
abcdef1234 my first commit

So if you want to get back to where you were before the pull, you can reset to one of your original commits like so:因此,如果您想回到拉取之前的位置,您可以像这样重置为您的原始提交之一:

git reset --hard 12bcdef345

(You might want to make a quick copy of your history, using git log --oneline , in case you reset too far back or change your mind.) (您可能需要使用git log --oneline快速复制您的历史git log --oneline ,以防您重置太远或改变主意。)

2) You can use reflog 2)您可以使用reflog

You run get log --oneline you'll get a history of commits for the current branch.您运行get log --oneline您将获得当前分支的提交历史记录。 There's a similar command for seeing commits that you've recently checked out: git reflog --oneline .有一个类似的命令可以查看您最近签出的提交: git reflog --oneline

Even if you delete your current branch, you can use git reflog to find commits that you had checked out, then re-create branches from old commits by running:即使您删除了当前分支,您也可以使用git reflog查找已签出的提交,然后通过运行以下命令从旧提交重新创建分支:

git checkout -b <branch-name> <commit-hash>

(Note that reflog typically holds onto your history for about 90 days) (请注意,reflog 通常会保留您的历史记录约 90 天)

3) You can "backup" your branch. 3)您可以“备份”您的分支。

If you're the nervous sort and you really want to be sure you don't lose your commits, or you're going to do something complicated and you don't want to have to search through reflog looking for your old commit, you can quickly create a backup branch using:如果你是那种紧张的人并且你真的想确保你不会丢失你的提交,或者你要做一些复杂的事情并且你不想通过reflog搜索你的旧提交,你可以使用以下方法快速创建备份分支:

git branch <backup-branch-name> <HEAD-or-branch-to-backup>

If you want, you can verify that it worked by checking out the backup branch and viewing it's history.如果需要,您可以通过检查备份分支并查看它的历史记录来验证它是否有效。 Then, with the original branch checked out, run your git pull .然后,签出原始分支后,运行git pull

If at some point in the future you decide that you want to restore your original branch to where it was when you backed it up, simply reset it to the backup branch like so:如果在将来的某个时候您决定要将原始分支恢复到备份时的位置,只需将其重置为备份分支,如下所示:

git reset --hard <backup-branch-name>

NOTE笔记

This will work if your local changes are not committed yet.如果您的本地更改尚未提交,这将起作用。 It is unclear to me if you have already committed your local changes.我不清楚您是否已经提交了本地更改。 Before running the commands please leave a comment answering my question.在运行命令之前,请留下评论来回答我的问题。

If I understand correctly you need to pull data from remote while keeping the local changes you have at the moment.如果我理解正确,您需要从远程提取数据,同时保留您目前所做的本地更改。 If this is the case, here is a simple solution.如果是这种情况,这里有一个简单的解决方案。 Run these commands:运行这些命令:

git stash
git pull
git stash apply

Now this will first stash your current changes and allow you to pull from remote.现在这将首先存储您当前的更改并允许您从远程拉取。 When you will run git stash apply there may be some conflicts.当您运行git stash apply ,可能会出现一些冲突。 Again I'm not sure what you mean by I can parse the commits and go back to normal , but if you have resolves conflicts before you should be just fine doing it.同样,我不确定你的意思是我可以解析提交并恢复正常,但是如果您在解决冲突之前应该没问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM