Git

Signing Commits / Tags in Git with GPG

Source: https://help.github.com/en/github/authenticating-to-github/managing-commit-signature-verification

Generate new/Use existing GPG key

  • See Basic GPG guide

Add key to Github / Bitbucket

  • Add to Github under Settings > SSH & GPG keys

Tell Local Git about your GPG key

  • Get GPG key id:
    gpg --list-secret-keys --keyid-format LONG
    
  • Set User Signing Key:
    git config --global user.signingkey <keyid>
    

    (↑ substutute with the GPG key ID from the list above, after 4096R/...)
    For local git configuration, add in the config file:

    [user] signingkey = <keyid>
    
  • Manual sign:
    git commit -S -m "message"
    
  • To auto sign every commit, add to git config:
    [commit]  gpgsign = true
    

Verify signed commits

  • To verify a signed commit
    git verfiy-commit <commit-hash>
    

    Note: The public key has to be imported in order for gpg to be able to verify the commit

Clean-up branches that were deleted on remote

When a branch gets removed on the remote, it may stay locally if it was checked out before. To remove branches that were deleted on remote locally as well, do this:

First you need to fetch the pruned branches:

git fetch -p
"git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"

You can also add this to your .gitconfig as an alias:

[alias]
	clean-pruned-branches = "!git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D"

Now you can simply run

git fetch -p
git clean-pruned-branches

Lines of Code

gitlogbyloc() {
  git ls-files | while read f; do git blame -w -M -C -C --line-porcelain "$f" | grep -I '^author '; done | sort -f | uniq -ic | sort -n --reverse
}

Output looks like this:

710 author Ahsoka Tano
709 author Anakin Skywalker
501 author Not Committed Yet

Override .gitconfig in certain directories

To override the gitconfig for multiple folders, add this at the end of the global gitconfig file (usually at ~/.gitconfig or ~/.git/config)

[includeIf "gitdir:~/toplevelFolder1/"]
  path = ~/topLevelFolder1/.gitconfig_include

Rebase onto

Link to Twitter

git rebase --onto origin/main Branch1 Branch2

Change Past Commits Using Rebase

This may only be the last resort and can have destructive consequences.

If not otherwise applied, the commit date of all past commits will be set to the current time.

Abstracted from a post from Atlassian Bitbucket

Steps:

  1. Find the commit you want to change
  2. Copy the hash of the commit that was done before the commit you want to change.
  3. git rebase -i <Earlier Commit Hash>
  4. In the opened text editor, locate the commit you want to change by its hash and change the word pick to edit. Close the text editor after this.
  5. In the command line, change the commit. After any change, the commit message editor will open. Just close it. a. Author: git commit --amend --author="Author Name <email@address.com>" b. Date: git commit --amend --date="$(date -R)" or git commit --amend --date="Thu, 15 Apr 2021 21:48:13 +0200" c. Message: git commit --amend -m "New Commit Message"
  6. Check git log
  7. To continue: git rebase --continue
  8. It may happen that there are conflics while applying the next commits. Check the files, add them and continue with git rebase --continue
  9. Merge or force push a. git pull b. git push origin main --force