Page
Introduction to Git and GitHub
While working with Ansible content, you will use a version control system to manage your automation code. Git is one such widely used version control system. Let's take a look at how it can be used to manage Ansible content.
In this lesson, you will:
- Learn about key concepts of Git.
- Learn how to install and configure Git on your environment.
What is Git?
Git is a widely used, open source version control system to track code changes and manage code or configuration files efficiently. It is an essential tool for maintaining the integrity of your code over time. When an error occurs in your code, Git makes it easier for you to roll back changes quickly which helps keep your systems operating reliably. It also enables teams to work on projects simultaneously without introducing conflicts in the source code.
What is GitHub?
GitHub is a cloud-based platform where teams can store, share, and collaborate to write code. It provides additional enterprise-level features like issue tracking, project management, and more. Git commands that are used locally on your machine to manage your local Git repository will work the same on GitHub, but you can use additional GitHub-specific commands or features to interact with the remote repository on GitHub's servers.
If your organization uses GitHub, you can explore their documentation for more information.
Key concepts
Let's explore some key terminology and concepts used with Git:
- A
Repository
is a collection of files and folders tracked by Git. It stores the entire history of changes made to those files. Modified
files have been changed, but not committed to your database.Staging
, also known as the index, is a space where you can prepare changes before committing them to the repository. Files in the staging area have been marked for inclusion in the next commit.- A
Commit
represents a snapshot of changes to the files in a repository at a specific point in time. Each commit has a unique identifier and a commit message describing the changes. Branches
support the creation of new features or fixes in isolation from the main codebase. Branches enable parallel development and experimentation.Merging
combines changes from one branch into another. It's used to integrate changes made in feature branches back into the main codebase.Pull Requests
: (also known as merge requests) are proposals to merge changes from one branch into another. They facilitate code review and collaboration among team members.
Installing Git
Most Linux and macOS installations have Git by default, but not Windows. You can confirm installation by typing the following command in your terminal window:
$ git --version
You should receive output that is similar to the following:
git version 2.44.0
If Git is not installed, you can download it from the official website, git-scm.com, and follow the instructions for your operating system.
If you are a RHEL user and don't have Git installed, click here for more information on how to download the supported component from our Red Hat Software Collections.
Configuring Git
Type the following in your Windows command prompt, which will make it possible for you and other code contributors to associate you with your commits:
$ git config --global user.name "yourname"
$ git config --global user.email "youremailaddress"
To confirm that Git has been configured correctly, you can use the following command to display your configured name and email:
$ git config --global --list
The following command will open a browser containing other configuration commands:
$ git help config
Or you can use:
$ git config -l
Commonly used Git commands
Create a new repository in the current directory:
git init
Add files to staging:
git add <file>
Commit changes with a descriptive message:
git commit -m "Commit message"
Push changes from local repository to default repository:
git push
View changes to a file:
git diff <file>
Create a branch:
git branch <branch-name>
Switch branches:
git checkout <branch-name>
Merge branches:
git merge <branch-name>