Codes Git

Daily Git Commands You Should Know

“Always work with/surround yourself with people who help make you a better version of you. Kindly avoid those who don’t.”

– Don Roff


Part of being a programmer is by understanding the use of Git, because sometimes the project you do is a collaboration work with the others. It would be devastating if you or somebody else replaced each other’s elegantly-hard-thought code without knowing the changes.

Quoted from the source: “Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.”

It’s a version control system. To make it simple, here are some of the main uses of using Git:

  • No need to copy-paste code changes, eliminating human error on updates.
  • Track the updates made by the programmers in a project, enabling one to review the changes made by the others.
  • Notify update conflicts if a programmer does an update from an outdated version.
  • Rollback changes in a version if necessary.
  • Track multiple versions of updates.
  • Merge some versions to become one version only.

And some other uses I don’t know yet. Though, I survived this far from only this much.


First of all, you need to install Git. Choose a version that is compatible with your computer from this site. If you are using Mac, you can use this command:

sudo apt-get install git

No problems found when installing? Good.

And then, create a new directory where ever you want and name it whatever you want, of course. After that, inside the directory, type the command git init. If everything is well, this kind of response will appear:

Initialized empty Git repository in /Users/<username>/Documents/training/gitex/.git/

The .git directory is hidden. It is important to leave it there, because the magic of git can only happen if the directory exists. In other words, it keeps track of the files and updates you made to the project.

The Commands You Should Know

Add – git add

This command adds a file or many files to be tracked on git. To add a file, just type git add <filename>, for example:

$ git add gitdemo.txt

To add all files inside the project directory, type git add . Yes, type the period (.) too. It saves the time of adding the files one by one.

Commit –git commit

Commit means register an update to the git. It creates a version entry including the message, usually you will write a summary of what the update is all about.

When entering git commit, the terminal will take you to the vim editor, for you to enter the commit message for other project developers to know. However, personally I don’t like using vim editor, so I use this simpler command:

git commit -m <message>

And this is an example of the command:

$ git commit -m "Added file gitdemo.txt!"

You will see this kind of response if it run successfully:

[master (root-commit) 6bbf87b] Add gitdemo.txt
 1 file changed, 1 insertion(+)
 create mode 100644 gitdemo.txt

A Concept You Need to Understand Before Continuing to the Next Commands

Using git add does not mean it’s already registered as a new version. It moves the file from the working directory to staged area. Then, if you enter git commit, the files in the staged area will be registered as part of a new version to the git repository. See the illustration below:

Illustration of git

Now that you understand, let’s continue to the next commands.

Stash git stash

When you want to temporarily save your progress, because maybe you are not done with your current work, but you have to switch to another branch, use the command stash. Upon executing git stash, you will see this kind of response:

Saved working directory and index state WIP on newbranch: 6bbf87b Add gitdemo.txt

Then you can now switch to another branch.

Your life story continues, and some day in the future, you return to the branch you stashed some time ago, to continue doing the work. Type git stash pop to retrieve the temporarily saved files. This kind of response will show if executed successfully:

On branch newbranch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   another_test.txt

Dropped refs/stash@{0} (99a40bb435c2303c3781339884f604b183ccd289)

Checkout – git checkout

Checkout means switching to another branch. And it allows us to freely move to any branch as long as there are no conflict in the modified files in the staging area.

The command is simple: git checkout <branch_name>. To successfully do a checkout, the branch must exist.. Here is a sample of the command:

$ git checkout master
Switched to branch 'master'

The response above means that the command successfully switched to another branch named master from its previous branch.

Another variant of checkout which I often use is git checkout -b <branch_name>. It’s a command to create a new branch based on the current branch where the command is executed.

Suppose you are currently at branchA, and you want to create a branchAB. So, you run git checkout -b branchAB. You will create a branchAB and also automatically switch to branch branchAB, where the version is still the same with branchA. Now, you can modify branchAB without making changes to the original branchA.

View Branches git branch

Show list of created branches in a project. It will show the branches you have in your local git repository. For example, this is the response of executing git branch:

* newbranch

The * in front of a branch name shows which branch you are currently at. It’s good practice to make sure the branch you are working on is the correct one. Just execute git branch.

Here are some other variants of git branch I frequently use:

To create a new branch, execute git branch <file_name>. The difference with git checkout -b <file_name> is that by executing git branch <file_name>, the branch is created but not automatically switched to the new branch.

