git branch branchname ref
where ref
is either a sha (abcd123
) or a pointer HEAD@{5}
git reset HEAD~1
--hard
undoes changes to files, --soft
leaves the index unchanged
git reset HEAD file
Bonus: Undo git add .
with git reset
(but just don't do git add .
in the first place).
[git remote add upstream https://github.com/otheruser/repo.git] # First time only
git fetch upstream
[git checkout master] # If not on master already
git merge upstream/master
git rev-list -n 1 HEAD -- file_path
git checkout deleting_commit^ -- file_path
git reset HEAD --hard
git clean -fd
git reflog # View a list of commit SHAs
git cherry-pick abcd123 # Apply the commit with that SHA to the current branch
git commit --amend -m "New commit message"
git add path/to/forgotten.file
git commit --amend –C HEAD
git ls-tree --full-tree -r HEAD
# While on the branch you want to merge to
git checkout --patch other_branch file
git diff --name-status branch1..branch2
- Local:
git branch -D branchname
- Remote:
git push origin --delete branchname
Delete first commit:
git update-ref -d HEAD