What is Git?

A distributed version control system that records and manages the history of changes to files. Changes edited in the working directory are registered in the staging area with git add, recorded in the local repository with git commit, and sent to a remote repository such as GitHub with git push. Branches allow parallel development and are integrated with git merge. Besides source code, it is also used to manage RTL, verification testbenches, scripts and so on.

Japanese name: Git

Git appears in 6 practice questions across 3 topics, including Linux and Command-Line Basics, Software Development and Project Management, Cloud and Container Basics.

Field: Linux and Command-Line Basics

The three places

Git distinguishes the working directory where files are edited, the staging area (index) that holds the changes to be included in the next commit, and the repository (the .git directory) that stores the commit history. git status shows the differences among these three; git diff shows the difference between the working directory and the staging area, and git diff --staged between the staging area and the latest commit.

Branches and remotes

A branch is a name given to a fork in the commit history, letting new features or fixes proceed without affecting the main branch. git clone copies a remote repository locally, git pull brings in new remote commits, and git push sends your own commits. Merging branches that changed the same place separately produces a conflict that a person has to resolve.

What not to track

Files generated by builds and simulations, logs, large waveform data and the like are written in .gitignore to exclude them from tracking. A file that was once committed stays tracked even if written in .gitignore, so it is removed with git rm --cached. A password or key committed by mistake stays in the history, so the habit of checking the contents with git status and git diff before committing is important.

Try it: questions about Git

After editing main.c, a file managed with Git, only git commit -m "fix" was run, and "no changes added to commit" was shown and nothing was committed. Which of the following is the reason?

  1. ABefore committing, the changes have to be sent to the shared repository with git push
  2. BChanges to be included in a commit must first be registered in the staging area with git add
  3. CGit commits automatically when a file is saved, so there is no need to run git commit
Show answer and explanation

Answer: B. Changes to be included in a commit must first be registered in the staging area with git add

In Git, of the changes edited in the working directory, only those registered in the staging area (the index) with git add are recorded by the next git commit. This two-step mechanism lets you pick only related changes out of several edits and make them one commit. git status shows which changes are staged. There is also git commit -a, which stages all changes to tracked files and commits them at once.

You ran git commit in Git, but the commit does not yet appear in the GitHub repository shared by the team. Which of the following is the reason?

  1. Agit commit only registers changes in the staging area, and git add is needed to record them
  2. BSending committed changes to the shared repository requires running git pull
  3. Cgit commit records in the local repository, and git push is needed to send it to the remote
Show answer and explanation

Answer: C. git commit records in the local repository, and git push is needed to send it to the remote

Git is a distributed version control system in which each developer has the whole repository locally, and git commit records changes in that local repository. To send them to a remote repository such as GitHub, git push is run; conversely, git pull (or git fetch and git merge) brings new remote commits into the local repository. This is why commits can be made without a network connection and pushed together later. If someone else has pushed first, you may be asked to bring in their changes first.

FAQ

What is the difference between Git and GitHub?

Git is the version control software itself, running on your own computer. GitHub is a web service that hosts Git repositories and adds features such as reviews through pull requests and issue management. There are similar services such as GitLab, and servers kept in-house, and Git itself can be used with any of them.

How do I undo the last commit?

If it has not been pushed yet, git commit --amend can fix the last commit and git reset can undo it. For a commit already pushed and shared, it is safer to use git revert, which adds the undoing change as a new commit, so that it does not conflict with other people's histories.

Git in the explanations

Questions that mention Git (6)

  1. After editing main.c, a file managed with Git, only git commit -m "fix" was run, and "no changes added to commit" was shown and nothing was committed. Which of the following is the reason?
  2. You ran git commit in Git, but the commit does not yet appear in the GitHub repository shared by the team. Which of the following is the reason?
  3. "build/" was written in the .gitignore of a project managed with Git, but changes to build/out.log, which was committed earlier, keep appearing in git status. Which of the following is the reason?
  4. In Git, two developers changed the same line of the same file on different branches, and one branch was then merged into the other. Which describes what happens?
  5. After a firmware version known to work correctly there are 1,000 commits, and the latest commit has a bug. The bug was introduced by one commit and occurs in every commit after it. When the culprit commit is identified by binary search, as with git bisect, what is the maximum number of times the firmware must be built and tested?
  6. An environment was created by applying, with a declarative IaC (Infrastructure as Code) tool, a file that defines a network and two virtual machines. Immediately afterwards, the same file is applied again without any changes. Which of the following is what happens?

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Binary search Infrastructure as code (IaC) cron Environment variable File permissions grep Hard link I/O redirection inode Pipe (|)

Back to the glossary