Add a file despite listed in gitignore
Not sure why I wanted this 🤷. But i came across a situation where i wanted to add a file from bin. But most cases think why you’re doing this. You most probably not need to add ignored files.
git add --force my/ignore/file.foo
Add only selected files from the a list of other unstaged files (interactively)
git add -i
Use the awesome interactive mode! It has a whole lot more features. It’s quite self explanatory but you can read more about it here!
Cherry-pick pitfalls
You know how to cherry-pick.
Here’s a gotcha you’ll need to remember when git cherry-pick A..B
:
- In the “cherry-pick A..B” form, A should be older than B. If they’re the wrong order the command will silently fail.
- Also, this will not cherry-pick A, but rather everything after A up to and including B.
- To include A just type git cherry-pick A^..B
Being good at updating diff header for add --patch
[e]dit
This should allow you to quickly modify the hunks to select the parts you want.
- If you remove a line that starts with + then subtract one to the new line count (last digit of the hunk’s header).
- If you remove a line that starts with - then add one to the new line count (last digit of the hunk’s header).
- Don’t remove the other lines (reference lines).
Add only all unstaged files AND ignore untracked files to commit
git add -u
This is one you may use often. I highly recommend to make this an alias.
git config --global alias.stagem 'add -u .'
Ignore files from the add and commit process
A use case i can think of is when you’ve created a temporary file on the local repo that you have no intention to push but not a point to add to the .gitignore (being the file’s nature). Just ignore it so you can safely git add --all
.
- To ignore:
git update-index --assume-unchanged -- <filepath>
- To un-ignore:
git update-index --no-assume-unchanged -- <filepath>
This will ignore the file from showing in your add and commit status.
As this is quite the long command, i recommend getting it into an alias:
git config --global alias.ignore 'update-index --assume-unchanged --'
git config --global alias.unignore 'update-index --no-assume-unchanged --'
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.