0

Few weeks back, I have started work on new feature and created local branch from master using below method:

  1. git fetch
  2. git checkout master
  3. git pull
  4. git checkout -b feature-1 master

Fast forward, other devs have made many changes mean while and it was merged with master branch. So i have went ahead and tried to merge the updated master branch changes into my local feature-1 branch. So i have done below:

  1. git fetch
  2. git checkout master
  3. git pull
  4. git checkout fearure-1
  5. git merge master

Now instead of conflict, it performed the auto merge for few files and i have lost my changes in it. So what should i do to prevent that? I want to keep my changes with the changes which is in master branch.

DS9
  • 2,995
  • 4
  • 52
  • 102
  • Did you commit your changes before? – deepakchethan Sep 20 '21 at 13:52
  • yes i have frequently committed my changes and also pushed my local branch into remote. – DS9 Sep 20 '21 at 13:53
  • It is silly to say git pull after git fetch. Pull _does_ fetch. Pull or fetch, not both. – matt Sep 20 '21 at 13:54
  • 1
    yes you are right but got a habit to do that way (i.e. to check which branch is outdated in my local) – DS9 Sep 20 '21 at 13:56
  • Okay, let's get down to business. What does "lost my changes" mean? Also has your branch ever been merged into master? – matt Sep 20 '21 at 14:01
  • @matt No my branch was never merged into master. "lost my changes" means code which i have done in this local branch was overwrite with master branch code. – DS9 Sep 20 '21 at 14:05
  • 1
    A merge of master into your branch will not "overwrite" your committed version of the same file. It _combines_ what you did with what happened on master. – matt Sep 20 '21 at 14:07
  • @matt unfortunately that is happening for me, i will try to record a video and add it in my question. – DS9 Sep 20 '21 at 14:09
  • I doubt it. Probably there is something wrong with how you are "seeing" the file. – matt Sep 20 '21 at 15:09

1 Answers1

2

What I would do in this situation is reset your branch hard to before the new merge commit, to undo the merge, and rebase your branch onto origin/master, so that your commits are appended to the most recent state of master.

So assuming the merge is the last thing that happened, so that the most recent commit on your branch is a merge commit:

git fetch
git switch feature-1
git reset --hard @~1
git rebase origin/master

But be warned that that will not work if you've done anything since the merge.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • thank you very much for your answer but can you please provide snippet git commands to do that. I am lost here. – DS9 Sep 20 '21 at 14:07
  • for some reason it is not trying to do rebase for commit before X date, i have asked question here: https://stackoverflow.com/questions/69256297/how-merge-request-works-in-gitlab so something may have happened which might have messed up the branch but not sure. – DS9 Sep 20 '21 at 15:21
  • During rebase it have tried until the same commits which is visible in Gitlab MR (ss is attached in that question). so that may be the reason behind it. But i am not sure why it is not showing all the commits in MR. – DS9 Sep 20 '21 at 15:24
  • Well, you're not providing enough information for me to say anything more about the matter. – matt Sep 20 '21 at 15:35