Git¶
Separate git configurations for work and play¶
Put something like this in your ~/.gitconfig:
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig.work
[includeIf "gitdir:~/code/"]
path = ~/.gitconfig.personal
~/work, you can have a separate user/email etc for
repos in that folder.
Delete a tag that's already pushed¶
Signing commited commits¶
If you want to sign the last N commits that you have already pushed:
Create an empty commit¶
git commit --allow-empty -m "blah blah"
Useful if you have a gitops workflow and just want to raise a PR to trigger something from there.
Show parents of a merge commit¶
or
Both these show similar information: the committer, the parent(s), commit text and so on.
Show files changed between two commit IDs¶
Now that you have the two parents of a merged request (see above tip), you can see all files changed between its two parents with a command like this:
Color coded git output¶
Git objects¶
- Tree
- Blob
- Commit
- Tag
Reverting a reverted commit in git¶
If you've reverted a recent commit with:
You can undo the revert with this:
Using git stash to save changes temporarily¶
- First do a
git add(if its a new file to be tracked), thengit stash - Check with
git stash list - Reapply with
git stash apply - Otherwise use
git stash popto recover the files and discard the stash - Creating a branch from a stash:
View commits that aren't in master¶
- To see commits that have not yet merged to master:
Bitbucket: checkout a pull request¶
- From here: https://gist.github.com/hongymagic/6339056
First add this to .git/config under the origin section:
Then fetch the pull requests:
## Modern Git: switch and restore
Instead of the overloaded `git checkout`, use:
- `git switch <branch-name>` - Switch to a branch.
- `git switch -c <new-branch>` - Create and switch to a new branch.
- `git restore <file>` - Unstage or discard changes in a file.
## View commits that aren't in main
- To see commits that have not yet merged to main:
``` sh
git log --no-merges main..
...
Git rebase vs normal pull¶
- Instead of a normal pull, try this:
git pull --rebase origin main
- And make it permanent with this:
git config --global pull.rebase true - This removes the superfluous 'merge commit' that comes up normally.
- Finally:
Git log on a file¶
actually do this:
Submodules¶
- To update all submodules:
- To fetch the latest code from a submodule:
Then merge the code. The next time the parent repository is pulled, updating the submodule will get the latest commit in it.
Working with remotes¶
- Changing a remote's name
- Adding a remote
- Then as usual push/pull to and from these remotes
Leaderboards¶
Hide Whitespace Noise¶
Good when some one changes indentations and a whole lot of rubbish comes).
Show words that have changed inline¶
See what everyone is up to¶
Generate a changelog¶
View complex logs¶
Handy aliases¶
Put something like this in your .gitconfig
[alias]
st = status --branch --short
wat = log --graph --decorate --oneline -15
follow = log --follow -p