I am using a lot the Source Control UI of Visual Code Studio and feel stupid not to know all the secrets of git, plus it seems to be more handy to use the command line with complex cases, so here is my take on the git commands and tricks that are essentials for you to know!

  1. Git commit When you only write
git commit

without the -m

git commit -m

you are in VIM, in escape mode. You then need to start insert mode to write. The simplest way is to type i, and a message will appear on the bottom (– INSERT –). You are in insert mode and you can now type in your message.

After that, you must exit insert mode, and you do that by pressing Esc once. The – INSERT – message on the bottom should vanish. You are now in escape mode again, and you must save and quit.

That is done by using the : key to enter command mode and typing the command wq or x, leaving you with either :wq or :x typed at the bottom.

w stands for write and q for quit, so wq is write and quit. x is an alias for wq.

So you write :wq and you just press Enter and you’re done, out of VIM.

To change the editor to something different run the following command (eg. for vim):

git config --global core.editor "vim"

To have a comment done in Visual Studio Code you can run:

git config core.editor "code --wait"
  1. Undoing things

I found the Git documentation pretty confusing about undoing things, so here is my attempt to make it clearer:

Case A: You just committed and forgot a file or you want to change the commit message.

git commit --amend

If you staged nothing in between (between your commit and the git commit –amend), you will lend on the “change commit message” part. If you staged another file, fileB, then it will replace the initial commit with the fileB added inside the commit. You will see only 1 commit in the git graph.

Case B: You have some staged stuff and you want to send it back to the “modified” / “changes” state For all the files in staged:

git reset .

or for a particular file:

git reset file_name

Case C: git restore, git checkout and git switch

Case D: If you want to remove the last commits of a branch and set it back to the state of a previous commit:

 git reset --hard sha1
  1. Working with remotes

If you git clone a repo from Github for example, you will If you’ve cloned a repository (by running git clone), and then run

 git remote

you should at least see origin — that is the default name Git gives to the server you cloned from. If you run

 git remote -v

you can see the URLs that Git has stored for the shortname to be used when reading and writing to that remote.

You can add a remote:

git remote add <shortname> <url>

For example you add this git remote add pb https://github.com/paulboone/ticgit, now you can run git fetch pb, it accessible locally for your machine.

Why do we need several remotes? For example:

  • if the project is hosted on GitHub and GitLab.
  • If we deploy in prod via git, we save on GitHub and we push on prod.
  • If we want to clone a repo in local on my computer, in another folder.

If you want to see more information about a particular remote, you can use the git remote show </span> command.

You can rename git remote rename pb paul or remove a remote git remote remove paul. Now if you run git remote, there is only origin left.

  1. Tagging

List tags:

 git tag
 OR
 git tag -l
 OR
 git tag -l "v1.8.5*"

Creating tags:

 git tag
 OR
 git tag -l
 OR
 git tag -l "v1.8.5*"
  1. Aliasing Most used aliases:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status