I found this scenario quite “common” but looking around nothing helped.
I’m not really a Git expert but this should be simple.
Let’s make an example:
UserA and UserB are working on the same version (A). UserA committed and pushed to the remote, so now it’s in version B.
I commit only SOME of the files that I wish to push.
When I try to pull (because it’s impossible to push due to the different version) Git will tell me that the “changes not committed will be overwritten by merge”.
UserA: A------B
\ \<-not possible
UserB: \------C
I wish to pull the new version keeping all my changes, also the not committed ones. Is it possible?
I’m using IntelliJ and his built-in plugin (I find it quite friendly), so if someone can explain me also how to do this from there will be great!
It is possible.
I think the best practice is to commit your desired changes as you already did (let’s say you work on branch “dev”). Then you create a new branch with the changes uncommitted, let’s say you call it “mywork” :
git checkout -b mywork. You commit your other changes on this branch.Then comes the merge. First check you’re working on the right branch
git checkout dev. At that point I’m not too sure it’ll work properly so an idea would be to rungit checkout -b tempdev commitIdWithChangedToBePushed(i.e. you branch from former commit).Then you will merge it all. Run
git pull origin(origin is your remote) as you would have done. Now it’s all properly merged locally, on tempdev. Merge tempdev into dev and delete tempdev. Now, push to origin.As a result, you have now 2 branches :
devwith your clean changes, andmyworkwith your other changes. You can merge them if your idea was to integrate your other changes later.By the way, I don’t know IntelliJ plugin so just try to reproduce it in there.
It is also possible to use stash but I am not a fan of it, just
git stashand after you merge,git stash pop(or I think that’s the process). I don’t like it because merging is not always a trivial process and having your changes back will let your run in troubles. Plus you may forget you stashed changes, and stash over them or forget to unstash. Maybe that’s just me though.