Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing local-only changes with git

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?

like image 541
Peter Alexander Avatar asked Nov 24 '11 20:11

Peter Alexander


People also ask

Will git pull erase my local changes?

Never pull before you commit any valid changes. This will wipe off all your changes. To retain your code, you have to commit, then pull, then finally push. First pull the code(hard reset also maybe, as I do it sometimes) from repo to your local directory.

Does git pull affect all local branches?

It will only change your current local branch in the merge part, but the fetch part will update all of the other remote branches if they had changes since the last fetch.


2 Answers

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

like image 88
Jacob Groundwater Avatar answered Sep 27 '22 21:09

Jacob Groundwater


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.

like image 32
u-punkt Avatar answered Sep 27 '22 21:09

u-punkt