git: error: Your local changes to the following files would be overwritten by merge

You’ve got some local changes in your git repo. What to do?

1. you want to keep your changes

a. and track them

git add <local-changes>; git commit -m "<your message>"

b. but don’t want to track them

Note: if you’re doing a git pullthen:

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

will still result in error: Your local changes to the following files would be overwritten by merge

git: what to do with untracked files

I tried --skip-worktree which didn’t work so just moved my .gitignore file (which was causing the problem) out of the way.

https://stackoverflow.com/questions/36996875/git-pull-error-for-git-update-index-assume-unchanged-files

More: https://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree/23806990#23806990

2. you don’t want your changes

git co <local-changes>

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).

https://stackoverflow.com/questions/835501/how-do-you-stash-an-untracked-file

 

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

etc

 

Notes:

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

https://stackoverflow.com/questions/39128500/working-tree-vs-working-directory

 

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.

https://hackernoon.com/exclude-files-from-git-without-committing-changes-to-gitignore-986fa712e78d

 

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

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

http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html

 

gpristine

RANT: I’m so sick of having to think about making my gitrepo clean again. I actually used to take a copy of the directory so I could just blast away the directory if I suspected I was going to end up in a messy git state.

Even git reset hard HEAD (I can’t even remember (or be bothered) trying to check I’ve got the right syntax) ) doesn’t always work – e.g. if you’ve got untracked files you’ve still got to remove them manually.

I finally tracked down the code to completely reset your git repo – the short and intuitive:

git reset --hard && git clean -dfx

Or if you have zsh then simply:

gpristine

Way to go!

https://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind

 

git rebase: CONFLICT

git rebase master
First, rewinding head to replay your work on top of it…
Applying: using feature branch for now
Applying: building from feature branch
Using index info to reconstruct a base tree…
M file/A
Falling back to patching base and 3-way merge…
Auto-merging file/A
CONFLICT (content): Merge conflict in file/A
error: Failed to merge in the changes.
Patch failed at 0002 building from feature branch
Use ‘git am –show-current-patch’ to see the failed patch

Resolve all conflicts manually, mark them as resolved with
“git add/rm <conflicted_files>”, then run “git rebase –continue”.
You can instead skip this commit: run “git rebase –skip”.
To abort and get back to the state before “git rebase”, run “git rebase –abort”.