Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git workflow: Can I prevent a certain file from being merged to another branch but still keep it under version control?

I have a repository with two branches: live and stage. The repository holds code for a web based application. I would like to maintain two different .htaccess files for the stage and live branches, but still keep them version controlled, since the nature of serving a stage site is a little bit different (right now for instance, I want the caches to be timed differently).

I'm a novice with git so there is probably an easy explanation, but I'd like to have a workflow where when I was in live and I pulled changes (simple git merge stage), I didn't overwrite the existing .htaccess in the live branch. Is there any way to configure this? Am I missing some big concept in git?

Likewise, there is also a chunk of a (but possibly many) .html files where I would like to keep track of changes but not merge them back into live. Just to be clear I want live and stage to got keep track of changes in these files, but know not to merge those certain changes when doing merges.

like image 286
Ankur Avatar asked Dec 23 '10 03:12

Ankur


People also ask

How do you stop a process from being merged?

You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.

How do you stop a git branch from merging?

How do I cancel a git merge? Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.

How do I pull changes from another branch without merging?

You could use git checkout from the branch you want to transfer the changes to: git checkout <branch name> . That will change all files to match the version in the desired branch. Then you can commit, change, discard whatever you want.

Does git merge affect both branches?

No, merging does only affect one branch.


2 Answers

I found an answer on another question on Stack Overflow, credit goes to fcurella:

Let's say you want to exclude the file config.php

On branch A:

  1. Create a file named .gitattributes in the same dir, with this line: config.php merge=ours. This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B: repeat steps 1-2

Try merging now. Your file should be left untouched.

This seems like a more scalable solution.

like image 113
Ankur Avatar answered Sep 28 '22 12:09

Ankur


Ankur, try this:

Assuming you're checked out on the live branch, run:

git merge --no-commit --no-ff stage 

This will merge the two branches, but instead of making a new commit, will fail and leave these changes in your index file and working directory. Now since you want to keep your .htaccess file intact on the live branch, you can checkout this file from live:

git checkout live .htaccess 

Now your working tree has all the changes from stage with the exception of .htaccess, and so you can finalize the merge commit:

git commit -m "Pulled changes from stage" -a 
like image 28
Hakop Palyan Avatar answered Sep 28 '22 10:09

Hakop Palyan