Managing local-only changes with git

人走茶凉 提交于 2019-12-03 12:34:59

问题


On my local branch, I have some personal (local-only) changes to a Makefile (just changing the path to the compiler). Obviously I don't want to commit those changes in as they are only relevant to me. However, if I don't commit them then I get an error when I try to sync with the remote branch:

% git fetch upstream
% git merge upstream/master
error: Your local changes to 'Makefile' would be overwritten by merge.  Aborting.
Please, commit your changes or stash them before you can merge.

Stashing and then un-stashing the file every time this happens seems tedious. On Perforce for example, you would just move those file(s) to a separate change list and resolve merge conflicts where necessary.

What I want to happen is for git to automatically merge my local Makefile with the remote one (where possible), but without having to commit it. How would I got about doing that?


回答1:


There are likely several ways to approach this problem, here's my thought.

Make a new makefix branch and commit the makefile there. Whenever you need to make the project, switch to that branch. You can work in master and just keep merging, or rebasing the makefix branch against master.

The general idea is that you're creating a branch containing your Makefile that is never pushed.

Personally I would rebase makefix against master so my Makefile changes always stayed ahead of the actual pushable code. It just feels cleaner in my head.

Code Example

git branch makefix
git checkout makefix

Make your changes to Makefile

git add Makefile
git commit -m "Add Local Makefile Changes to Compiler Path"

For every-day work

git checkout master
git fetch upstream
git merge upstream/master

git checkout makefix
git rebase master

It's long and ugly, so I hope someone else has a better way =]




回答2:


It's probably easier to let git do the rebase autmatically, i.e. create a local branch, configure rebase, add your changes, pull ...

git checkout -b mybranch origin/master
git config branch.mybranch.rebase true

Adapt the Makefile to your needs ...

git add Makefile
git commit -m 'My local-only Makefile.'

From now on git will rebase your changes upon

git pull

Alternatively, (especially if rebasing is no option for you) you can create a copy of the regular Makefile to use and gitignore:

cp Makefile Makefile.local
echo Makefile.local >> .git/info/exclude
make -f Makefile.local

However, with that variant you need to keep an eye on the (regular, git-controlled) Makefile and update your local version accordingly if necessary.




回答3:


Why not fix the Makefile to use a variable for the path to the compiler instead of something hard-coded? Then you can just set the proper value in your environment. A lot of these (like CC for the C compiler, CPP for the C Preprocessor, etc, are pre-set by make). If yours is not, you can give it a default value that can be overridden by an environment variable. This example assume GNU make, other make utilities allow similar solutions:

FOO ?= /usr/bin/foo

test:
        @echo CC is ${CC}
        @echo FOO is ${FOO}

(make sure to use real tabs).

This gives:

$ make
CC is cc
FOO is /usr/bin/foo
$ export FOO=/opt/bin/foo
$ make
CC is cc
FOO is /opt/bin/foo
$ make FOO=/just/this/once
CC is cc
FOO is /just/this/once

This is a much more maintainable solution, and avoids the risk of one day accidentally pushing your local-only changes upstream.



来源:https://stackoverflow.com/questions/8262249/managing-local-only-changes-with-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!