Skip to content

Instantly share code, notes, and snippets.

@guillaumemolter
Last active November 19, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumemolter/c45eb3720e343af6f5f5 to your computer and use it in GitHub Desktop.
Save guillaumemolter/c45eb3720e343af6f5f5 to your computer and use it in GitHub Desktop.
Git cheat sheet

Updated version: https://hsph.me/git

https://hsph.me/gitpr

Guillaume's Git cheat sheet

Because sometimes reseting the stach on the orgin of the local branch is confusing me :-)

Update local branch without loosing local commits and to prevent merge commits ( Merge branch 'master' into )

git pull --rebase origin master

Cleanup a branch before PR (i.e: squashing typos and code styling)

  1. Get a nice list of commits git log --oneline origin/master..<branchname>
  2. Interactive rebase git rebase -i <commitid> (or git rebase -i origin/master)
  3. Follow prompts (squash to merge all commits together) then Esc+wq
  4. Force (because we rewrite history) push to branch git push -f origin <branchname>

Add a submodule

  1. go to the right folder
  2. git submodule add git@github.com:REPO.git (SSH URL)

Move local commit from master to a branch

  1. (optionnal but recommanded) Make sure your repo is set with a SSH URL (git@github.com:REPO.git)
  2. (optionnal but recommanded) git fetch origin
  3. (optionnal but recommanded if this is not a brand new repo) git rebase origin/master
  4. git branch BRANCHNAME
  5. git reset --hard origin/master (Use with care - make sure "git status" is clean and you're still on master)

Update submodules

git submodule update --init --recursive

Clone with submodules but without top level folder

git clone --recursive git@github:me/name.git .

Remove a submodule

  1. Delete the relevant section from the .gitmodules file (usually 3 lines).
  2. Stage the changes git add .gitmodules.
  3. Delete the relevant section from .git/config (usually 2 lines).
  4. Run git rm --cached <path_to_submodule> (no trailing slash).
  5. Run rm -rf .git/modules/<path_to_submodule>
  6. Commit git commit -m "<commit_message>"
  7. Delete the now untracked submodule files rm -rf <path_to_submodule>

Clean and reset a repo and its submodules

  1. git clean -xfd
  2. git submodule foreach --recursive git clean -xfd
  3. git reset --hard
  4. git submodule foreach --recursive git reset --hard
  5. git submodule update --init --recursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment