0% found this document useful (0 votes)
13 views49 pages

Devnet 2123

The document is a guide to advanced Git commands presented at Cisco Live by Kareem Iskander. It covers topics such as merging, pull requests, interactive rebase, recovering deleted commits, and using submodules. The document also includes useful commands and resources for further learning about Git.

Uploaded by

Adriana Mitsova
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)
13 views49 pages

Devnet 2123

The document is a guide to advanced Git commands presented at Cisco Live by Kareem Iskander. It covers topics such as merging, pull requests, interactive rebase, recovering deleted commits, and using submodules. The document also includes useful commands and resources for further learning about Git.

Uploaded by

Adriana Mitsova
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

#CiscoLiveAPJC

Get into Git!


The Advanced guide to Git commands

Kareem Iskander, Lead Technical Advocate


@Kareem_Isk
DEVNET-2123

#CiscoLiveAPJC
Cisco Webex App
[Link]
ciscolivebot/#DEVNET-2123

Questions?
Use Cisco Webex App to chat
with the speaker after the session

How
1 Find this session in the Cisco Live Mobile App

2 Click “Join the Discussion”

3 Install the Webex App or go directly to the Webex space

4 Enter messages/questions in the Webex space

Webex spaces will be moderated


by the speaker until November 15, 2024.

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 4
Agenda

• 2min git refresher


• Merging and Pull Request
• Interactive Rebase
• Recovering Deleted Commits
• Submodules
• Search and Find
• Resources

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 5
What is Git?
Git

• An open-source distributed version control


system
• Designed with performance, security, and
flexibility in mind
• Stores snapshots of the full file instead of diffs
• Changes are stored in trees
• Trees contain changed files
• Commits contain trees
#CiscoLiveAPJC © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public
Git vs. GitHub
• GitHub is a commercial
company, that runs [Link]
based on Git Version Control
System.

Git is an open source


Distributed Version Control
System

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 8
Git: Technical
Overview
Useful Git Commands
Action What it does Command
Tell git who you are > git config --global [Link] “your name”
Setup
one-time setup > git config --global [Link] your@[Link]
Clone Clone (“download”) a git repository > git clone <ur>
Check the Status of your local > git status
Status
repository
Checkout A Create and Checkout a local Branch > git checkout –b new-branch-name
Branch Creates a “safe place” for your changes

Add Add a file to your next commit. > git add filename
Commit Commit your changes. > git commit –m “Your commit message.”
Check-out a file from the last > git checkout filename
commit.
Checkout A File Reverts any changes you have made and
restores the last committed version of a file.

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 10
DevNet Sample-Code Workflow
git checkout

Sample Sample
git clone Code Code
(main) (mycode)

Step Action Git Command git commit Edit


1. Clone the Remote Repository git clone url

2. Create and Checkout a Local Branch git checkout –b new-branch-name


def f(x): def f(x):
3. Incrementally Commit Changes git add filename ... ...
git commit -m “Commit message” ...
git add ...

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 11
Merge & PR
Merging
• Merging is designed to integrate changes from one branch into
another branch
Use Case
• Multi-site Python deployment automation in the main branch
• The Main branch also contains a newly added Meraki deployment script
• The Feature branch needs to expand on Meraki deployment to include
camera option for the new site

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 13
Merge - Steps

OR

• Merging is a non-destructive operation


• Existing branches are not changed in any way
• Feature branch will have irrelevant commits
• Can be difficult for developers to understand features added to
branch
#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 14
Pull Request
• Pull requests are a mechanism for a developer to notify team
members that they have completed a feature.
• Once their feature branch is ready, the developer files a pull
request via their GitHub account
• This lets everybody involved know that they need to review the
code and merge it into the main branch.

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
Pull Request - Steps

Use Case
• Let’s take our Meraki cameras Feature branch
• I’ve made changes, commits and I’d like to notify Main branch
owners
• Feature branch is to become the base code in Main

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 16
Interactive
Rebase
Interactive Rebase
A Tool for Optimising & Cleaning up Commit History
• Change a commit's message
• Delete a commit
• Reorder commits
• Combine multiple commits into one
• Edit/Split an existing commit into multiple new ones

DO NOT use Interactive Rebase on commits that you've


already pushed/shared on remote repo!

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 18
Interactive Rebase
Use Case
• You are working on the Feature branch to expand Meraki
automation
• You have been making commits ever function you write
• You are ready to merge into Main branch
• You have realized:
1. “Over Commit-ted” - get it?
2. Commit messages aren’t cutting it

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 19
Interactive Rebase - Steps Change Commit
Range

