Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

A beginner's guide to Git version control

August 2, 2023
Mohammadi Iram
Related topics:
Open source
Related products:
Red Hat OpenShift

    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:

    • 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

    Related Posts

    • Git best practices: Workflows for GitOps deployments

    • Why should developers care about GitOps?

    • Test GitHub projects with GitHub Actions and Testing Farm

    • How to set up your GitOps directory structure

    • Separating IDE workspaces from code repositories

    Recent Posts

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    What’s up next?

    Getting GitOps e-book card

    It's a jungle out there. Learn how to navigate the complex world of modern container-based software development and distribution with Getting GitOps: A Practical Platform with OpenShift, Argo CD, and Tekton.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.