Connect to the Repository
Connect to the GitHub Repository.
We'll cover the following
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:
- Click Clone or Download, then click the copy or clipboard button.
- Use the
git clone
command in the terminal, replace the<Github clone URL>
with the repository’s URL.
# Replace the <GitHub clone URL> with the repository's URL.git clone <Github clone URL>
- Change into the repository’s directory using the command below:
# Change into the ansible directorycd ansible
- List all the contents of the directory and show hidden files using the command below for the
bash
shell:
# List all the contents of the current directoryls -a
You can use the following command for listing the contents in PowerShell
:
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.
- Create a new file within the
ansible
repository by using the following command:
# Bashtouch ansible.sh# PowerShellNew-Item -Name ansible.sh
- View untracked changes in the working area using the command:
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:
# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## ansible.shnothing added to commit but untracked files present (use "git add" to track)
- Add an ad-hoc Ansible command to
ansible.sh
:
echo "ansible localhost -m ping" > ansible.sh
- View git changes using the command:
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,
- Track changes to
ansible.sh
:
git add ansible.sh
- View git changes:
git status
The output displays that the file ansible.sh
is now being tracked and is in the staging area.
On branch masterChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: ansible.sh
- Modify
ansible.sh
, add another ad-hoc command:
echo "ansible localhost -m file -a \"path=ansible.txt state=touch\"" >> ansible.sh
- View git changes:
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.
On branch masterChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: ansible.shChanges 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
- Show the difference between changes.
Use the
git diff
command:
git diff ansible.sh
By using the git diff
command you can see the differences in the working area and staging area versions.
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"
- Add the second changes to the staging:
You can use the git add
command to stage the changes.
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.
- Commit the staged change to the local repository. Use
-m
to leave a commit message.
git commit ansible.sh -m 'added localhost commands'
You will come across the following output:
** Please tell me who you are.Rungit 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.
- Add
user.email
to the git config, replace<YourEmail>
with your email.
# Replace <YourEmail> with the actual emailgit config --global user.email "<YourEmail>"
- Add
user.name
to the git config, replace<YourName>
with your name.
# Replace <YourName> with the actual namegit config --global user.name "<YourName>"
- Add the
ansible.sh
to the staging area again. Use thegit add
command to stage the changes.
git add ansible.sh
- Commit the changes.
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:
[master 0d57bf4] added ping localhost command1 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.
- Push local commits.
git push
- 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 usePAT
in the future as the password-based authentication will go obsolete. Learn more here.
- Get the status of the repository.
git status
The commits you made to your local repository are now available on GitHub
.
Review the output of the git status
command below:
# On branch masternothing to commit, working directory clean
Practice the commands in the terminal.
We have provided a summarized view of the commands below:
# Replace the <GitHub clone URL> with the repository's URL.git clone <Github clone URL># Change into the ansible directorycd ansible# List all the contents of the current directoryls -a# Create a filetouch ansible.sh# Get the status of the repositorygit status# Stage Changesgit add ansible.sh# Get the status of the repositorygit status# Update the fileecho "ansible localhost -m file -a \"path=ansible.txt state=touch\"" >> ansible.sh# Get the status of the repositorygit status# Get the difference between the staged and working area versions.git diff ansible.sh# Stage the new changesgit add ansible.sh# Commit the changesgit commit ansible.sh -m 'added localhost commands'# Configure user.email# Replace <Your email> with the actual emailgit config --global user.email "<YourEmail>"# Configure user.name# Replace <Your name> with the actual namegit config --global user.name "<YourName>"# Stage Changesgit add ansible.sh# Push the commitsgit push# Get the status of the repositorygit status
In this lesson, we introduced the essential git commands
. Review the commands below:
clone
: To clone the remoteGitHub
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 theGitHub
repository.
Get hands-on with 1300+ tech skills courses.