A collection of personal and technical notes.
A
branch to origin/main
from B
branchCurrent:
A -> B -> origin/main
New:
A -> origin/main
B -> origin/main
Commands:
git checkout A
git rebase --onto origin/main B
git log --oneline
git rebase -i HEAD~n
edit 1234567 commit message A
Save and then close – :x
. The rebase
will stop at the commit you marked
edit
, then do the amending.
git commit --amend
After making the changes.
git rebase --continue
Finally, push your changes to the remote repository.
git push origin [branch] --force
git rebase
actiongit reflog
# find the appropriate hash and number to reset to
git reset --hard HEAD@{N}
# replace `N` with that number from the reflog hash
Selectively add a change to staging.
git add -p
Set a local branch to track a remote branch.
git branch --set-upstream-to=origin/remote_branch local_branch
Deleted a remote main branch, then sync the new local main branch.
git branch -m <backup> main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
Delete all local branch(es) except the main
branch.
git branch | grep -v 'main' | xargs git branch -D
Print the contents of a commit using its <hash>
.
git cat-file -p <hash>
git clean -n
Display all of the file(s) to be removed from the working tree.
git clean -f
Remove all of the regular untracked file(s) from the working tree.
git switch -c <commit_hash>
Create a new branch from a detached head with a <commit_hash>
.
git checkout <commit_hash>
Checkout a certain commit from history. NOTE:
This will create a detached
head, and will recommend to create a new branch.
Set the global
default branch.
git config --global init.defaultBranch branch-name
git config --list --local
List the git configuration of a local repository.
git config --list
List the global git configuration of a user.
git switch -c <branch> <remote>/<branch>
Switch to a remote branch and track it locally.
git switch -
Switch the active branch to the previously active branch.
git stash --include-untracked
Or,
git stash -u
Undo and stash away the changes to tracked and untracked files.
git stash list
Display a list of all the stashed changes.
git stash push -m "description"
Create a new stash with a good description.
git stash push <directory | file> --keep-index
Undo and stash away the changes made to a directory/file.
git stash clear
Delete all the changes made and stashed away.
git fetch <remote> --prune
Download all of the commits from <remote>
, excluding deleted remote branch.
Delete the last commit but keep the changes in the working directory.
git reset --soft HEAD^
Delete a certain number of commits from the top and then push it to the remote repo.
git reset --hard HEAD~<number_of_commits>
git push <remote> <branch> --force
Align the local branch with remote/branch
and remove all dangling changes.
git reset --hard remote/branch
Stop tracking a file and remove it from the working directory.
git rm file.name
Squash a certain number of commits.
git rebase -i HEAD~<number_of_commits>
Create a new tag on the latest commit.
git tag <name>
Rename an <old>
tag to a <new>
tag.
git tag <new> <old>
git tag -d <old>
git push origin <new> :<old>