Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling temporary changes (not to be committed) in Git

Tags:

Often while working on a branch I need to introduce some "temporary" changes (such as additional debugging info, or a change which lets me better observe the thing i'm actually working on).

About these "temporary" changes:

  • I want them in my working copy of my branch, because they help me to work on the actual change,
  • I don't want them committed to the branch, because the branch is going to be merged into master some time and they're not production code.

Currently I just keep them as unstaged and I skip them manually when staging every commit. However I can't stay with this solution because:

  • All the time I have to remember which files I need to skip,
  • Someday I'm going to end up with 2 changes in one file, one being temporary, one to be committed, and it's going to be really troublesome.

How should I deal with it?


gitignore is obviously out of the question because I don't want to ignore the whole files and I'm still interested in changes from other committers (I need to rebase the branch to master from time to time).

like image 935
Kos Avatar asked Aug 22 '11 12:08

Kos


People also ask

How temporarily save changes without commit git?

Here is how to save local changes without commit in Git. Just issue git stash command as shown below from your present working directory which contains unsaved unchanges. This will stash all changes you made and your repository will be restored to your last commit.

How do I save a temporary change in git?

The "git stash" command can help you to (temporarily but safely) store your uncommitted local changes - and leave you with a clean working copy.

What does stashing changes mean in git?

Stashing your work The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.


2 Answers

I typically deal with this by using:

git add -p 

... to stage changes hunk-by-hunk. Then you just have to make sure to press n for your debugging changes.


If I have more involved changes of this type, I'll create a branch called something like local-changes with a commit at its tip that introduces the debugging code. After creating a few more commits, I'd then use:

git rebase -i master 

... to reorder them so that the commit with the local changes is at the tip again. Then to update master and return to the local changes branch, you can do:

git checkout master git merge local-changes^ git checkout local-changes 
like image 92
Mark Longair Avatar answered Oct 23 '22 17:10

Mark Longair


Try git update-index --assume-unchanged <file>. This way git will not add the file to the index when using git add or git commit -a.

EDIT: This doesn't allow you to deal with the case of having one file with both temporary and permanent changes.

like image 42
Chaitanya Gupta Avatar answered Oct 23 '22 17:10

Chaitanya Gupta