简体   繁体   中英

Where are my changes committed to git today?

I have very little knowledge on git. I don't want to think bad. Today I began to push my changes to git and then I wanted to resolve conflicts. So, what I did for today is :

git pull origin master               # 1
git commit -m "message"              # 2
git push origin master               # 3

git reset --hard origin/master       # 4  resulted in fatal error.

git add *                            # 5
git commit -a -m "my second commit"  # 6
git fetch origin master              # 7
git merge -s recursive \
            -X theirs origin/master  # 8 resulted in message 'up-to-date'
git pull origin/master               # 9 resulted in message 'up-to-date'

After that I noticed that all my changes on local repo are lost !

Is there a way back?

Getting your work back

Have no fear: your commits aren't gone, just unreferenced!

First, make sure your working copy is clean; that is, the output of git status shows that you have no local modifications or staged files. If you do, run git stash to keep your changes off to the side while you're working.

Now run:

git reflog

You'll see a list of the names (sha hashes) and messages of the commits that your current branch has pointed to in the recent past. Look for the name of the commit with the message "message" (or whichever was the last commit before the bad merge). Copy the sha of that commit and point master back to that commit with:

git reset --hard <sha>

Look around your working copy; you should have your work back! If it's still not there, go back to the reflog and walk back through the history until you find the right commit that does have your work. Now you can re-merge if necessary and push your work back out to origin if you choose.

If you stashed your changes at the beginning, get them back with git stash pop once you're satisfied with where you are.

What went wrong

The name origin/master refers to a special kind of branch called a remote tracking branch . Basically, it's a read-only mirror that's synchronized with the last known state of a branch in another git repository; in this case, the master branch in the origin remote. But the rules for when it's actually updated are a little tricky.

When you run a fully-specified command like git pull origin master or git fetch origin master , git will fetch the commit on master from origin , but it stores the commit's name in a special reference called FETCH_HEAD rather than updating origin/master . To keep your remote tracking branches up to date, just run git fetch with no arguments instead.

So, what likely happened is that your reset (#4) and your merge (#8) were both targetting an older commit on master !

What you want to do

To keep up to date with upstream work:

# Update all remote-tracking branches with the lastest changes
git fetch
# Merge them in
git merge origin/master

To share your work:

git push origin master

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