Skip to content

Instantly share code, notes, and snippets.

@SebastianGrans
Last active December 3, 2021 19:47
Show Gist options
  • Save SebastianGrans/d173fee19af62c2285e69464729b56eb to your computer and use it in GitHub Desktop.
Save SebastianGrans/d173fee19af62c2285e69464729b56eb to your computer and use it in GitHub Desktop.
Common git commands that I keep forgetting

Copy a branch from the original repo to a new branch on my fork

  1. Clone it:
    • git clone <url> <folder>
  2. Change origin url to my own fork:
    • git remote set-url origin <url>
  3. (Optional) Rename the branch
    • git branch -m <old> <new_branch_name>
  4. Push it to your repo
    • git push -u origin <new_branch_name>

Squashing multiple commits into one

  1. Initiate a rebase:
    • git rebase -i HEAD~4
    • This selects the last four commits, but not all are necessarily included.
  2. The previous command opens up in Nano.
    • Comment out the commits you don't want to squash.
    • Change pick to s in front of the commits you want to squash.
  3. Save.
  4. Push to remote.
    • If you have already pushed one of these commits, you need to perform a force commit.
    • git push --force-with-lease
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment