Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Last active September 9, 2023 16:54
Show Gist options
  • Save mike-pete/f5e9de8f352a492426e1054e99911ab5 to your computer and use it in GitHub Desktop.
Save mike-pete/f5e9de8f352a492426e1054e99911ab5 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

rebase the last 4 commits: git rebase -i origin/<branch name>~4 development

clone again in a different directory: git worktree add ../the_project_2 <branch>

intereactive rebase: rebase -i origin/development

create branch: git branch <branch> OR git checkout -b <branch>

delete local branch: git branch -d <branch>

delete remote branch: git push <remote> --delete <branch>

show commmit hist: git log --oneline

revert to a specific commit: git checkout <commit-id> .

get a warming if there are changes on the remote branch during a force push git push --force-with-lease

Don’t forget the final ‘ .’ — You aren’t required to add this, and it may look like it has worked but if you leave this off it will take you to a new “detached head state” where you can make changes and it will allow you to make commits, but nothing will be saved and any commits you make will be lost.

git revert --no-commit 0766c053..HEAD git commit

  • Create a new stash: git stash -m <message>
  • Remove a stash: git stash drop <stash>
  • List stashes: git stash list

rebase

git checkout <local-branch>

# make a new branch to do the rebases on
git checkout -b <local-branch>-rebased

# squash everything to make rebasing easier:
git rebase -i <first commit of branch>

git fetch
git rebase origin/<remote branch you want to merge to>
git push origin <local-branch>-rebased:<remote branch you're merging to> --force-with-lease
@mike-pete
Copy link
Author

Get rid of old commits that are added to end of your MR:
git fetch
git rebase origin/<branch you're merging to>
git push --force-with-lease

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment