> For the complete documentation index, see [llms.txt](https://xmlking.gitbook.io/macos-setup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xmlking.gitbook.io/macos-setup/tips/git.md).

# Git

Git tips and FAQ

* Show contributors

  ```shell
  git shortlog -s -n
  ```
* Reverting a failed update

  ```shell
  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.
  ```

  ```shell
  # dry-run
  git clean -dfX -n 
  # if above command looks good, then run this:
  git clean -dfX 
  ```
* Prune local branches

  ```shell
  git remote prune origin --dry-run
  ```
* GitHUb: change remote from `http` (<https://github.com/xmlking/yeti.git>) to `ssh`

  ```shell
  git remote set-url origin git@github.com:xmlking/yeti.git
  ```
* Set git global config (which are already set for you in [dotfiles](https://github.com/xmlking/macbooksetup/blob/main/dotfiles/.gitconfig/README.md))

  ```shell
  git config --global init.defaultBranch develop
  git config --global pull.rebase false
  ```
* Rename git tags

  ```shell
  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:

  ```shell
  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

  ```shell
  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

  ```shell
  # 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](https://stevenmortimer.com/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](https://jeffkreeftmeijer.com/git-rebase/) blog
* Git worktrees Many developers want to run multiple coding agents in parallel, in the same repository. But how do you keep your agents from getting in each others' way? [Git worktrees](https://git-scm.com/docs/git-worktree) are a common technique for this. They allow you to check out multiple branches simultaneously, in separate directories, all sharing the same Git history

  ```shell
  # Create a worktree and change to its directory
  git worktree add -B my-branch && cd ../my-branch
  ```
