Git tips

How do I modify a specific commit?
I have the following commit history: HEAD HEAD~ HEAD~2 HEAD~3 git commit --amend modifies the current HEAD commit. But how do I modify HEAD~3?

append ^ to the commit hash

Store GitHub PAT in ~/.git-credentials, content:

https://<username>:github_pat_xxxxxxxxxxxxxx@github.com

Use VS Code as default git editor
git config --global core.editor "code --wait"

git log <branch>

git show <commit hash>

Undo last commit, keep changes in working directory and staging area git reset --soft HEAD~1

Undo staged files git restore --staged

reset to origin

git fetch origin
git reset --hard origin/main

Show remote origin URL git remote show origin

Remove last commit git reset --hard HEAD^

Remove last n commits git reset --hard HEAD~n

To accept their changes: git merge --strategy-option theirs

To accept yours: git merge --strategy-option ours

Use repo instead of npm in package.json npm install --save username/repo#branch-name

Create orphan branch https://gist.github.com/j8/8997663

To compare the current branch against master: $ git diff --name-status master

To compare commit with previous git diff commit~ commit or shorthand git diff COMMIT^!

Show all branches: git branch -a

Delete from origin: git push --delete origin <branch>

Delete local: git branch -D <branch>

Prune: git fetch --prune origin

Compare file changes between branches: git diff --name-status firstbranch..yourBranchName

Revert head: git reset --hard <commit_id> then git push --force

Revert a merge commit: git revert -m 1 <merge_commit_id>

git force push with lease https://stackoverflow.com/questions/52823692/git-push-force-with-lease-vs-force/52823955#52823955

Stash

diff the stashed file

git diff stash@{​​0}​​^1 stash@{​​0}​​ -- <filename>


retrieve the stashed file

git checkout stash@{​​0}​​ -- <filename>