A beginner's guide to Git version control | Red Hat Developer (2023)

Git is a widely used distributed version control system that allows software development teams to have multiple local copies of the project's source code that are independent of each other. Version control has come to be associated with Git—it is unquestionably the best version control program for new developers to start learning due to its popularity and wealth of resources related to its use. Read on for an overview of the basics.

Git configuration: Linux

Most Linux installations have Git, but to check, execute the following command in your terminal:

$ git --version

You should get output that is similar to the following:

git version 2.40.1

Git configuration: Windows

Git searches the $HOME directory for the .gitconfig file on Windows systems.

We need to tell Git to keep track of our login and email when we use it, as shown in the code snippet below. This makes it possible for other code contributors to identify the change's author and our contact information in case of problems.

$ git config --global user.name "username"$ git config --global user.email "useremail"

Use the following command if you need assistance:

$ git help config

This command will open a browser containing configuration commands. Essentially, the help command gives a manual from the help page for the given command.

You can also use the same command in the following ways:

$ git config --help

Use the following command to view configurations:

$ git config -l

Working with repositories

A directory that Git will track is called a repository,or repo. The Git repository contains the whole of our project. Git will trace any change we make. We'll use the command below to create a test directory:

$ mkdir test

We can then use the following command to enter the test directory:

$ cd test

Running git init command inside the directory lets Git know that it is a Git repository:

$ git initEmpty Git repository created and initialised in /home/uname/test/.git

Now a Git repository exists in this directory. Showing the .git file that Git generated inside the directory will be helpful; use the command as follows:

$ ls -a



