Skip to content

Instantly share code, notes, and snippets.

@Mabdelwanis
Forked from L1ghtmann/common-git-cmds.md
Created November 28, 2025 23:10
Show Gist options
  • Select an option

  • Save Mabdelwanis/47ca3703aacbbcbc9d5f4f1334a2e196 to your computer and use it in GitHub Desktop.

Select an option

Save Mabdelwanis/47ca3703aacbbcbc9d5f4f1334a2e196 to your computer and use it in GitHub Desktop.
Common git commands I forget

Git Commands

Note: SSH url format

[email protected]:name/repo.git

Add contributor remote branch for edits

  1. git remote add contributor https://github.com/name/repo.git
  2. git fetch contributor
  3. git checkout -b update contributor/master
  4. git push contributor update:master

Add upstream remote branch for ref

  1. git remote add upstream https://github.com/whoever/whatever.git
  2. git fetch upstream
  3. git pull upstream [target branch]

Squash all commits on top of main branch

  1. git checkout <new branch/fork>
  2. git reset --soft $(git merge-base $(git rev-parse --abbrev-ref HEAD) [master/main])
  3. git commit -m "Squashed commit"

Add project to GitHub

  1. Create repo on GitHub
  2. git init in proj dir
  3. git remote add origin [ssh_link_for_repo]
  4. git remote -v (to confirm)
  5. when ready:
    1. git add .
    2. git commit -m “message”
    3. git branch -M main
    4. git push -u origin main

Rename branch

  1. git branch -m [new_name] (local)
  2. git push origin -u [new_name] (remote)
  3. git push origin :[old_name] (old remote)

Compare two branches

  1. git diff [branch to compare]

Cherry-pick commits from one branch to another

  1. git cherry-pick [commit hash]
    1. if theres a conflict, go into the file and delete the bit trying to override + "=====" + branch headers
    2. git add [changed file(s)]
    3. git cherry-pick --continue

Add submodule to custom location

  1. git submodule add <link>.git
  2. git mv <submodule-dir> /new/location/name

Update submodules

  1. git submodule update --remote --merge

Scrap submodule changes from branch head

  1. git submodule deinit -f --all
  2. git submodule update --init OR
  3. git submodule foreach --recursive git reset --hard
  4. git submodule update --recursive

To go back n commits (won’t work for initial commit)

  1. git reset --hard HEAD~n || git reset --hard HEAD^ (for back one)
  2. git push origin [branch] -f

To go forward n commits (won’t work for initial commit)

  1. git reset --hard HEAD@{n}
  2. git push origin [branch] -f

To amend a commit's content

  1. git commit --amend --no-edit
  2. git push origin [branch] -f

To amend a commit's message

  1. git commit --amend
  2. git push origin [branch] -f

Remove current, uncommited changes

  1. git checkout .

Remove current, commited changes

  1. git reset --soft HEAD~1 [keeps work]
  2. git reset --hard HEAD~1 [deletes work]

Remove commit(s) from branch

  • n = # of commits to view
  1. git rebase -i HEAD~n
  2. see the descriptions for what can be done
  3. change the text in front of the sha1 and save to do the actions
  4. when done, check git log that everything worked as intended
  5. if so, git push origin [branch] -f

Squash current commits

  1. git checkout custom-branch
  2. git reset --soft $(git merge-base base-branch HEAD)
  3. git commit -m "My commit!"

To start a clean main branch (remove commit history)

  1. git checkout --orphan [tmp_branch]
  2. git add -A
  3. git commit -am "message"
  4. git branch -D [old_branch]
  5. git branch -m [old_branch]
  6. git push -f origin [old_branch]

To remove old remote branches

  1. git remote update origin --prune

To remove all but a select few remote branches

  1. git branch -a | grep -v "keep" | xargs -I{} echo {} | sed -e 's/^ *//' -e 's/remotes\/origin\///' | xargs git push origin --delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment