Shiqiang Jin

Uncommonly used git code list

Caleb / 2020-02-06

It happens to me that I forget the Git codes that I don’t use often. So I record notes them in this blog whenever I am googling to find the Git codes I forget.

List of Git Code

git rebase -i HEAD~2 # local
git push origin +master # remote

I often make a commit without noticing that I need to make another small changes further. In this situation, I need to delete this commit and then add all the changes together in the working directory to the staging area.

Delete a local commit

Assume we have 4 commits in the repository:

git log --oneline
9622c20 (HEAD -> master) add uncommonly used git
040964d (blog/master) stat 950 generalization error
1a3484b update stat 341
871e0d7 add week 3 in stat 341

Commit 9622c20 is the last commit you want to delete. To do that, we use

git rebase -i HEAD~2

This command will open a text editor with two lastest commits:

pick 040964d stat 950 generalization error
pick 9622c20 add uncommonly used git

# Rebase 1a3484b..9622c20 onto 1a3484b (2 commands)
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

The corresponding changes before the previous commit will be restored. If you want keep the changes, use git reset --soft <commit>

Delete a remote commit

git push origin +master

The + sign before the name of branch you are pushing tells git to force the push. Be careful of using this command because once you do it you delete them forever.

Undo the git add .

git reset # unstage all changes
git reset <file> # unstage <file>.

Restore an old version of the project 2

The most straightforward way to restore an old version is to use git reset with option --hard

git reset --hard 9622c20

HEAD is now at 9622c20 add uncommonly used git

This will rewind your HEAD branch to a specific version you’d like to restore. All commits that you have made after this version (9622c20) are effectively undone. Your project is exactly at it was at that time point.

If you use --soft instead of --hard flag, Git will keep all changes in those “undone” commits as local modifications:

git reset --soft 9622c20

You will be left with all the changes after that version (9622c20) in your working directory and then decide what to do with them.

Restore a revision in a new local branch

Using reset command is dangerous because it would wipe out all the versions after that old version. A safer way is to restore the old version in a new branch with HEAD branch untouched and decide what to do next.

git checkout -b old-project-state 9622c20

You’re gonna create a new branch called “old-project-state”

  1. How to delete a commit in git, local and remote
  2. How can I restore a previous version of my project?