'Git merge not giving the files from Main
I am working with git and visual studio. I have my local branch, remote branch and main(parent).
After a week of work, I did a merge from main (from visual studio but something like git merge main)
I got 100 files, i resolved 1 file (so didnt resolved technically, took their change and edited mine on top)
Just committed my local changes first and from VS i unstaged all the files I got from merge and did an undo on those files (thought I will do git merge main again)
Now, when I do git merge main I just get all files are up to date. Git is thinking it gave me all the files from parent, but I had it, i did an undo from VS and now i am unable to get those 100 files. How to get that ?
Note : Nothing on my local that I need to save. And dont want to create a new branch because of existing code review changes.
I also tried git reset --hard origin/local
Solution 1:[1]
When you perform a merge, you are creating two things: a new version of the content of your repository, and some metadata about what's been merged. When you started the merge process, git got ready to record the metadata, and paused while you sorted out the content.
Then this happened:
Just committed my local changes first and from VS i unstaged all the files I got from merge
This changed the content that would be included in the commit, but it didn't change the metadata. So when you committed, you were still committing a merge, not a "normal" commit.
An important thing to keep in mind here is that git doesn't version files, it versions the whole tree at once. A commit is a snapshot of that tree, plus some metadata, which includes its "parents". A merge commit therefore can't record which files were merged, only which commits were used to produce the result.
Now, when you ask to do the merge again, git is looking for commits which aren't already in the history of your current branch; it finds the merge commit you created, and sees that all the commits you're hoping to merge are already on one side of it, so don't need merging.
The solution is to rewrite history - create an alternative reality where you didn't perform this merge, but did commit your local changes.
- Make sure you have no uncommitted changes. If you have, save them somewhere like
git stash. - Use
git logor a graphical tool to find the commit hash of the merge commit (let's call it "M") and the last commit made on your branch before the merge (let's call it "B"). - Reset your branch to commit "B" with
git reset --hard B. This rewinds the branch, and discards everything after that point from your working copy. - Check out all the content from commit "M", without any metadata, using
git restore --source=M .(or, I think,git checkout M .on older versions of git before the more intuitiverestoreandswitchcommands were added) - You should now have the changes from the "not really a merge" as uncommitted changes. Commit them now.
- Perform the merge again - and this time, don't discard the changes you want to keep. :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
