Mastering Git commands 👩💻
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?
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):
Let's take a look at one of example. Assume right now I want to add Black to my project. Here are the steps:
requirements.txt. Now, run
git status, you can see Git recognized your changes in
requirements.txtand told you that it is not staged yet.
git add requirements.txt. See
git status. You can see the file is already in staged area and ready to be committed.
git commit -m <YOUR_MESSAGE>. Write meaningful messsage for your commit!
If you run
git log, you can see that your commit is saved.
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
"I want to delete branch". You can run
git branch -D <BRANCH_NAME>
"I want to create new branch and move to that branch directly". Run
git checkout -b <NEW_BRANCH>
"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>
"I want to connect my local repository to remote repository". Run
git remote add <REMOTE_NAME> <REMOTE_URL>. Here is one of example:
"I want to save my work but not yet committed". Run
git stashLet's say you want your stash to be in working directory again, run
git stash pop
"I want to merge branch X to this branch that I'm currently working at!". You can run
git merge <branch_name>
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! 🥳🥳🥳