The directory is now a Git repository. In fact, deleting the .git directory will uninitialize the repository (this can be very helpful when you're just starting out). Run the following command to make your directory a non-Git repository:

$ rm.git -rf

Use this only if you truly want to start over because it will remove Git from the directory completely.

Let's create two files in the test directory with the names file1 and file2. This will show how Git tracks files:

$ touch file1.txt$ touch file2.txt

Staging files: git add

The principles of the staging environment and the commit are essential to Git. You can add, change, or remove files as you work. However, anytime you reach an important stage or complete a portion of the work, you should move the files to a staging environment. Files that have been staged are ready to be committed to the repository you are working on. (We'll discuss commits in more detail in a later section.)

We can see what Git is tracking using the following command:

$ git statusOn branch mainNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)file1.txt File2.txt

git status displays a list of newly added or modified files and directories in the Git repository. In our example,file1.txt and file2.txt have been modified.

We must stage the files in order to instruct Git to keep track of changes. Let's use the add command to stage file1.txt:

$ git add file1.txt$ git statusOn branch masterNo commits yetChanges to be committed: (use "git rm --cached <file>..." to unstage) new file: file1.txtUntracked files: (use "git add <file>..." to include in what will be committed) file2.txt

Notice that file1.txt is listed under Changes to be committed by Git. This indicates that Git is keeping note of any modifications made to this file in order to commit those modifications to the repository. Let's add file2.txt now:

$ git add file2.txt$ git status...Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: file1.txt new file: file2.txt

You can instruct Git to monitor a file or directory using the add command or to stop tracking a file or directory with the unstaging command.

The git rm command is used to remove individual files or a set of files using the filename, as shown below. The git rm --cached command maintains a file in the working directory while removing it from the Git index.

$ git rm --cached file1.txtrm file1.txt$ git status…Untracked files: ( use "git add <file>..." to include in what will be committed) file1.txt

The file1.txt file is no longer being tracked.

Keeping track of untracked files

We frequently need a quick way to tell Git to add everything that is untracked to the staging area. We can do this by using * or .:

$ git add --all
$ git add .
$ git add *

Stage all changes (new, changed, and deleted) files by using --all rather than specific filenames.


Git uses commits to make changes to files and directories permanent. In a sense, every commit represents a new version of our repository. Even while a commit can be seen to be a more permanent change, Git makes it simple to undo those changes, which is the strength of version control with Git.

For Git users, this alters the fundamental development model. Git developers have the option to build up commits in their local repo before making a change and committing it to the main repository. It accomplishes this by tracking the history of commits. Git's main purpose is to allow users to make commits. Everything we wanted to stage has already been done, so we can now make those modifications.git commit is used to do this.

When using the git commit command, we recommend always including two arguments: -a and -m.

Stage all modified files: git commit -a

Add all untracked files to the staging area with the -a or --alloption. Note that only previously added files and folders are added using this method. Use the add command first if a file or directory has to be added that hasn't already been.

Commit messages: git commit -m

A commit message should always be included when making a commit. The -m or --message option is used for this. This message should be brief and descriptive, with just enough information included in the commit statement to summarize your actions since the last commit.

$ git commit -am "my first git commit"

View change history: git log

To view the history of changes that have been committed to a Git repository, use the git log command:

$ git log

In order to make our output easier to read and only to show the first seven characters of the commit ID, we'll also use the option --oneline command:

$ git log -oneline


cf0p490 (HEAD -> main) my first git commit

In this example, the commit ID's first seven characters are cf0p490. There will be a lot of commits; thus each one needs to have its own identification. We can move the HEAD pointer around as necessary.

Publishing changes: git push

Finally, the git push command is used to upload content from a local repository to a remote repository. Pushing refers to the process of transferring commits from your local repository to a remote repository.

$ git push


$ git push origin branchname

Where to learn more

Explore more Git resources on Red Hat Developer for new and advanced users:

  • Git cheat sheet
  • Git best practices: Workflows for GitOps deployments
  • Protect secrets in Git with the clean/smudge filter
  • How to ignore files in Git without .gitignore


How to do version control with git? ›

To use Git we need to have a project that we want to version control. This can either be a new project or an existing project. If it is a new project, then we need to create a new project folder (hint: we can use the mkdir command) and then navigate into that project folder in the terminal.

How do I use git version control in GitHub? ›

Click Code and copy the HTTPS link. Now open RStudio, click File/ New Project/ Version control/ Git and paste the HTTPS link from the Github repository into the Repository URL: field. Select a folder on your computer - that is where the “local” copy of your repository will be (the online one being on Github).

What are prerequisites to using the git command for version control? ›

  • A command line interface.
  • A text editor of your choice (I will be using VS Code).
  • A GitHub account.
Nov 5, 2021

Is Git the same as version control? ›

Version Control Systems (VCS) have seen great improvements over the past few decades and some are better than others. VCS are sometimes known as SCM (Source Code Management) tools or RCS (Revision Control System). One of the most popular VCS tools in use today is called Git.

Should I use Git for version control? ›

Git has workflow flexibility

With Git, teams can work together using various branching strategies that are not as possible in other version control systems. Git's branching capabilities offer users the ability to select a workflow depending on the size of a project or team or unique processes.

How do I use version control? ›

by Michael Ernst
  1. Use a descriptive commit message.
  2. Make each commit a logical unit.
  3. Avoid indiscriminate commits.
  4. Incorporate others' changes frequently.
  5. Share your changes frequently.
  6. Coordinate with your co-workers.
  7. Remember that the tools are line-based.
  8. Don't commit generated files.

How to use the Git commands? ›

Git commands
  1. git add. Moves changes from the working directory to the staging area. ...
  2. git branch. This command is your general-purpose branch administration tool. ...
  3. git checkout. ...
  4. git clean. ...
  5. git clone. ...
  6. git commit. ...
  7. git commit --amend. ...
  8. git config.

What's the difference between Git and GitHub? ›

what's the difference? Simply put, Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories.

How to check version using Git? ›

Check your version of Git

You can check your current version of Git by running the git --version command in a terminal (Linux, macOS) or command prompt (Windows). If you don't see a supported version of Git, you'll need to either upgrade Git or perform a fresh install, as described below.

How to do version control? ›

by Michael Ernst
  1. Use a descriptive commit message.
  2. Make each commit a logical unit.
  3. Avoid indiscriminate commits.
  4. Incorporate others' changes frequently.
  5. Share your changes frequently.
  6. Coordinate with your co-workers.
  7. Remember that the tools are line-based.
  8. Don't commit generated files.

How Git is distributed version control? ›

Git is a distributed version control system known for its speed, workflow compatibility, and open source foundation. With Git, software teams can experiment without fearing that they'll create lasting damage to the source code. Teams using a Git repository can tackle projects of any size with efficiency and speed.

How do I add files to Git version control? ›

To add and commit files to a Git repository

Create your new files or edit existing files in your local project directory. Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed.

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated: 30/11/2023

Views: 5946

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.