Repository

Looks good to me!

User Tools

Site Tools


kb:intranet:software:git:start

git

The list of helpful git commands was growing too large, so compiling into one page.

Branch

git fetch origin <REMOTE_BRANCH>:<LOCAL_BRANCH>

Submodules

Pull all changes:

git pull --recurse-submodules

Reset changes to submodules (including fast-forwards):

git submodule deinit -f .
git submodule update --init

# From old page

Initial setup

  • Change local username / email: git config user.email [EMAIL] or user.name [NAME]
  • Change default editor: git config --global core.editor vim

Small editing and cleanups

  • Reset author: git commit --amend --reset-author --no-edit
  • Set author: git commit --amend --author "[NAME] <[EMAIL]>" --no-edit
  • Clean all: git reset --hard && git clean -dfx

Patching

  • Create a patch: git diff > [FILE]
  • Apply a patch: git apply --whitespace=fix --reject [FILE]
  • Apply rejected patches: wiggle --replace [FILE] [FILE.rej]

Changing history

  • Interactive add: git add -p .
  • Rebase:
    • git rebase --committer-date-is-author-date --rebase-merges -i [COMMIT]~
    • Change pick to edit
    • git add and git commit --amend
    • git rebase --continue
  • Insert changes to previous commits:
    • git commit --fixup=[COMMIT]
    • git stash
    • git rebase -i --autosquash [COMMIT]^
    • git stash pop
  • Enable autosquashing by default: git config --global rebase.autoSquash true
  • Cherry pick commits:
    • git switch -c [BRANCH]
    • git cherry-pick -x [COMMIT]
    • git push --set-upstream origin [BRANCH]

Signing

By default, GPG keys are used for signing commits (manually using -S flag). As of Git 2.34 (Nov 2021), SSH keys can also be used to sign commits.

  • Enable commit signing: git config --global commit.gpgsign true
  • Use SSH key for signing: git config --global gpg.format ssh
  • Specify signing key (GPG/SSH): git config --global user.signingkey [PUB_KEY]
  • Verify commit has been signed: git log --show-signature ("No signature" for SSH)

Misc

  • Beautify git log: git config --global alias.adog "log --all --decorate --oneline --graph"
  • Check branches with latest commits: git branch -a --sort=-committerdate
  • Manually delete detached commits (warning! stashes will be deleted): git reflog expire --expire-unreachable=now --all && git gc --prune=now
  • Subversion integration: apt install git-svn
  • History: gitk
  • Push to multiple remotes (and fetch only from one):
    • git remote add all GITREPO1 (sets fetch)
    • git remote set-url --add --push all GITREPO1 (overrides push)
    • git remote set-url --add --push all GITREPO2 (appends push)
    • git push -u all main (set upstream to all)
.gitignore
[core]
autocrlf = false
 
[alias]
adog = log --all --decorate --oneline --graph
.gitconfig
[user]
        name = pyuxiang
        signingkey = ...
[commit]
        gpgsign = true
[gpg]
        format = ssh
[alias]
        # Personal
        adog = log --all --decorate --oneline --graph
        addw = -c interactive.diffFilter='git diff --color=always --word-diff' add -p
        diffw = diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'
        diff-staged = diff --cached
 
        # Common operations
        remove = reset --mixed HEAD
        uncommit = reset --soft HEAD~
        recommit = commit --amend
 
        # Get lists of stuff
        branches = branch -a
        tags = tag -l --sort=v:refname
        stashes = stash list
        remotes = remote -v
kb/intranet/software/git/start.txt · Last modified: 4 days ago (19 August 2025) by justin