Git checkout and merge without touching working tree
Say I have a feature branch, into which I merge upstream changes prior to pushing my changes back:
git branch feature1
... [edit my code]
... [commit]
git fetch origin master
git merge fetch_head [or rebase]
... [resolve conflicts]
... [build and test code]
At this point I wish to push my changes. The normal way of doing this would be:
git checkout master [changes a bunch of working tree files]
git merge feature1 [changes the same files right back]
This works fine, but will make the (date-checking) compiler think that a whole bunch of files are dirty and needs a rebuild even though the contents are the same. Is there a way to checkout-and-merge that leaves the working tree unchanged in this case?
Something like:
git checkout master --merge-branch feature1
EDIT:
I am only talking about fast forward merges that by definition would not change the state of the files.
A simple and safe way to do this—without a push or a forced update—is to fetch feature1 into master:
(feature1)$ git fetch . feature1:master
From .
4a6000d..8675309 feature1 -> master
The trick is using .
to get the local feature1 ref. This is safer than forcibly updating the master branch, since it ensures the update is a fast-forward. (See the <refspec> parameter in the git-fetch documentation for details.)
Now that feature1 and master are the same, switching between them will not touch any files:
(feature1)$ git checkout master
Switched to branch 'master'
(master)$
[Edit] This is only a partial solution / workaround. See the actual answer by @djpohly below.
Firstly, you can push from anywhere. Doesn't matter what you have checked out, or whether the commits you want to push are in master.
git push REMOTE_REPO feature1:master
see git help push
Hint: git push remoteRepo localRef:remoteRef
As for bringing master to where you are now without fiddling with your working copy... You can force it like so:
# (while still on feature1 branch)
git checkout -B master origin/master
But this does a hard reset on master. ie it doesn't check for fast-forward.