Git check and merge without touching the working tree

Let's say I have a branch with functions in which I merge upstream changes before 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 moment I want to push my changes. The usual way to do this is:

 git checkout master [changes a bunch of working tree files] git merge feature1 [changes the same files right back] 

This works fine, but makes the compiler (checking the date) think that a whole bunch of files are dirty and needs to be rebuilt, even if the contents are the same. Is there a checkout-and-merge method that leaves the working tree unchanged in this case?

Something like:

 git checkout master --merge-branch feature1 

EDIT:

I am talking only about fast merges, which by definition will not change the state of files.

+20
git
Sep 10 '09 at 1:37
source share
4 answers

[Edit] This is only a partial solution / workaround. See Actual @ djpohly answer below.

Firstly, you can click from anywhere. It doesn’t matter what you checked, or the commits you want to push are in the main.

 git push REMOTE_REPO feature1:master 

see git help push

Hint: git push remoteRepo localRef:remoteRef

As for transferring the wizard to where you are now, without using your working copy ... You can force it like this:

 # (while still on feature1 branch) git checkout -B master origin/master 

But it does a hard reset for the wizard. those. he does not check fast forward.

+7
Sep 10 '09 at 1:52
source share

An easy and safe way to do this - without pushing or force updating - is to get feature1 in master:

 (feature1)$ git fetch . feature1:master From . 4a6000d..8675309 feature1 -> master 

The trick is using . to get the local function 1 ref. This is safer than forcing a main branch update, as it provides a quick switch. (See the <refspec> parameter in the git -fetch documentation for more 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)$ 
+36
Sep 09 '12 at 23:08
source share

It is not possible to merge (or rebase) without touching the working directory (and index), since merge conflicts can occur that must be resolved using the working directory (and / or index).

You can always have a different clone (perhaps using an alternative or symbolic directory of objects to save disk space) or another working directory with contrib/workdir/git-new-workdir . Or use a tool like

+3
Sep 10 '09 at 8:01
source share

If you only care about multiple files and use Unix, you can manually change mtime after the fact using touch -d <timestamp> . Make sure you use ls --full-time to get the timestamp, because the display is not accurate by default.

For example, imagine you are using Docker to create an image for a Python-based web application. If the requirements.txt file changes, it takes a long time to recover, because it needs to load a bunch of third-party libraries and compile them. Just reset this mtime file after merging:

 ls -og --full-time src/requirements.txt # -rw-r--r-- 1 282 2015-11-04 20:03:28.918979065 +0400 src/requirements.txt git checkout master git merge --no-ff feature-foo touch src/requirements.txt -d "2015-11-04 20:03:28.918979065 +0400" 
0
Nov 04 '15 at 9:17
source share



All Articles