[go: up one dir, main page]

0% found this document useful (0 votes)
23 views34 pages

Hands On Series Part 4 Git For Version Control

Git is a free and open source version control system that provides functionality to efficiently create and manage code in a collaborative manner. It allows outsourcing change tracking and defining operational rules. Key concepts include infrastructure as code, where infrastructure configuration is represented in human and machine-readable formats like YAML and tracked in a version control system. This enables change tracking, rollbacks, and automation of deployments through tools like Ansible and Terraform. The document provides an overview of Git and getting started instructions, including installation, basic configuration, and integrating with IDEs like VS Code. It also outlines learning resources and a reference guide.

Uploaded by

lyobov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views34 pages

Hands On Series Part 4 Git For Version Control

Git is a free and open source version control system that provides functionality to efficiently create and manage code in a collaborative manner. It allows outsourcing change tracking and defining operational rules. Key concepts include infrastructure as code, where infrastructure configuration is represented in human and machine-readable formats like YAML and tracked in a version control system. This enables change tracking, rollbacks, and automation of deployments through tools like Ansible and Terraform. The document provides an overview of Git and getting started instructions, including installation, basic configuration, and integrating with IDEs like VS Code. It also outlines learning resources and a reference guide.

Uploaded by

lyobov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Git: Use Case & Introduction

20.4.2022
Ed Moore

Customer Success Specialist


Why are we here?

© 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

IDE Integration (Optional)


VS Code is the most popular tool with integrated
SCM; additionally, there are many extensions to
make life easier - a personal favorite is Git Graph.

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

o Branching & Issue Resolution – 10 minutes


Easily one of my most visited pages which comes in
o Hands on (Breakout)– 25 minutes useful when you’re in a mess or have forgotten a
command.
o Basic GitHub Walkthrough & Additional

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.

What makes this difficult?

Problem Statement As solutions have become more complex,


collaboration with legacy tools is time
consuming, error prone and slow.

What can we do about it?


• Outsource change tracking to a tool.
• Define policy-based operational rules.
• Enhance capabilities through integrations.

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Why Git?

Free & Open Source Performance

• GPLv2 guarantee • Speed


• Commercial platform offerings • Scalability
• Industry leader 1 2 • Reliability

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

Individual developer Infrastructure-Oriented Code-Oriented


Starting to utilize some of the functionality of The state of the infrastructure is The IT team are geographically spread
version-controlled systems, which may expressed with readable configuration and across EMEA; they have a joint interest in
include common integrations and validations. a standardized, automated pipeline. managing a full-stack spanning multiple
Tools such as VS Code provide a low barrier datacenters, branches and a campus
Configuration needs to be versioned and
to entry with a wide array of free solutions. estate.
meet specific requirements to ensure the
security and performance of underlying
applications.

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Key Concepts

Infrastructure as Code ---


- hosts: dnac_servers
vars_files:
- credentials.yml
gather_facts: no
• A representation of the desired end-state tasks:
configuration
- name: Find discovery
cisco.dnac.discovery_range_info:
• Structured Data like YANG and YAML dnac_host: "{{dnac_host}}"
dnac_username: "{{dnac_username}}"
• Human and machine readable dnac_password: "{{dnac_password}}"
• Tracked in Version Control system dnac_verify: "{{dnac_verify}}"
dnac_debug: "{{dnac_debug}}"
startIndex: 1
• Change tracking and rollback recordsToReturn: 500
• Deployed via automation or orchestration tools register: discovery_range_result

- name: Set fact of filtered discoveries


• Ansible, Terraform, CloudFormation, NSO, etc. set_fact:
discoveries_filtered:
"{{discovery_range_result.dnac_response.response |
selectattr('name', 'equalto', 'start_discovery_test2')}}"
when:
- discovery_range_result is defined
- discovery_range_result.dnac_response is defined
- discovery_range_result.dnac_response.response is
defined
register: discovery_filtered_results

© 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

Configuration Automated Deployment to


modeled and Version control & deployment to Production
stored as IaC code branching Stage for
validation

Continuous Integration Continuous Deployment


(CI) (CD)
© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Overview User #1- Local User #2- Local

Working directory Working directory

Component Architecture Staging area Staging area


Typical functions
Repository Repository
Two users are working on a project together,
1. User #1 starts working on a project on their machine.
2. The pair decide to start working together and so User #1
They decide
Remote repository
3. User #2 begins to work with User #1 and so clones a
copy of the remote repository to their machine.
4. Additional functionality/integrations are added as
required.
GitHub, Gitlab, Bitbucket

Workflow Compliance
External Services
Automation checks

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
*CRUD – Create, Read, Update, Delete
Term

Distributed version control


• Peer-to-Peer approach to version control,
as opposed to the client-server model of Version 3

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

• Most common are Git, Mercurial, and Version 3


Bazaar
Version 2

Version 1

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Introduction
Key Characteristics

• Git is a distributed VCS


• Git stores snapshots, not differences
• Git performs nearly every operation locally
• Most operations need only local files, so they
can be performed offline

• Git has Integrity


• Everything in Git is checksummed (SHA1)
before it is stored and is then referred to by
that checksum

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Lifecycle: File
Tracked

Untracked Unmodified Modified Staged

Create a new file


Edit the file

Add the file


