gitfilebranchcheckoutpull

git checkout & git pull : Avoid intermediate file changes when executing


I'm surprised I couldn't find anything about that...

In short, my question is if is there a way to avoid the intermediate file changes between the following two commands, if after the following two commands the file content is exactly as was before?

git checkout dev
git pull

The motivation is (Solution that meets only one of the motivations is also better than nothing):

This is a very common scenario when I'm coming back from a feature branch to master after merging origin/feature into origin/dev using a remote source control server.

I would expect that performing git fetch origin dev before will solve this but it doesn't. Also I found some answers like this which talks about grouping the commands together for easiness, but didn't targetted the file-changes issue.


Solution

  • git fetch origin master:master
    

    Replace master with the branch you want to update. (In the OP's scenario, it will be git fetch origin dev:dev)

    This command only works for a fast-forward merge.

    Once the local branch is updated with the remote changes you can switch to the desired branch without causing file changes.

    References: