Introduction to Git Controls

Hanbo
Terra
Published in
5 min readMay 1, 2023

Git is a powerful version control system that allows developers to efficiently manage changes to their code. Whether you’re working on a small personal project or contributing to a large collaborative project, understanding the basics of Git is essential. In this article, we’ll cover some of the basic Git controls that every developer should know.

In this series of articles, we’ll explore the world of Git controls from the command line interface, starting with the basics and working our way up to more advanced commands. Whether you’re a seasoned developer or just getting started, understanding Git controls is essential for managing your code effectively. So, let’s dive in and learn how to make the most of Git!

What is Git?

Git is a distributed version control system that was created by Linus Torvalds in 2005. It is designed to handle everything from small to very large projects with speed and efficiency. Git allows developers to work collaboratively on the same codebase without worrying about conflicts or overwriting each other’s changes.

Git tracks changes to your code over time and allows you to easily roll back to a previous version if something goes wrong. It also provides tools for branching and merging, allowing you to experiment with new features or fix bugs without affecting the main codebase.

Basic Git controls

Here are some of the most basic Git controls that every developer should know:

Probably the most important Git Command

One of the most important Git controls you’ll use is git status. This command tells you the current status of your working directory, including which files have been modified and which changes have been staged for commit.

>>> git status
On branch develop
Your branch is up to date with 'origin/develop'.

nothing to commit, working tree clean

This will show you the current status of your working directory, including which branch you’re on, any changes that have been made, and which files have been staged for commit.

If you’ve made changes to your code that you want to commit, you’ll need to stage those changes using the git add command (see the sections below). Once you've added your changes, you can use git status again to see which files have been staged:

This will show you which files have been staged and which changes are still waiting to be committed.

By regularly using git status, you can keep track of your changes and ensure that your code is always in a clean and stable state. It's a simple yet powerful tool that every developer should have in their Git toolkit.

Clone a repository

The first step in working with Git is to clone a repository. Cloning creates a local copy of the remote repository on your computer that you can work on.

To clone a repository, use the git clone command followed by the URL of the remote repository:

git clone https://github.com/username/repository.git

Stage changes

Once you have made changes to your code, you need to stage them before committing. Staging means that you are selecting which changes you want to include in the next commit.

To stage changes, use the git add command followed by the file or files you want to stage:

git add file1.txt file2.txt

You can also stage all changes at once using:

git add .

Commit changes

Once you have staged your changes, you can commit them to the repository. A commit is a snapshot of your code at a specific point in time.

To commit changes, use the git commit command followed by a commit message that describes the changes you made:

git commit -m "feat(parser): add ability to parse arrays"

Note that it would be very helpful if your commit messages follow the standard git commit message convention.

Push changes

Once you have committed your changes, you need to push them to the remote repository so that other developers can access them.

To push changes, use the git push command followed by the name of the remote repository and the branch you want to push to:

git push origin main

When you push your changes to a remote repository using the git push command, Git will automatically merge your changes with the existing code on the remote repository. However, in some cases, you may need to force push your changes using git push -f (or --force flag) to overwrite the existing code on the remote repository.

It’s important to use git push -f with caution, as it can potentially cause conflicts and data loss if other developers have made changes to the remote repository since your last push. You should always communicate with your team members before force pushing your changes, and make sure that everyone is on the same page to avoid conflicts.

If you do need to force push your changes, you can use the following command:

git push -f <remote-name> <branch-name>

This will overwrite the code on the remote repository with your changes, potentially losing any changes that were made by other developers in the meantime.

In general, it’s best to avoid force pushing your changes unless it’s absolutely necessary. Instead, you can use other Git controls like git rebase or git merge to merge your changes with the existing code on the remote repository in a more controlled way.

Pull changes

If other developers have made changes to the code since you last pulled, you will need to pull those changes before you can push your own changes.

To pull changes, use the git pull command:

git pull

Using git pull --rebase instead of git pull will apply your local changes on top of the updated code, resulting in a cleaner commit history. This command essentially rewinds your local repository to the point where it diverged from the remote repository, applies the changes from the remote repository, and then applies your local changes on top.

To use git pull --rebase, simply type the following command in your terminal:

git pull --rebase

This will fetch the changes from the remote repository and apply them to your local repository, then apply your local changes on top.

Create a branch

Branching allows you to work on new features or bug fixes without affecting the main codebase.

To create a new branch, use the git branch command followed by the name of the new branch:

git branch new-feature

To switch to the new branch, use the git checkout command followed by the name of the branch:

git checkout new-feature

To list all the branches, you can use the following command:

git branch -a

You can then press ‘q’ and enter to exit the command.

Merge branches

Once you have finished working on a new feature or bug fix, you can merge your changes back into the main codebase.

To merge a branch, switch to the branch you want to merge into and use the git merge command followed by the name of the branch you want to merge:

git checkout main
git merge new-feature

Conclusion

To wrap up this article, we’ve explored the basics of Git controls from the command line interface, which will serve as a solid foundation for managing your code using Git. By mastering these controls, you’ll be able to effectively track changes to your code, collaborate with other developers, and manage your repositories more efficiently. In the upcoming articles, we’ll dive into more advanced Git techniques and examples to help you take your Git skills to the next level. So stay tuned for more!

--

--