Mastering Git commands πŸ‘©β€πŸ’»

Mar 22, 2021Β·

5 min read

You probably know Git and its commands, but do you know how does it work?

Git is one of common Version Control that we can use for software development. "What is Version Control?". Well, have you ever been in this situation?

Too many version of README

Yes, in fact I think we have already experienced manually do versioning on a document. Of course, our codebase will be very big and manually versioning won't work. Therefore, we use Git to do versioning. Other than versioning, Git also helps you work collaboratively with your teammates πŸ˜„

By reading this post, hopefully you will understand what does Git command mean πŸ‘

Three states of Git

There are 3 states of Git that you need to remember:

  • Modified -> you have changed the file but not yet put to the local database
  • Staged -> you tell Git that these files will be put into local database
  • Committed -> finally the data will be stored into database

These states describes on files that we have tracked (added to database). What if we add new file and we haven't told Git yet? It means the file is Untracked. If you are confused, you can see illustration below (taken from git-scm site):

Lifecycle of Git

Let's take a look at one of example. Assume right now I want to add Black to my project. Here are the steps:

  • Add black to requirements.txt. Now, run git status, you can see Git recognized your changes in requirements.txt and told you that it is not staged yet.

image.png

  • Run git add requirements.txt. See git status. You can see the file is already in staged area and ready to be committed.

image.png

  • Run git commit -m <YOUR_MESSAGE>. Write meaningful messsage for your commit!

image.png

If you run git log, you can see that your commit is saved.

image.png

Good job, now you understand some basics of Git!πŸ‘

Common Git commands

Let's say you want to master other Git commands. Here are some commands that I use regularly:

  • "I want to reset the last commit". Run git reset <PREVIOUS_TWO_HASH>. Confused? Let's say the latest commit has XX hash and before that, the commit hash is YY. In order to reset the XX commit, you need to run git reset YY

  • "I want to revert the last commit and tells Git that I revert the changes". Run git revert <COMMIT_HASH>.

  • "I want to create new branch". Run git branch <NAME_BRANCH>. Check if your branch exists by running git branch -a image.png image.png

  • "I want to delete branch". You can run git branch -D <BRANCH_NAME> image.png

  • "I want to create new branch and move to that branch directly". Run git checkout -b <NEW_BRANCH> image.png

  • "I want to upload my commit to remote repository". Run git push <REMOTE_NAME> <BRANCH>

  • "I want to make my directory updated, the same as branch X". Run git pull <REMOTE_NAME> <BRANCH>

  • "I want to download project from remote repository". Run git clone <REMOTE_URL> image.png

  • "I want to connect my local repository to remote repository". Run git remote add <REMOTE_NAME> <REMOTE_URL>. Here is one of example: image.png

  • "I want to save my work but not yet committed". Run git stash image.png Let's say you want your stash to be in working directory again, run git stash pop image.png

  • "I want to merge branch X to this branch that I'm currently working at!". You can run git merge <branch_name>

Conclusion

That's all some commands that you can use to improve your Git workflow. For more information about Git commands, you can take a look at the documentation. Have fun! πŸ₯³πŸ₯³πŸ₯³

Β