tips
40 tips Git settings and fixes
One command, setting or habit at a time about Git settings and fixes: what it does, the line to copy and a caveat or two.
Git settings and fixes 40
-
Cancels a merge that stopped on conflicts and puts the branch back the way it was before.
git merge --abort -
Makes a GitHub Actions workflow cancel its older run on the same branch when a new one starts.
concurrency: -
Prints the absolute path of the top folder of the current Git repository, from any subfolder.
git rev-parse --show-toplevel -
Lists every Git setting whose name matches a pattern, with its value.
git config --get-regexp '^user\.' -
Commit new content with the message and authorship of an existing commit.
git commit -C ORIG_HEAD -
Queue a better message for an earlier commit without touching its files.
git commit --fixup=reword:HEAD~1 -
Stop Git from silently dropping message lines that start with a hash.
git config --global commit.cleanup scissors -
Write a subject and a body from the command line without opening an editor.
git commit -m "Fix login redirect" -m "The old code sent signed-out users to a 404." -
Gets a capitalization change into git on a case insensitive file system like macOS.
git mv readme.md README.md -
Prints short git output straight to the terminal instead of in less.
git config --global pager.branch false -
Changes where origin points after a rename, a move or a switch to SSH.
git remote set-url origin [email protected]:owner/new-name.git -
Pushes annotated tags on the commits you push, so release tags never stay local.
git config --global push.followTags true -
Defines your own git subcommands for commands you type often.
git config --global alias.last "log -1 --stat" -
Stops git from printing names like café as octal escape codes.
git config --global core.quotePath false -
Makes git init start on main, or any name you like, without the hint every time.
git config --global init.defaultBranch main -
Reports how much disk space the repository's objects take, in readable units.
git count-objects -vH -
Sorts tags as versions, so v1.10 comes after v1.9 instead of before it.
git config --global tag.sort version:refname -
Signs commits with an SSH key instead of GPG, with three config lines.
git config --global gpg.format ssh -
Resolves a whole conflicted file by taking your version or the incoming one.
git checkout --theirs package-lock.json -
Shows the file behind every git setting, so you know where to change it.
git config --show-origin --list -
Jumps back to the commit you were on before the last merge, rebase or reset.
git reset --hard ORIG_HEAD -
Makes git pull refuse to create a merge commit unless it can simply fast-forward.
git config --global pull.ff only -
Prints git branch and similar lists in columns when the output goes to a terminal.
git config --global column.ui auto -
Ignores files like .DS_Store in every repository from one personal file.
git config --global core.excludesFile ~/.gitignore_global -
Lists local branches with the upstream branch each one tracks and the last commit.
git branch -vv -
Starts every commit message editor with a text of your own, such as a checklist.
-
Rewrites https GitHub URLs to ssh automatically for clone, fetch and submodules.
git config --global url."[email protected]:".insteadOf "https://github.com/" -
Puts GitHub's noreply address in your commits instead of your real email address.
git config --global user.email "[email protected]" -
Stashes uncommitted changes before a rebase and restores them afterwards.
git rebase --autostash origin/main -
Adds the original text to merge conflicts so you can see what each side changed.
git config --global merge.conflictStyle zdiff3 -
Switches Git's diff algorithm so moved or repetitive code can show up as cleaner hunks.
git diff --diff-algorithm=histogram -
Shows the staged diff in your editor while you write the commit message.
git commit -v -
Gives every repository under one folder its own git settings, like a work email address.
name = Your Name -
Checks out only chosen folders of a large Git repository, so the working tree stays small.
git sparse-checkout set docs tools/scripts -
Makes git ask for confirmation when it guesses what you meant after a typo in a command name.
git config --global help.autocorrect prompt -
Registers the repository for scheduled background upkeep like prefetching and commit-graph updates, so it stays fast.
git maintenance start -
Removes stale remote-tracking branches every time you fetch.
git fetch --prune -
Lists your branches with the most recently committed one at the top.
git branch --sort=-committerdate -
Makes the first push of a new branch set up tracking automatically.
git config --global push.autoSetupRemote true -
Adds your staged changes to the previous commit without touching its message.
git add forgotten-file.txt
Nothing in this filter yet.