Version 3
Commit the file
Version 2
Remove a file
Version 1

© 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

Git add Git commit Git push


GitHub
Git fetch GitLab
Bitbucket
Git pull

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started

Visual Studio Code


Popular IDE with lots of Integrations
Visual Studio Code is a code editor redefined
and optimized for building and debugging
modern web and cloud applications. A
lightweight version is available by replacing the
dotcom GitHub address to dotdev:
i.e.: https://github.dev/moore-
automation/dnac_software_compliance_reques
ts

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started
Task Overview

• Initialise our repository


• Add files to the staging area
• Commit files to the local repository
• Verify changes and state is as expected.
• Experiment with different situations

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started

Create a new file


mkdir example_demo
cd example_demo
touch example.txt
echo “sample text” >> example.txt

Check status
My first project One of the most useful commands is:
git status
It details a summary of gits’ understanding of your
repository.

Useful tools & IDEs


VS Code is the most popular tool with integrated
SCM; additionally, there are many extensions to
make life easier - a personal favourite is Git Graph.

© 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.

Clone existing repository


Initialize our repository git clone https://github.com/repo_name...

Create a new Repository


cd /path/to/your/existing/code
git init

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started

Add file(s) to staging area

Files can be added to the index using the add


command; a snapshot of the added content is then
incorporated into future commits. Similarly, they can
be removed using the rm command from either the
index or working directory.

Remove file(s) from index

Add/Remove Files Add specific file to the index


git add <filename>
Add all files in directory (except ignored)
git add .

Local Remove file(s) from index


Working Remove specific file from index
Staging area Repository
directory
git rm --cached <filename>
Git add Git commit
Remove specific file from directory and index.
git rm -f <filename>

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started

Commit

Snapshot of the entire repository at a given time,


where all modifications to Staging Area are saved in
the local repo. Files not added to Staging Area are
not included.
git commit -a

Clone existing repository

Commit Changes Messages are required to indicate the purpose of each


commit, there are many popular standards but general is
to describe intent in a concise manner.
git commit -am ”this is commit message"

Local SHA Hashing


Working Each commit has unique SHA1 hash (40 chars), for
Staging area Repository
directory
example:

Git add Git commit b4a450b50d9bf229dfb483be612506b813bb4fa2


hash(parent commit + date + author + message)

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Getting Started

Verify changes

Differences Delta of the change between Repository and Staging Area


git status –short
State: (M) Modified,(A) Added, (D) Deleted, (?) Untracked

Difference between old (a) to new(b)


t.txt file
diff
Diff provides a view of the differences found between
tracked files prior to a commit.
git diff

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 log provides a useful overview of the history of


commits made within the repository. There are
many options that provide slightly different views,
two are included below.
git log

Git Tags

What’s going on? A tag is a user-friendly pointer (label) to a given commit


which is statically assigned to the commit.
git tag <tag_name>
git tag <tag_name> 98ae100

Local Alias
Working You can create git specific aliases or as standard
Staging area Repository
directory
within bash/zsh etc..

Git add Git commit git config --global alias.history “log -


-oneline --graph --all”

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Branching & Issue Resolution
Task Overview

• Create a new branch


• Make updates to our new ‘feature’
• Compare differences between branches
• Verify changes and state is as expected.
• Experiment with different situations

© 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

Git Branches A branch in Git is simply a lightweight movable


pointer to a commit.
git branch
Merging a branch
Merging Process
In order to merge changes into a desired branch
you need to checkout that branch and merge from
the branch you’ve added too.
git branch <target>
git checkout -b <source>

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

• Use branches to implement new features or fix


bugs.
• Try to keep the amount of changes in a branch
small
• e.g., single feature, bug fix, change request

• Give your branches a meaningful name


• e.g., bug ID, change request ID

• The more you change and the longer you


maintain a branch, the more your code will
diverge from master.

© 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>

“I shouldn’t have gitignore


Gitignore is a file located in the root directory which

added this” contains patterns with which git will ignore.


Common examples include build artefacts or private
keys etc..

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.

“I should not have Checkout


We used checkout to swap branches in the previous

deleted that!” section, it can be used for a different purpose to


undo a change where the working directory is
‘swapped’ to the previous commit. This is useful for
troubleshooting or rectification.

Resolution
git reset –-hard HEAD
git checkout .

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential
Issue Resolution

Commit message update


Quite often you’ll encounter unclear or inaccurate
commit messages; these can be amended in the
following manner:
git commit –amend –m |”better message”

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.

“What do you mean Aborting a Merge


Merges can be aborted simply using the –-abort

there’s a conflict?” option.


Git Merge –-abort

(master) $ git merge feature


Auto-merging blah.file

CONFLICT (content): Merge conflict in


blah.file
Automatic merge failed; fix conflicts
and then commit the result.

© 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

VS Code – Source Control Tutorials

What now? https://code.visualstudio.com/docs/sourcecontrol/o


verview

Automation for Engineers – White paper


https://www.cisco.com/c/dam/en/us/solutions/colla
teral/executive-perspectives/technology-
perspectives/automation-driving-network-eng-
skills-trans.pdf

Github Tutorials/Docs
https://docs.github.com/en/get-started

© 2022 Cisco and/or its affiliates. All rights reserved. Cisco Confidential

You might also like