Featured image of post How Git Works and Common Git Commands

How Git Works and Common Git Commands

Do testers need to know Git? Since test automation requires it, Git is an essential skill too.

Preface

As an ambitious test engineer, with agile practices now in full swing, you need to work closely with developers. Tasks such as supporting unit testing and integration testing require you to confront the development code directly. Moreover, the test automation you build is itself code, so code management has in fact become an indispensable skill for testers today.

And in the course of this love-hate relationship with developers, diving straight into the code repository to see what shady dealings have been written that dare not face testing is also an advanced technique for in-depth testing.

But the first hurdle here is dealing with the code repository. So in this article we will take a look at today’s most mainstream code branch management tool, Git, and how to operate a code repository in the right way.

What Is Git?

Git was created by the celebrated Linus Torvalds, universally known as the famous father of Linux.

Although Linux achieved tremendous success and attracted a large number of developers to the project, in the early days, as the manager of the Linux project, Linus still merged everyone’s code mainly by hand, without the help of any code management tool.

It was not that Linus liked this approach, but rather that he was extremely picky about code version management tools; commercial ones like clearcase and open source ones like CVS, SVN, and so on all failed to meet his expectations for code branch management.

It was not until 2002 that Linus settled on a commercial distributed version control system, BitKeeper; and given Linux’s enormous appeal, BitKeeper was willing to let the Linux community use it for free. But in 2005, a community developer reverse-engineered BitKeeper without permission and made use of its undisclosed interfaces, which led BitKeeper to revoke the community’s free usage rights.

Back to square one, Linus then decided to develop his own code version control system. The basic requirements for this system were:

  • Speed
  • Simple design
  • Support for a large number of parallel development branches
  • Fully distributed
  • The ability to efficiently manage very large-scale projects like the Linux kernel

Then, with the efficiency of a true master, Linus took only 10 days to release the first version of Git. And he gave this version a slightly mocking name — Git (in English slang it means “an unpleasant person”).

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your way)

Of course, after Linus released the first version of Git, other interested members of the community (Junio Hamano) began to participate and gradually took over Git’s day-to-day maintenance, while Linus continued to devote most of his energy back to maintaining the Linux kernel.

Git vs SVN

Before Git, the most popular code version management tool was probably SVN.

It is not that SVN has major flaws, but the application scenarios of Git and SVN do differ considerably. Here is a table for comparison

FeatureGitSVN
BranchLightweight branches, fast to createA branch is a full copy of the repository
Branch mergingFast-forward merges, fast mergingManual merging of changes required
DistributedYes, designed to be distributedNo, no full distribution support
SnapshotStores snapshots of changesStores file differences
SpeedFast, distributed designSlow, centralized design
Access controlControlled via Git Hooks and GitoliteControlled via hook scripts
Learning curveSteeper, requires understanding commands and conceptsGentler, easy to get started
TagsLightweight tags, can be modified laterImmutable static tags
HistoryRetains full historyRetains only recent history
Merge conflictsRequire manual resolutionAutomatically attempts resolution, manual intervention when conflicts arise
Command-line toolYes, a powerful and flexible Git CLIRelatively simple, mainly used to manage and operate the repository

Overall, Git is now more popular and its surrounding ecosystem is more mature, so it has gradually become the most mainstream version control tool today

How Git Works

So how exactly does Git work?

Architecturally, Git is actually a three-layer architecture,

  1. Working directory: This is the actual directory where the project files reside. Modifications made to files in the working directory are considered to be in an “untracked” state until an explicit commit operation is performed.
  2. Staging area: The staging area acts as a buffer zone between the working directory and the .git directory. Files in the staging area are in a “staged” state so that they can be included in the next commit. It is the means that allows you to select and control changes.
  3. Local repository: That is, the .git directory. It is the core container of the Git version control system, containing various metadata, the object database, and configuration information. The .git directory stores the snapshots of commits, manages the project’s history, and accepts Git’s various operational commands.

In addition to these three layers of local code management, there is also the concept of a remote repository, used for collaboration among different members on the same project code.

  1. Remote repository: A remote repository is a central repository for team collaboration, ensuring code synchronization and coordination through push, pull, merge, and other operations.

Combining the architecture above with the main Git operation commands, let us understand how Git works:

  1. First, we use git init or git clone to set up a local Git repository, which will contain the .git local repository directory.
  2. Code newly added or modified locally is at this point actually in the untracked state
  3. Running the git add . command causes the relevant changes to enter the staging area, with the corresponding state being staged or modified
  4. Then running the git commit command submits the changes from the staging area into the local repository, with the corresponding state being committed
  5. git diff is the command for viewing changes; as you can see, it actually compares the difference between the working directory and the staging area, while git diff HEAD compares the local repository with the working directory
  6. git push submits the contents of the local repository to the remote repository
  7. git fetch is used to synchronize the remote repository to the local repository
  8. The git pull we commonly use directly pulls the remote repository down into the local working directory

Common Git Commands

Here I borrow a Chinese-language Git cheatsheet for quick reference.

The detailed usage of the commands will not be listed here; the core commands are mainly just the few mentioned in the schematic diagram above.

For further study, you can also make use of online learning tools. Here is one I recommend:

https://learngitbranching.js.org/

Built with Hugo
Theme Stack designed by Jimmy