Frequently Used

Git - Frequently Used

Some git commands I frequently use but sometimes keep forgetting.

checkout to main, pull latest, and clear other branches

git checkout main && git pull && git branch -D $(git branch)


# restore a file
git restore --source <branch_name> <file_relative_path>
# restore to master
git restore --source master pages/docs/index.tsx
# restore to one last commit in master
git restore --source master~1 pages/docs/index.tsx
# restore to one last commit in current branch
git restore -source=HEAD~1 pages/docs/index.tsx
# restore a folder
git restore --source <branch_name> <folder_relative_path>
git restore -source=HEAD~1 pages/docs

references : git-tower


# init rebase
git rebase <upstream> <branch_name>
# pull and rebase
git pull --rebase <upstream> <branch_name>
# when resolve conflicts
## after resolving conflicts and add to staged
git rebase --continue
## to cancel rebase process
git rebase --abort

references : git docs

relocate last commit to another branch

when you accidentally commit to branch_a but actually you want to commit it in branch_b

git checkout branch_a
git reset HEAD~1
git checkout -b branch_b
# or
git checkout branch_b
git add --all
git commit -m "commit message"

remove created tag

git tag -d <tag_name>