A situation I encounter frequently is one where Unity has made local changes and I need to do a git pull. One or more of the local changes will get overwritten, blocking the merge. I don’t particularly care about this file as I didn’t manually edit it and I just need to get the latest code.
Here’s how you can git pull, but discard any local changes that might conflict: https://gist.github.com/kelnishi/7e4973abdef2c55b768f6e61cbc7639f
First, use stash --include-untracked to set aside all local changes.
git stash --include-untracked
Now when you pull, it should finish cleanly.
git pull
Next you’ll stash pop to reapply all the local changes you had. After doing this, the files that blocked the original pull will be marked as conflicted.
git stash pop
*Make sure you’re at the root directory of your git repo.
We’ll use some bash git magic to list all the files.
ls-files -u will list all unmerged (conflicted) files from the stash pop.
The output will be a bit verbose so we’ll use cut to select just the file names.
Since each conflict has multiple states listed, we’ll use uniq to remove the duplicate entries.
awk will wrap our lines in quotes so spaces won’t trip us up.
paste will concatenate our 1 file per line into a single space delimited line.
git ls-files --full-name -u | cut -f 2 | uniq | awk '{print "\""$0"\""}' | paste -s -d" " -
You should get all the conflicted files, wrapped in quotes, on a single line output.
*Now is a good time to check to see that you’re not blowing away work you want to keep.
If it all the files are ones you think should be reset, we’re good to go.
Next, FILES=$(!!) saves the output of the last command into a variable. We need to do this because reset will change state and the output of ls-files.
eval will unwrap our variable and allow us to pass the space delimited list as parameters to our two git commands. In this case, we’ll use reset (to unstage and de-conflict) and checkout (to discard the changes) against all our conflicted files.
FILES=$(!!); eval git reset -- $FILES; eval git checkout -- $FILES
After running the last line, you should be in a state similar to before you started this whole process. You should have your local changes intact (and not staged), your branch up-to-date with origin, and any conflicts blocking the merge will have been discarded.
the gist: https://gist.github.com/kelnishi/7e4973abdef2c55b768f6e61cbc7639f