Important Areas of Git that You Should Know as a Developer!

What is Git?

Git is a version control that helps developers to maintain their code. Imagine if suddenly Mark Zuckerberg wants to make Facebook like the first released version, how the developers could find the first version of Facebook? Git is the answer for this case.

Git also helped developers to collaborate with the team. For instance, developers do not need to copy and paste their code when they want to merge it, but they can use git to transfer their code to others.

Github is not Git

Most people may think about Github is Git. In fact, Github is just a version control hosting for git, and this is how each developer can collaborate their code. There are also some git hosting such as, GitLab, Bitbucket, and SourceForge

Areas of git

Git has three important areas, which are Working Directory, Staging, and Repository.

  1. Working Directory = Each file that has been modified in the git folder will be in this area.
  2. Staging = The file can be reviewed in this area before the user does the commit command. The file will be moved here after using the git add command.
  3. Repository = The file will move to this area after the user commits all files in the Staging area using the git commit command.

How to move the file between all areas?

Terminal Hands-on Time!

I have prepared a git folder for you here! Make sure your machine has installed git.

Open terminal or command line, go to the directory that you want, and type this command below.

git clone https://github.com/raaynaldo/git-learn/

Moving files to the Working Directory area

You will get a new folder with 3 files inside, which are car.txt, food.txt, and song.txt. Open your car.txt, type the most favorite car there, and don’t forget to save! Do the same to the food.txt file.

Back to the terminal and type this command below.

git status

Your car.txt and food.txt are in the Working Directory area now. The file with red color means it is in the Working Directory area.

Moving files to the Staging area

Let’s move the file to the Staging area. Type this command in the terminal.

git add food.txt----- or -----git add .

The difference between git add food.txt and git add . is the file that moves to the Staging area. If we use git add . , all files in the Working Directory will move to the Staging area.

Back to the terminal and executegit status .

Your car.txt stays in the Working Directory area and food.txt is in the Staging area now. The file with green color means it is in the Staging area.

Moving files to the Repository area

The last area is the Repository area. Let’s move the food.txt. Using this command below will move everything inside the Staging area to the Repository area.

git commit -m "Add food."

"Add food." means commit comment and it could be everything. However, we should put a clear comment of our commit, so others could understand what files are committed.

Back to the terminal and executegit status .

Because food.txt moved to the Repository area, it will not show anymore in git status.

Moving back from Repository to Staging area

git reset --soft HEAD~1

Moving back from Staging to Working Directory area

git reset food.txt----- or -----git reset . #It will move all files in Staging to Working Directory.

Remove a file from the Working Directory area

git checkout food.txt----- or -----git checkout . #It will remove all files in Working Directory.

Be careful! You will lose all your changes!

Conclusion

These three areas of git are important to understand how git works. Play around with these commands until you are clear and comfortable on how to move and move back to each area, and it will help your understanding of using git.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store