Connect to the Repository

Cloning a repository

Git’s advantage as a distributed source control system is its ability to clone a copy of the repository locally. Having a local copy of the repository gives you complete autonomy.

Use the git clone command to clone the repository you created. The clone command requires the <GitHub clone URL>.

Clone the repository by following these steps:

  1. Click Clone or Download, then click the copy or clipboard button.
  1. Use the git clone command in the terminal, replace the <Github clone URL> with the repository’s URL.
Press + to interact
# Replace the <GitHub clone URL> with the repository's URL.
git clone <Github clone URL>
  1. Change into the repository’s directory using the command below:
Press + to interact
# Change into the ansible directory
cd ansible
  1. List all the contents of the directory and show hidden files using the command below for the bash shell:
Press + to interact
# List all the contents of the current directory
ls -a

You can use the following command for listing the contents in PowerShell:

Press + to interact
Get-ChildItem -Force

Notice the .git directory. The .git folder makes it a Git repository. It was added when you created the repository using GitHub’s web interface.

Tracking changes

Git does not automatically commit your changes. You are responsible for staging and committing the changes. Git is aware of the changes, but you have to tell it which changes it should care about.

When you create new files, they start in the working area. Git does not track changes to these files, but it is aware they were created. The working area is also referred to as “untracked.”

Let’s dive into how to track changes step by step.

  1. Create a new file within the ansible repository by using the following command:
Press + to interact
# Bash
touch ansible.sh
# PowerShell
New-Item -Name ansible.sh
  1. View untracked changes in the working area using the command:
Press + to interact
git status

The git status command’s output informs you that there is one untracked file, ansible.sh. Review the Line-5 of the output below:

Press + to interact
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# ansible.sh
nothing added to commit but untracked files present (use "git add" to track)
  1. Add an ad-hoc Ansible command to ansible.sh:
Press + to interact
echo "ansible localhost -m ping" > ansible.sh
  1. View git changes using the command:
Press + to interact
git status

The git status command returns the same output as before. That’s what untracked means. Git is simply aware that the file exists but does not keep any information on that file’s changes.

Staging changes in Git

To keep track of the changes to a file, you have to move it from the working area to the staging area.

You can stage the changes to the ansible.sh file using the git add command. Explore the steps below,

  1. Track changes to ansible.sh:
Press + to interact
git add ansible.sh
  1. View git changes:
Press + to interact
git status

The output displays that the file ansible.sh is now being tracked and is in the staging area.

Press + to interact
On branch master
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: ansible.sh
  1. Modify ansible.sh, add another ad-hoc command:
Press + to interact
echo "ansible localhost -m file -a \"path=ansible.txt state=touch\"" >> ansible.sh
  1. View git changes:
Press + to interact
git status

The output now shows a tracked change and an untracked change. Git has the first change in the staging area and the second change, which is untracked, in the working area.

Press + to interact
On branch master
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: ansible.sh
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: ansible.sh
  1. Show the difference between changes. Use the git diff command:
Press + to interact
git diff ansible.sh

By using the git diff command you can see the differences in the working area and staging area versions.

Press + to interact
diff --git a/ansible.sh b/ansible.sh index 75c6cb5..73a8a9a 100644
--- a/ansible.sh
+++ b/ansible.sh
@@ -1 +1,2 @@
ansible localhost -m ping
+ansible localhost -m file -a "path=ansible.txt state=touch"
  1. Add the second changes to the staging:

You can use the git add command to stage the changes.

Press + to interact
git add ansible.sh

Committing changes

The power of source control is having a rich history to audit and rollback to. Commits make up that rich history.

Before you can audit or roll back to a change, it needs to be committed. When you commit a change, it moves from the staging area into the repository. Commit requires a commit message. You can make the history rich and informative through meaningful, thoughtful, and concise commit messages.

Use the git commit command to commit the changes to the local repository.

  1. Commit the staged change to the local repository. Use -m to leave a commit message.
Press + to interact
git commit ansible.sh -m 'added localhost commands'

You will come across the following output:

Press + to interact
** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.

Tracking changes isn’t enough. You also need to know who made the changes. Git’s configuration is used to provide the user name and email associated with the person who committed to the repository.

  1. Add user.email to the git config, replace <YourEmail> with your email.
Press + to interact
# Replace <YourEmail> with the actual email
git config --global user.email "<YourEmail>"
  1. Add user.name to the git config, replace <YourName> with your name.
Press + to interact
# Replace <YourName> with the actual name
git config --global user.name "<YourName>"
  1. Add the ansible.sh to the staging area again. Use the git add command to stage the changes.
Press + to interact
git add ansible.sh
  1. Commit the changes.
Press + to interact
git commit ansible.sh -m 'added localhost commands'

Your change has been committed to the local repository with the commit message you provided.

The output of the command will look like the one below:

Press + to interact
[master 0d57bf4] added ping localhost command
1 file changed, 2 insertions(+)
create mode 100644 ansible.sh

Visualize the Git workflow below.

Pushing changes

The final step is to push your committed changes back to the centralized repository on GitHub. Pushing your changes makes them available for others to retrieve and also serves as a backup.

  1. Push local commits.
Press + to interact
git push
  1. When prompted, enter your Github username and password.

Personal Access Token
If you have two-factor authentication enabled, you’ll have to use a personal access token(PAT) instead of a username and password. You will have to use PAT in the future as the password-based authentication will go obsolete. Learn more here.

  1. Get the status of the repository.
Press + to interact
git status

The commits you made to your local repository are now available on GitHub.

Review the output of the git status command below:

Press + to interact
# On branch master
nothing to commit, working directory clean

Practice the commands in the terminal.

We have provided a summarized view of the commands below:

Press + to interact
# Replace the <GitHub clone URL> with the repository's URL.
git clone <Github clone URL>
# Change into the ansible directory
cd ansible
# List all the contents of the current directory
ls -a
# Create a file
touch ansible.sh
# Get the status of the repository
git status
# Stage Changes
git add ansible.sh
# Get the status of the repository
git status
# Update the file
echo "ansible localhost -m file -a \"path=ansible.txt state=touch\"" >> ansible.sh
# Get the status of the repository
git status
# Get the difference between the staged and working area versions.
git diff ansible.sh
# Stage the new changes
git add ansible.sh
# Commit the changes
git commit ansible.sh -m 'added localhost commands'
# Configure user.email
# Replace <Your email> with the actual email
git config --global user.email "<YourEmail>"
# Configure user.name
# Replace <Your name> with the actual name
git config --global user.name "<YourName>"
# Stage Changes
git add ansible.sh
# Push the commits
git push
# Get the status of the repository
git status
Terminal 1
Terminal
Loading...

In this lesson, we introduced the essential git commands. Review the commands below:

  • clone: To clone the remote GitHub repository.
  • status: To get the current status of the repository.
  • add: To stage the changes in the repository.
  • diff: To get the difference between the staging area and working area versions.
  • commit: To commit the staged changes to the local repository.
  • push: To push the committed changes to the GitHub repository.

Get hands-on with 1300+ tech skills courses.