Hands On Series Part 4 Git For Version Control
Hands On Series Part 4 Git For Version Control
20.4.2022
Ed Moore
© 2022 Cisco
© 2022and/or its affiliates.
Cisco and/or Allrights
its affiliates. All rights reserved.
reserved. Cisco Confidential
Cisco Confidential
Getting Started
Installation
Follow install instructions documented in the git-
scm site for your chosen operating system:
https://git-scm.com/book/en/v2/Getting-Started-
Installing-Git
Basic Config
How to get started After Installation/validation set your username and email:
git config --global user.name my_username
git config --global user.email my@email.com
https://code.visualstudio.com/docs/introvideos/basics
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Before we begin. Learning Labs
https://developer.cisco.com/learning/labs/git-
intro/introduction/
o Introduction – 5 mins
If you’re like me and struggle with non-practical
learning, we’ve created a Learning Lab to nail down
o Overview to Git – 25 minutes
these basics to repeat at anytime or share with
colleagues.
o Getting Started – 5 minutes
Reference Guide
o Hands on (Breakout)– 15 minutes https://git-scm.com/docs
topics
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Overview
What do we want?
To efficiently create new functionality and
manage existing functionality in a collaborative
manner.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Why Git?
Common Integrations
• Pipelines: Actions/Runners 3
• Comms: Teams/Email etc..
• Validation: Linters, pre-commit
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Primary Personas
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Key Concepts
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Key Concepts: Infra as Code & CI/CD
The “Source Of Truth” Stage / Test / Production
Lab
Workflow Compliance
External Services
Automation checks
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
*CRUD – Create, Read, Update, Delete
Term
centralized systems
Version 2
• Each user has a full working copy along Version 3
with the full change history Version 1
Version 3
Version 2
• Working copies essentially function as Version 2
remote backups, which helps avoid relying Version 1
on one single source as a point of failure Version 1
Version 1
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Introduction
Key Characteristics
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Lifecycle: File
Tracked
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Moving files between the main areas of
Git and remote repositories
Local
Working Remote
Staging area Repository
directory repository
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Task Overview
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Check status
My first project One of the most useful commands is:
git status
It details a summary of gits’ understanding of your
repository.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
init vs clone
Init is used to initialize a local repository and create
the .git subdirectory. Git clone first initializes the
local directory then copies the remote repository
into that folder.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Commit
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Verify changes
Difftool
Difftool is a useful appliance to compare the file
Actual line differences and can be used with useful tools to
changes make identifying differences simpler.
From Line 1, two lines were added
git config diff.tool vscode
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Logs
Git Tags
Local Alias
Working You can create git specific aliases or as standard
Staging area Repository
directory
within bash/zsh etc..
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Branching & Issue Resolution
Task Overview
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Branches & Issues
What is a Branch?
A branch in Git is simply a lightweight movable
Git Branches
pointer to a commit. To list the created branches
you can use the below command.
git branch
Creating a branch Swapping between branches is achieved by using
the branch command with – specified branch name.
git branch <name>
Creating a Branch
Creating a branch can be done in two ways: git branch
creates the branch but does not swap you into it,
whereas git checkout –b combines the two processes.
git branch <name>
git checkout -b <name>
Delete a Branch
Branches can be deleted simply by using the –d
option.
git branch –d <name>
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Branches & Issues
Merging
Merging Strategies
▪ Fast Forward vs Non Fast Forward
▪ Alternative : Rebase (discuss later)
▪ Pull request & Branch Policies
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Git Branches
General Recommendations
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Fetch
Remote processes
Fetch is used to request all remote branches with
their associated commits and files to the
/refs/remotes directory. It doesn’t affect local
branches and can therefore be used to compare
remote vs local changes.
git fetch
Pull
Pull concatenates fetch and merge, where the
remote branches and heads execute a local merge
commit.
git pull <remote>
Push
As an inverse of pull, push uploads the commits
from the local repository to the remote. Conflicts
can be common when working in collaboration, so
general practice is to fetch before a push and
preferably to use seperate branches and pull
requests.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
git push origin -u
Issue Resolution
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Issue Resolution
Reset
Git Reset can be used to move both the HEAD
pointer and the current branch pointer to a chosen
point.
git reset <reference_pointer>
Resolution
git reset HEAD
echo ’private.key’ >> .gitignore
git add .
git commit –m “Adding the public key”
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Issue Resolution
Working/Stage Issues
Similar to the previous example reset can be used
to rectify unmade changes by resetting to HEAD or
an earlier time.
Resolution
git reset –-hard HEAD
git checkout .
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Issue Resolution
Git Clean
“Urgh, this is a mess” Clean essentially removes all untracked files from
our Working directory.
git clean –f
In combination with the –nd option, it can indicate all
untracked files & directories.
git clean -dn
Git Revert
Revert essentially acts as an undo operation though
as historical integrity is important it calculates the
inverse of a previous commit.
git revert HEAD
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Issue Resolution
Merge conflict
When the same line is modified in two branche, this
usually happens when more than 1 person works on
the project. Git is unable to resolve which changes it
should incorporate automatically.
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Pro Git eBook (git-scm)
https://github.com/progit/progit2/releases/downloa
d/2.1.386/progit.pdf
Github Tutorials/Docs
https://docs.github.com/en/get-started
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential