git: what to do with untracked files

Either track or ignore.


Track: Add untracked file to another branch

I’ve got a branch dev/branch with untracked files A, B.

I’m happy to delete or stash B but I’d like to add A to another branch test/branch.

How do I do that?


The easiest would have been if you were happy to stash them all. Then you could do git stash --all(which includes untracked and ignored files).


But, this has multiple parts.

1. let’s get rid of B

If we don’t know what to do with B, e.g. you don’t have time to look at it, then stash it. Otherwise, delete it.

  • stash:

git add B; git stash

  • delete:

rm B

2. let’s move A to test/branch

git checkout test/branch

git add A




Current docs say working directory but, as of 2.9.1, will read working tree.


Ignore: just ignore untracked files

I’ve got a feature branch with a custom entry in .gitignore to ignore *.out.

However, I now checkout masterand find I’ve got a whole load of untracked files.

I don’t want to add it to the global .gitignore as I don’t want to change the behaviour for other developers.

Neat trick: add it to .git/info/exclude which acts as a local .gitignore.


Note: if you just want to temporarily exclude a file you can use:

git update-index --assume-unchanged <file>


Leave a Reply

Your email address will not be published. Required fields are marked *