1

I do alot of personal development tweaks on code on my side, like adding an account automatically, opening up sublime in a certain way when there's an exception (with a rescue_from from an ApplicationController), and other misc tweaks I think are very useful for me but that I don't think I should/other colleagues would like to have committed.

I searched around a bit and supposedly git doesn't have any way to ignore single file lines.

I figured a solution (albeit probably a little complicated and involving markup) would be using Git pre-commit hooks, but... doesn't sound very neat to me.

How can I keep personal code tweaks on my side, inside existing, committed files, without manually stashing/restoring them between commits, while also being branch-independent?

parreirat
  • 715
  • 3
  • 9
  • 22

1 Answers1

1

I searched around a bit and supposedly git doesn't have any way to ignore single file lines.

Good news you can do it.

How?

You will use something called hunk in git.

Hunk what?

Hunk allow you to choose which changes you want to add to the staging area and then committing them. You can choose any part of the file to add (as long as its a single change) or not to add.

Once you have chosen your changes to commit you will "leave" the changes you don't wish to commit in your working directory.

You can then choose if you want this file to be tracked as modified or not withe the help of the assume-unchanged flag.


Here is a sample code for you.

# make any changes to any given file

# add the file with the `-p` flag.
git add -p

# now you can choose form the following options what you want to do.
# usually you will use the `s` for splitting up your changes.

git add -P

Using git add -p to add only parts of changes which you will choose to commit.
You can choose which changes you wish to add (picking the changes) and not committing them all.

enter image description here enter image description here

# once you done editing you will have 2 copies of the file 
# (assuming you did not add all the changes)
# one file with the "private" changes in your working dir
# and the "public" changes waiting for commit in the staging area.

Add the file to .gitignore file

This will ignore the file and any changes made to it.

--assume-unchaged

Raise the --assume-unchaged flag on this file so it will stop tracking changes on this file

Using method (2) will tell git to ignore this file even when ts already committed.
It will allow you to modify the file without having to commit it to the repository.

git-update-index

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Sounds great! I already used hunks for commiting and stashing, but did not know about the asume-unchanged flag. I'll accept your answer in a few days, just to see if any easier solution pops up. – parreirat Jan 12 '16 at 12:30