Merging a git branch keeping some file untouched

Standard

I usually maintain a release branch to upload my code to the stage environment or production. Its very common to have some sort of config files which are related to the targeted environment. For example: database connection files, api URLs etc. And I believe that everyone uses different DB and APIs for dev and prod. Yes we use! 🙂

Now the problem begins when we merge our master branch with release and all of our config files gets overwritten. What the F**K. Its again rework on our end to fix those files to make our code work on that particular environment.

There are some workarounds to get rid of this sci-fi thing in a very simple way.

Lets talk git now.

Lets assume we have a repository called captain-cool. Currently we are in master branch and we have a config.xml having my database connection specific details.

Now its time to move our captain-cool project to production. Lets create a new branch called release1.0

git branch release1.0

above command will create a branch on local with exact copy of our master branch which is ready to get pushed on our git server.

We have to switch to our very new branch. So let’s do then.

git checkout release1.0

Now lets go to our config.xml file and make appropriate changes as per the environment we are planning to use.

Now we are in release1.0 branch and can make our push move.

git add -all
git commit -m "updating config for stage"
git push origin release1.0

enter your credentials and our release1.0 branch with updated config.xml file is on the git server. Congratulations!

Now the action begins when we have added some new cool features and wanted to push those features to release1.0 branch to make them live.

Let’s switch back to master branch and do some modifications.

git checkout master

Doing some changes ……….@#@########&*(&&

Now lets push our new features to master

git add --all
git commit -m "adding some cool features"
git push origin master

Now, Its time for our heroic move to merge our new cool features from master to release1.0

Let’s do then.

  1. switch to release1.0
    git checkout release1.0
  2. run merge command with option --no-commit
    git merge --no-commit master

    --no-commit option don’t commit your merged changes to the repository. we did make use of this option just because we want our config.xml file to keep un-touched. Remember right?

  3. Now we have to revert our config.xml file as per the last commit to our release1.0 branch as our last commit had the version that we want to keep.
    git checkout HEAD -- some/path/config.xml
    

    We have our old config.xml file with us

  4. It’s time to commit our merge.
    git commit -m "merged cool features from master"
  5. And in final step, let’s push our cool features to our release1.0
    git push origin release1.0

Here we have successfully merged changes from master to release1.0 keeping our config.xml untouched.

If you guys have more than one file to ignore then I afraid you have to loop step 3.

Guys let me know if you came across a better solution to do this as I found this solution little long and time consuming.

Sometime I want to create a command line utility to merge all those above steps. Let me know if anybody did that.

Cheers!

Happy Learning!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s