A code editor with four icons symbolizing DevOps, developers, a gear, and a cluster.

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 init 

Empty 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

Output:

.git

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 status

On branch main

No commits yet

Untracked 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 status
On branch master
No commits yet
Changes to be committed:
   (use "git rm --cached <file>..." to unstage)
        new file: file1.txt
Untracked 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.txt
rm 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.

Commits

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 --all option. 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

Output:

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

Or:

$ git push origin branchname

Where to learn more

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