Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Published
3 min read
Git for Beginners: Basics and Essential Commands

What is Git?

Git is a tool used to track changes in your code. It is termed a Distributed Version Control System (DVCS) because it allows multiple developers to collaborate on a project, track changes, and switch back to previous versions if needed.

Why is Git used?

When working on projects, we need to keep track of the changes and versions, this is where Git comes into the picture. If we complete one feature, push it to the repository, and then start working on another feature, if the feature breaks any changes and we need to go back to the previous commit, we can do it easily.

In teams, it allows us to collaborate. Git allows us to add a local copy of the project to our system, work on it offline, and then, when the work is done, sync it with the central repository.

Core Terminologies

  • Repository: A repository is a database, either local or on a central server, that saves the project files, folders, changes, etc.

  • Working Directory: It is the directory where we are working on the project.

  • Staging: Staging area is where changes are prepared for committing into the repository.

  • Commit: A snapshot of the staged changes at a specific point of time. Every commit has a unique identifier (SHA) and a commit message.

  • Branch: A branch is a separate line of code used for adding new features, fixing bugs, etc. It is separate from the main codebase and can be later merged into main.

  • HEAD: It is a reference to the last commit in the current branch.

  • Main/Master: It is a default branch where there is production-ready code.

Common Git commands

git init is used to initialize a Git repository in our project. It adds a .git hidden directory inside our working directory where everything is stored.

git add <filename> is used to commit a specific file into the staging area.

git add . is used to add all the files of the project into the staging area.

git commit -m <message> is used to commit the staging area changes into the repository with a message.

git status shows the status of the changes as untracked, modified, or staged.

git log displays all logs of commits in the current working branch history.

git branch list all local branches.

git branch <branch-name> is used to create a new branch.

git switch <branch-name> is used to switch to another branch.

There are many more Git commands, you can read about them here.

Practically using Git inside our project

  1. Open any project inside your favorite code editor.

  2. Initialize Git repository with git init

  3. Make changes and add these changes to staging area with git add .

  4. Finally push changes into repository with git commit -m “Initial commit”

Congratulations, you have used Git inside your own project.

Conclusion

In this article we understood what Git is, why it is used in modern software development, and the core terminologies of Git, like “repository,” “commit,” “branch,” etc. Also, we learned about some common Git commands, and we used it practically in our project. With the basic understanding of Git, you can now confidently manage your code and work efficiently in a team. Now the best way to learn it thoroughly is by practicing it regularly in your projects.

More from this blog

M

Mursaleen's Blog

13 posts