# Git Commands You can also review [Oh Shit, Git!?!](https://ohshitgit.com/), for some good solutions to comon git problems. ## Random Ideas [Kart: DVC for geospatial and tabular data. Git for GIS](https://kartproject.org/), [Discussion](https://news.ycombinator.com/item?id=38073512#git), [Go to Post from 2023-10-30T20:40:06](https://social.lansky.name/@hn50/111325898767760054) [Use KeePassXC to sign your Git commits](https://code.mendhak.com/keepassxc-sign-git-commit-with-ssh/) ## Git Commands and Examples Create a new branch locally within an already cloned repository: ```bash git checkout -b ``` Delete a local branch: ```bash git branch -d ``` Rebase onto the main branch: ```bash git fetch git rebase origin/main git push origin HEAD -f ``` Abort a rebase: ```bash git rebase --abort ``` Stash changes: ```bash git stash git stash pop ``` Revert a commit: ```bash # Should work if only 1 commit was made git reset --soft HEAD~1 # More forceful approach: git revert cb7cf15b54ff09495201244b070d18d96d4703ce git reset --hard HEAD~2 ``` Show changes between two tags: ```bash # Tag from previous version git tag -a v0.1.0 -m "Release version 0.1.0" # Add changes git commit -am "add hint for change log" # and more # Add final tag for version git tag -a v0.2.0 -m "Release version 0.2.0" # Show diff between tags git log v0.1.0..v0.2.0 --no-merges --format="%h - %s" --date=short ``` Git diff log between commits: ```bash git log 79e28d9cef4cc777afc9e5b2569a5d34d9331867..6888fd61ae9d5744effcf27620a645e1750cbafc --no-merges --format="%h - %s (%an, %ad)" --date=short ``` Debug SSH connection via Git: ```bash GIT_SSH_COMMAND="ssh -v" git pull unset GIT_SSH_COMMAND ``` Add executable flag on Windows: ```bash git update-index --chmod=+x git_mirror.sh ``` Backup a repository from a source with all branches and tags. ```bash # fresh clone git clone --mirror # existing local clone update git fetch --all --tags --prune #Optional: zip it tar czf repo-backup.git.tar.gz repo.git ``` Restore a repository to a new destination with all branches and tags. ```bash cd repo.git git remote -v git remote set-url origin git remote -v git push --mirror ``` Renaming branches: ```bash # Delete remote branch git push origin --delete wikiMaster # Delete local branch git branch -d wikiMaster # Move branch git branch -m main wikiMaster # Push git push origin HEAD ``` Wipe a whole git repository, removing all branches, tags, and history, leaving behind only a single, empty main branch. ```bash git checkout --orphan main && \ git rm -rf . && \ git commit --allow-empty -m "Initialize empty main branch" --no-verify && \ git push origin main --force && \ git for-each-ref --format='%(refname:short)' refs/heads/ | grep -v '^main$' | xargs -I{} git push origin --delete {} && \ git tag -l | xargs -n 1 git push --delete origin ```