Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A beginner's guide to Git version control

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

Share:

    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

    • Why Models-as-a-Service architecture is ideal for AI models

    • How to run MicroShift as a container using MINC

    • OpenShift 4.19 brings a unified console for developers and admins

    • 3 steps to secure network segmentation with Ansible and AWS

    • Integrate vLLM inference on macOS/iOS using OpenAI APIs

    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

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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

    Red Hat legal and privacy links

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

    Report a website issue