uber
uber

Reputation: 5103

How to revert back commit from different branch which affected branch not intended to be affected?

I was working on a branch hello-feature

I then switched to a different branch hello-feature-copy

I intended to push changes for hello-feature-copy so that my colleague could see them, and not affect my local development in hello-feature.

Now it seems that all changes in hello-feature are gone. How to revert this?

This is the command history I got after running history

 1116  git status
 1117  git add src
 1118* git checkout -b hello-feature-copy
 1119* git checkout -
 1120  git checkout -b hello-feature-copy
 1121  git checkout -
 1122  git status
 1123  git checkout -
 1124  git add .
 1125  git commit -m “work for you to see”

Upvotes: 0

Views: 31

Answers (1)

matt
matt

Reputation: 535945

It's hard to tell what you did just from the history (though it was nice of you to provide it), but this seems fairly straightforward, normal behavior. I think the key piece of info you're missing is that when you checkout a branch, Git throws your whole working tree away (the files you can see) and replaces it with a copy of the files in the most recent commit on that branch (which technically is the branch).

Let's say I'm on main and I say this:

% echo hello > hello.txt
% git add .
% git checkout -b mybranch
% git commit -m'here you go'
% git switch main

Now where's hello.txt? I don't see it. Well, of course not! I'm back on main. The commit where I added that file is on mybranch, not on main. When I switched back to main, the set of visible files (the working tree) was replaced with the files in the most recent commit on main. And hello.txt is not one of those.

Upvotes: 2

Related Questions