简体   繁体   中英

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. 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?

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. 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?

There are a few options here.

1) You don't have to do anything.

As the documentation says :

git pull is shorthand for git fetch followed by 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:

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.)

2) You can use reflog

You run get log --oneline you'll get a history of commits for the current branch. There's a similar command for seeing commits that you've recently checked out: 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 checkout -b <branch-name> <commit-hash>

(Note that reflog typically holds onto your history for about 90 days)

3) You can "backup" your branch.

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:

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 .

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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