Visualising basic git commands: Part 1

Prafulla Ashtikar
6 min readFeb 19, 2023

Git is a popular version control system used by software developers to track changes in their code. It allows you to manage your codebase in an organised and efficient manner.

Often, developers use common git commands like git add, git commit and git push/ pull, etc on a day to day basis but are not sure how things really work in the background. Sometimes, git newbies get stuck when there is a conflict or they do a small mistake in the git commands and end up deleting the branch and re creating a new one and hence not using git effectively.

git is powerful, if used effectively it can save a lot of your time.

In this blog, we will visualise some of the basic commands of Git.

Basic work flow

A remote repository is stored on the cloud service, such as GitHub, and allows you to share your code with others and collaborate on projects. A local repository is stored on your own computer.

  1. git clone :
git clone

You can clone/copy an existing repository in a new directory. In the above diagram, the circles represent commits. When you do a git clone, you copy the whole repository as it is to your computer. ( A new directory gets created with the same name as the original). So you get the exact replica of what exists in the original repository now into your new directory.

2. git pull :

git pull

The git pull command is used to retrieve latest changes from a remote repository into your local branch. The git pull in the above diagram copies the new commit to your local repository. Actually, ‘git pull’ does more than just ‘pulling’ the latest changes, however, we will deal with that in the part 2 of this blog series.

3. git branch :

git branch

A branch is where you can work independently without impacting the main/ master branch. The git branchcommand will create a new branch B1 in above diagram. It will create a new pointer on the latest commit. This command will only create a branch, it won’t switch to that branch, you are still on the master branch. To switch to a new branch we use git checkout.

git branch B1
git checkout B1

The following command is the combination of git branch and git checkout, meaning it will create a new branch B1 and also switch to this new branch.

git checkout -b B1

4. git add and git commit :

git add and git commit

Now that we have our new branch ready to safely play on without affecting the master branch, we can do our code changes and add and commit them. When you do a git add, you send the code to a staging area ( Refer to the first diagram of Basic work flow).

The staging area is a place to keep whatever needs to go in the next commit. When you make changes to your code, you use the git add command to stage those changes.

When you are done with adding your changes, you can finally make a commit using the git commit command. This creates a new snapshot of your code and stores it in your local repository. Commits are like checkpoints in your code's history, allowing you to go back to a previous version if needed. You can make as many commits you want to before pushing. You can also add a message to your commit.

git commit -m "Your message to decribe the commit"

As a best practice, make smaller and multiple commits throughout the development of the task you are on. This way, you can easily revert to your earlier version if something goes wrong.

5. git push:

git push

The git push command is used to send changes from your local repository to a remote repository. This is useful when you have completed a feature or made significant changes and want to share them with others. In the diagram above, there are 3 commits on the local branch B1. With the following command, all the commits are copied to a new branch B1 in remote repository.

git push --set-upstream origin B1

Now that you have your commits in branch B1 on remote repository, you can raise a pull request and get approvals from your team if needed and then merge to remote master. You can select one of the options as shown in the below screenshot.

Let us look at each of these options :

  • Create a merge commit : All commits on B1 will get added to remote master. An additional commit called as ‘merge commit’ gets added in this case to the remote master.
git merge
  • Squash and merge : All the commits in B1 will be squashed together as one single commit and then merged to remote master.
  • Rebase and merge : Imagine, before you merge, someone gets an approval on her PR and merges to the remote master. This is shown as a triangle in the diagram below. In that case, you can select ‘rebase and merge’ option to rebase your commits on top of that commit (notice that all of this is happening on the remote repository) and then merge to remote master.

You can also do a rebase in your local branch in the terminal. Refer the diagram below. To do that you would need to pull the latest master first and then go to your branch and rebase. You would now need to “forcefully” push B1 to remote repository because of the rebase. So the commands would be:

git checkout master
git pull
git checkout B1
git rebase master
git push origin B1 --force

As a best practice, rebase locally first, force push and do a squash and merge to master.

Conclusion

Git is a powerful tool for managing code and collaborating with others. By understanding and visualising the basics of Git, you can use Git more efficiently to track changes to your code and work with others on projects. Git has a lot more goodness, this is just the beginning! We will dig further in the next (upcoming) blog.

--

--