Random git tipsΒΆ

Remove bad commit via interactive rebase:

git rebase -i origin/main
# The most recent commit is the bottom of the list
# Leave the first commit untouched
# Say I have 3 commits
# pick d83fd3 Added some feature
# s 11111a Fixed some stuff
# s 11111b Fix some more stuff

# This will remove the fix some stuff commits
git push origin feature --force

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master
# At this point, untracked (new and unstaged) files remain in your working directory
# to remove those also:
# x: ignored files, d: untracked directory, f: untracked files
git clean -xdf

List branches on remote:

git branch -r

Create a new branch and switch to it:

git checkout -b <new_branch_name>

Check what remote a local branch is tracking

git branch -vv

Pull a directory from another branch, without changing the log of the current branch:

git checkout source_branch -- path/to/folder

Remove the last n commits

git reset --hard HEAD~2
git push origin +HEAD

# +HEAD indicates that the current local branch's HEAD should be pushed, and the + forces the update to the remote branch, even if it results in a non-fast-forward update. (similar to using --force)

Comments

comments powered by Disqus