Skip to content

Instantly share code, notes, and snippets.

@sonukapoor
Last active November 30, 2023 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonukapoor/1accbdc21f4ddea2f75b2e2247ddb85f to your computer and use it in GitHub Desktop.
Save sonukapoor/1accbdc21f4ddea2f75b2e2247ddb85f to your computer and use it in GitHub Desktop.
Git rebase easy
// make sure that you are on your branch
git fetch origin
git reset $(git merge-base master $(git branch --show-current))
Git download PR into new branch
git fetch origin pull/2/head
git checkout -b NEW_BRANCH_NAME FETCH_HEAD
Check the history of a file between two branches
git shortlog main...release/<YOUR_VERSION> <RELATIVE_FILE_NAME_WITH_PATH>
View history of a file with changes (even if it was moved)
git log --follow --patch -- libs/cx-portal/lifecycle/features/lifecycle/src/lib/lifecycle.component.t
Create new branch from Commit
git branch <NEW_BRANCH_NAME> <COMMIT_ID>
Git show last 10 commits on one line
git log --pretty=oneline -10
or
git log -10 --online
Git squash list x items when using fixup!
git rebase -i --autosquash HEAD~x
Git undo from previous commit
git checkout -f HEAD~1 file1 file2 file3
Git undo last commit
-- preserve commits (making it un-staged, aka undo a git commit that was not pushed yet)
git reset --soft HEAD~1 or git reset HEAD~
-- discard last commit
git reset --hard HEAD~1
-- go to a specific commit and discard anything after that. You can get the hash using git log
git reset --hard <SHA>
Git sort branch list
git branch --sort=-committerdate # DESC
git branch --sort=committerdate # ASC
View reflog
git reflog
Push local branch to a different remote branch
git push origin local-name:remote-name
Rename local branch
git branch -m new-name
Rename remote branch
git push origin :old-name new-name
git push origin -u new-name
Find git branch using pattern
git branch --list '*some-branch*'
Checkout last branch
git checkout -
Find SHA1 of file
git ls-files -s fileName.ext
Undo `Your branch is ahead of master`
git reset --hard origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment