Managing local-only changes with git

后端 未结 3 563
野的像风
野的像风 2021-02-04 12:54

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

相关标签:
3条回答
  • 2021-02-04 13:02

    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.

    0 讨论(0)
  • 2021-02-04 13:14

    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 =]

    0 讨论(0)
  • 2021-02-04 13:16

    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.

    0 讨论(0)
提交回复
热议问题