To get the list of branches in local and all of the remote repositories, execute git branch -a.

To delete a branch, run git branch -D <branch_name>. Note that you can only execute this command when you are not currently at the branch you want to delete.

Merge Branches – git merge

Combine every updates from the other branch to the current active branch. To run this command, simply execute git merge <branch_name>. Suppose you have these branches:

* branch_a

Currently you are in branch_a. So, if you execute git merge branch_c, all the changes in branch_c will be updated to branch_a. In practice, here are some rules to follow:

  • branch_c must be originated from branch_a.
  • Prior to be merged with branch_c, branch_a must not be updated with anything that might possibly clash with the updates from branch_c.

Not following these rules will result in a merge conflict. What’s a merge conflict, you ask? A merge conflict is when there are different statements in the same line or lines in a file between the two branches. Usually it’s caused by two programmers working on the same files and not synchronized properly.

Merge conflicts must be solved manually, requiring the programmer to observe carefully which statement of the code is more necessary, or maybe both are. The example below shows a merge conflict in a file:

const statementA = "This is statement A";
<<<<<<< HEAD
const statementB = "This is statement B";
const statementC = "This is statement C";
const statementB = "This is statement B, isn't it?";
>>>>>>> branch_b

The lines between <<<<<<< HEAD and ======= is the changes you made on the current branch. And the lines between ======= and >>>>>>> branch_b are the changes committed on branch_b.

To solve the conflict, simply compare the two changes and then keep the statements that is important. Remove the unnecessary lines and also remove <<<<<<< HEAD and ======= and >>>>>>> branch_b. It might become something combined from the two branches like this:

const statementA = "This is statement A";
const statementC = "This is statement C";
const statementB = "This is statement B";

Finally, commit the changes if you are confident that everything looks alright.

Clone from a Remote Repository – git clone

I am sure you already know about GitHub, GitLab, Bitbucket, and the likes of them. Those are development platforms which enables teams of programmers to work together and share the code in a project. Or simply: platforms for git repositories.

The git repositories in your computer are local repositories and the ones in the platforms are remote repositories. To continue the work of a project saved in a remote repository, you need to clone the project, which means copying the project to your local repository.

To do this, simply run git clone <source>. For example, if you want to clone the codes from ExpressJS framework, run this code in the directory you choose:

git clone

After some time has passed, the project will be cloned to your computer.

The best thing if you clone a project this way is that you don’t have do a step to link the local repository to the remote repository, because it’s already done automatically.

Pull from a Remote Repository – git pull

Before you start working, it is good practice to update all the latest versions to your local repository. You can do this by switching to the branch you are currently working on or the branch you want to get the updates from, and run git pull. All the updates committed in the remote branch will be merged to your local repository.

Push to a Remote Repository – git push

After you work on your updates, you would want to share it to your team members as a contribution. Because they will update their codes from a single source: the remote repository where you clone and pull the project before. To update an existing branch in a remote repository, simply switch to the branch in your local repository and run git push.

Sometimes, it will fail if there are another updates you have not synchronized from the remote branch. If this happens, run git pull and solve the conflicts if they exist, then run git push again.

The branches you created in your local repository is not automatically add to the remote repository. To add the branch there, run this command:

git push --set-upstream <source> <branch_name>

For example, if you want to push branch_a which is not existing yet in the remote repository, you would run:

git push --set-upstream origin branch_a


Well, that’s the commands I use every day in my work. There are some other commands such as git reflog and git reset, though I don’t use it very often, but it’s also good if you know about it.

At work, I usually mix between using the terminal and GUI such as GitKraken. For example, for some cases such as resolving conflicts, writing a rather detail commit messages, and seeing the commit histories, I use the GUI. On the other hand, for pulling and pushing branches, I use the terminal (because I want to keep my GitKraken free).

But most importantly, just use whatever that works for you, as long as you are comfortable with it.

Let me know if you have any interesting tricks about Git, I’d like to learn more about them. Keep practicing and always live your code, to code your life!

“Then it comes the day when you can’t be your best version… -because such version doesn’t exist.”

– Deyth Banger

By Ericko Yap

Just a guy who is obsessed to improve himself. Working as a programmer in a digital banking company. Currently programming himself in calisthenics, reading books, and maintaining a blog.

Leave a Reply

Your email address will not be published. Required fields are marked *