Git

Git tips and FAQ

  • Show contributors

    git shortlog -s -n
  • Reverting a failed update

    git reset --hard # Reset any changes
    git clean -fd # Delete newly added files and directories
  • Is there a way to remove all ignored files from a local git working tree?

    git-clean - Remove untracked files from the working tree
    -d for removing directories
    -f remove forcefully
    -n Don’t actually remove anything, just show what would be done.
    -X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
    # dry-run
    git clean -dfX -n 
    # if above command looks good, then run this:
    git clean -dfX 
  • Prune local branches

    git remote prune origin --dry-run
  • GitHUb: change remote from http (https://github.com/xmlking/yeti.git) to ssh

    git remote set-url origin git@github.com:xmlking/yeti.git
  • Set git global config (which are already set for you in dotfiles)

    git config --global init.defaultBranch develop
    git config --global pull.rebase false
  • Rename git tags

    git tag new old
    git tag -d old
    git push origin :refs/tags/old
    git push --tags
     
    git pull --prune --tags
  • Undoing the Last Commit:

    git reset --soft HEAD~1
    # If you don't want to keep these changes, simply use the --hard flag. 
    git reset --hard HEAD~1
  • Tags

    git describe --tags
    
    # Delete Tags
    ## local
    git tag --delete v0.1.3
    ## remote
    git push --delete origin v0.1.3
  • setup ssh keys for github and signing GPG key for commits and tags

    TODO YubiKey

  • 5 steps to change GitHub default branch from master to main

    # Step 1 
    # create main branch locally, taking the history from master
    git branch -m master main
    
    # Step 2 
    # push the new local main branch to the remote repo (GitHub) 
    git push -u origin main
    
    # Step 3
    # switch the current HEAD to the main branch
    git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
    
    # Step 4
    # change the default branch on GitHub to main
    # https://docs.github.com/en/github/administering-a-repository/setting-the-default-branch
    
    # Step 5
    # delete the master branch on the remote
    git push origin --delete master

    Ref: 5-steps-to-change-github-default-branch-from-master-to-main

  • When to use Rebase vs merge?

    • When merging feature -> main or release -> main use merge

    • When merging main --> feature to keep your feature branch up-to-date, use rebase Read jeffkreeftmeijer's Rebase vs Merge blog

Last updated