user2524282
user2524282

Reputation: 335

git committed but not pushed - cannot find the files in branch, where are they?

I thought I was on the latest commit of my branch. I moved my code to my local folder, and added and committed them.

When I tried to push the new files, I got the following error:

fatal: You are not currently on a branch.

and I realized I had one of the older commits I specifically checked out.

In my lack of understanding of whatever I was doing, I ended up accidentally recloning the original branch. My files are gone.

Can I find them, or are they literally gone? The commit went through, but git log is not showing anything, and the commit ID I thought I was working with is showing nothing.

I don't know very much about git, so here's all I do:

git clone -b branch_name [email protected]:code.git

and then add, commit, and push changes.

In this case, I checked out a specific commit ID, and added and committed while I had it checked out. But that commit is not showing up.

Upvotes: 0

Views: 1133

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

It appears that you were working in a detached HEAD state, which would explain the error message about not being on any branch. The commits you made should still be visible in the reflog, so first try:

git reflog

See if you can recognize the latest commit you made while not on a branch. The fix here might be to checkout that commit, again in the detached HEAD state, and then create a bona fide branch, e.g.

git checkout S8dk2K8W        # replace S8dk2K8W with actual commit hash
git checkout -b your_branch
git push origin your_branch

Upvotes: 3

Related Questions