Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did I really mess up my repository with my messing around?

a bit of a git newcomer and I wanted to make sure I didn't do any big damage to my git repository. Basically, I used git reset --hard HEAD to revert back to my previous commit as I messed some things up. I then realized I didn't want to lose everything I had done, so I did a git reset HEAD@{1}. Which didn't seem to do anything helpful. Then I tried a git reset --hard ORIG_HEAD.

So I figured, whatever, I'll do the little bit of work over. After I did, I went to push it, and I kept getting this error:

Updates were rejected because the tip of your current branch is behind

I tried git pull origin and it kept not allowing it for a bunch of reasons I can't totally remember.

In the end, I just ended up doing a git push -f and it seemed to work fine and my project is working well again.

Here's my git reflog output if it helps:

enter image description here

I'm just curious if I somehow really messed up either my BitBucket repository that I pushed to or my local one in terms of being able to revert back again. Is there any way to check if my git repo is still functioning properly?

EDIT: BitBucket also says I "stripped" a commit...

like image 323
Doug Smith Avatar asked Jan 12 '23 00:01

Doug Smith


1 Answers

It's going to be ok

Relax.

To ease your concerns, clone another copy in a different directory. Then check things out and bring in the new code you want to push. It will all work out.

Ack! A commit is dangling!

Based on your text I think you might have re-written master's commit history/tree, as a result of git reset HEAD@{1} followed by git reset --hard ORIG_HEAD. Running through exactly what you said, I too "lost" one commit.

This was likely the error message you saw.

20:19:44 (master) ~/code/wat/so_test$ git push
To [email protected]:rgbkrk/so_test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:rgbkrk/so_test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Forcing the git push caused the master branch to take your new path.

Remember how I said to relax? Well, that commit hasn't disappeared. Provided you have the original repo you were working in, it should still be in the git reflog, but not in git log. It's just out there dangling.

2b47149 HEAD@{2}: commit: The bad commit
01346b9 HEAD@{3}: reset: moving to ORIG_HEAD
42af608 HEAD@{4}: reset: moving to HEAD@{3}
01346b9 HEAD@{5}: reset: moving to HEAD@{1}
2f2d2ce HEAD@{6}: commit: More junk text
01346b9 HEAD@{7}: commit: Text
42af608 HEAD@{8}: clone: from [email protected]:rgbkrk/so_test.git

If you truly need that commit back you can do a diff on that commit and the HEAD of your current master. git diff 2f2d2ce..HEAD in the case of the repo I created for this question. You can always merge it into master too.

An easy way to find the "unreachable" commits is to use git fsck, making sure to enable --no-reflogs.

20:48:02 (master) ~/code/wat/so_test$ git fsck --unreachable --no-reflogs
Checking object directories: 100% (256/256), done.
Checking objects: 100% (5/5), done.
unreachable tree 0ca2f12b74de7a33f6dc62374a85d982a96531ce
unreachable commit 2f2d2ce1b6591f0e7a5f67af81a125231ee2e9a1
unreachable blob bcb7bae71bbdfb79ba31ee41e4aa3a11b1ad2f7f

For more on restoring lost commits, check out git ready's article.

Notes for the Future

Couple things to point out: git reset --hard can be dangerous since it throws away all your uncommitted changes. If you have some work you want to keep, but don't want it in this commit, use git stash.

like image 132
Kyle Kelley Avatar answered Jan 19 '23 10:01

Kyle Kelley