1. How far back do you want to go? C1 C2 C3 C4

2.

3. Determine action to apply to your commits


4. Make changes & save

5. Check changes

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 20
Let’s try it!
Interactive Rebase - Reword

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
Interactive Rebase - Squash

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 23
Cherry Picking
Cherry Picking
Use Case
• You are working on the Feature branch to expand your Meraki site
automation
• You have made a commit on the Feature Branch
• You noticed you are working in the Main Branch
• The commit does not belong in Main .. Yet
• What to do??

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 25
Cherry Picking
Integrate Single, Specific commit

• Cherry Picking allows you to select individual commits


• Cherry Picking integrate specific commits to Branch
• Only Commit C2 from Branch B to integrate into Branch A

C C C Branch
1 3 5 A

C C Branch
2 4 B

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 26
Cherry Picking - Steps
1. While on Main Grab the commit
hash

2. Checkout the Feature branch

4. Clean up Main
3. Cherry Pick the commit

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 27
Let’s try it!
Cherry Picking

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 30
Reference logs
Reflog
• Reflog is a journal of all the movement
• Reflog is a Protocol of HEAD Pointer Movements
• Merge, Rebase, Cherry Pick, Reset .. All of the movements

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 32
Reflog
Use Case
• You are working on the Feature to expand your Meraki site
automation
• You have gone “commit-crazy”
• You think you want to get rid of some commitsReset HEAD
• You use “git reset”
C C
• You delete an important commit C
1 2 3
Main

• Panic Mode

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 33
Reflog - Steps
1. Reflog are in chronological order

2. Find the catastrophic reset and copy the state before into new
Branch

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 34
Let’s try it!
Reflog - Recover Commits

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 37
Submodules
Submodules
• Git Repo inside Git Repo
• Submodule is a standard git repo which means you can add,
commit, pull ..
• Difference is that it is nested within a parent repo
• Important to know:
• Submodule content are not stored in the parent repository
• Parent repo stores:
• Submodule remote URL
• Local path
• Checked out revision
#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 39
Submodules
Use Case
• You decided to use an SDK to automate your Meraki Deployment
• SDK needs to be embedded into your Branch
• Option 1 (bad):
• Download the SDK , Mix it with your code and treat it as one branch
• Updating the external code is now a manual process

• Option 2 (good):
• Git Submodules

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 40
Submodules - Steps - Local Project
1. Let’s create a subfolder with our library

2. Grab the library as a Git Submodule

3. View system files


DO NOT forget to commit
the submodule in Parent repo

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 41
Submodules - Steps - Cloned Repo
1. Clone Repo with Submodules

2. Notice the submodules, empty folders

3. Populate the submodules


OR

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 42
Submodules - Revision
In a “Standard” Git Repo In a Submodule Git Repo
• You checkout a Branch • Your last commit is your
checkout revision
• The last commit is your
checkout revision • You must explicitly update and
commit a submodule for the
• The new revisions are always
point to move
the latest commits
C C C
C C Main C
3
Main
2 3 2
1 1

C C C C
C
C C Main C Main
2 3 4 2 3 4
1 1

New Rev Revision stays

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 43
Search and Find
Search & Find

Action flag Command


By date --before / -- after git log --after="2023-2-1" --before"2023-4-1"

By message --grep git log --grep="” (Can use Regex)


By author --author git log --author=”Kareem iskander"
-- <filename> git log -- [Link]
By file

By branch <branch-A> git log feature..main (in main but not in feature)

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 46
Resources
Tech learning, shaped to you.
[Link]

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 48
Kareem Iskander
Lead Technical Advocate, Cisco Learning &
Certification

kiskande@[Link]

@Kareem_Isk

[Link]

[Link]

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 49
Complete Your Session Evaluations

Complete a minimum of 4 session surveys and the Overall Event Survey to


claim a Cisco Live T-Shirt.

Complete your surveys in the Cisco Live mobile app.

#CiscoLiveAPJC DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 50
• Visit the Cisco Stand
for related demos

• Book your one-on-one


Meet the Expert meeting

• Attend the interactive education


Continue with DevNet, Capture the Flag,
and Walk-in Labs
your education
• Visit the On-Demand Library
for more sessions at
[Link]/on-demand

Contact me at: kiskande@[Link]

DEVNET-2123 © 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public 51
Thank you

#CiscoLiveAPJC
#CiscoLiveAPJC

You might also like