Software Development and Design · Topic 6 of 7
Version Control and Everyday Git Commands
Learn why version control matters and how to use Git clone, add, rm, commit, push, pull, status, log and diff to track and share changes to code.
- 4 min
- Git is distributed, so each clone holds the full history.
git clonecopies a repository andgit initcreates a new one.git addstages changes andgit commitsaves them locally.
What it is
Version control is a system that records every change to a set of files so that you can review, compare and restore earlier versions.
Git is the most widely used version control system. It is distributed, which means every developer has a full copy of the project history on their own machine.
Network engineers use Git to track device templates, scripts and configuration files in the same way developers track source code.
How it works
Git moves changes through three areas, and a few commands move them from one to the next.
Advantages of version control
- History. You can see who changed what, when and why.
- Rollback. You can return to a known good version after a mistake.
- Collaboration. Many people can work on the same files without overwriting each other.
- Branching. You can try a change in isolation and merge it when ready.
- Backup. With a remote copy, the history survives the loss of one machine.
- Audit. Each commit has an author and a message, which helps with reviews and compliance.
The three areas
| Area | Meaning |
|---|---|
| Working directory | The files you see and edit |
| Staging area | The changes you have chosen for the next commit |
| Repository | The saved history of commits |
Getting a repository
git clone copies a remote repository, with all its history, to your machine. git init instead starts a new, empty repository in the current folder.
$ git clone REPO_ADDRESS
$ cd project
$ git status
git status shows which files are changed, which are staged and which are not tracked.
Adding and removing files
git add moves changes into the staging area. This includes new files, because Git does not track a file until you add it.
$ git add script.py
$ git add .
$ git rm old_notes.txt
git add . stages every change in the current folder. git rm removes a file from the working directory and stages the removal. Use git rm --cached to stop tracking a file but keep it on disk.
Committing
A commit saves a snapshot of the staged changes together with a message.
$ git commit -m "Add VLAN check function"
$ git log --oneline
Each commit has a unique ID, which is a hash. Write messages that explain the reason for the change. git log lists the history, and the --oneline option shortens each entry to one line.
Push and pull
Commits stay local until you share them. git push sends your commits to the remote repository. git pull fetches new commits from the remote and merges them into your current branch.
$ git push origin main
$ git pull origin main
The name origin is the default name for the remote you cloned from. git fetch downloads new commits without merging them, so you can look first.
Comparing with git diff
git diff shows changes that are not yet staged. git diff --staged shows the changes that are staged for the next commit. You can also compare two commits or two branches by giving their names.
$ git diff
$ git diff --staged
$ git diff main feature
The output is a unified diff. Lines that start with a minus sign were removed and lines that start with a plus sign were added.
Run git status often. It tells you which area each change is in and usually suggests the next command.
What to remember for the exam
Seven facts are worth learning.
- Git is distributed, so each clone holds the full history.
git clonecopies a repository andgit initcreates a new one.git addstages changes andgit commitsaves them locally.git pushsends commits to the remote andgit pullbrings them in.git rmremoves a file from the working directory and stages the deletion.git diffshows unstaged changes andgit diff --stagedshows staged ones.- Version control gives history, rollback, collaboration and an audit trail.
Common mistakes
Four mistakes appear often.
Forgetting to add before commit. A commit saves only staged changes, so edits you did not add are left out.
Expecting commit to share work. A commit is local. You still need git push.
Confusing pull and fetch. A pull fetches and merges. A fetch only downloads.
Vague commit messages. A message such as fix tells later readers nothing.
Do not commit passwords, tokens or keys. They stay in the history even after you delete the file.
Check yourself
Work through these three questions.
What does
git clonedo?It copies a remote repository, including its history, to your machine.
Why does a change not appear in a commit even though you edited the file?
Only staged changes are committed, so you must run
git addfirst.What is the difference between
git diffandgit diff --staged?git diffshows changes that are not staged.git diff --stagedshows the changes staged for the next commit.
Topic 6 of 7 in Software Development and Design
Test yourself on Software Development and Design
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.