SlideShare a Scribd company logo
1 of 50
Download to read offline
secret sauce
Violeta Menéndez González
v.menendezgonzalez@surrey.ac.uk
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
What is git?
● Source code management tool
my_code.py
my_code_NEW.py
my_code_NEW2.py
my_code_final.py
my_code_DEFINITELY_FINAL.py
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
What is git?
Distributed instead of centralised:
● Each user has a full copy of the repo
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
What is git?
● It’s not GitHub!
● GitHub is an internet hosting provider that hosts your code and you can
manage it using git
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
What is git useful for?
● Reproducibility
● Collaboration
● Debugging
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
GUIs
This talk is about the commands and concepts. Everything is done on the Linux
terminal, but there are lots of GUIs that are helpful to manage your repository.
Some GUIs are integrated into your IDE (like VSCode).
For more: https://git-scm.com/downloads/guis
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Terminology
● Repository (repo): contains the project’s code and changes
● Working copy: your own copy of the repository
● Commit: a git object that represents changes to the code
● Revision: a reference to a git object. In practice, is a version of your
codebase
● Working tree or working area: where you work and make changes
● Index or staging area: changes that we want to commit
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Development history
● Tracks changes to our source code
○ Like a timeline or tree of changes
● Good and clean history is important:
○ Easier code reviewing
○ More difficult to introduce problems
○ Make debugging much easier
○ Code quality!
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Keeping track of changes with commits
● Every change is a commit with:
○ A unique identifier
○ A date
○ An author
○ A clear and concise message
○ A set of changes to the code (diff)
● Good messages for good history:
○ Short description in imperative
○ Blank line
○ Long description
● Commit ID: SHA1
○ Hash: means it’s unique!
○ It encodes history
commit 2bedc34e012778ca0abb70c14e9e40d13a81f53e
Author: Violeta Menendez Gonzalez <v.menendezgonzalez@surrey.ac.uk>
Date: Wed May 25 15:54:31 2022 +0100
Check if cuda available before using
diff --git a/train.py b/train.py
index bc31650..10ebd4f 100644
--- a/train.py
+++ b/train.py
@@ -572,4 +572,5 @@ if __name__ == '__main__':
try:
main()
finally:
- print(torch.cuda.memory_summary(abbreviated=True))
+ if torch.cuda.is_available():
+ print(torch.cuda.memory_summary(abbreviated=True))
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
History is editable
● Vital for good history
● Local history is editable:
○ Don’t modify history that has been pushed to the shared repo
● History needs to be clean:
○ Linear: commits don’t depend on later commits, we don’t want commits to break the system
○ Atomic: commits need to be concise, fixing only one bug or implementing only one feature.
Pro tip: if the commit message contains the word ‘and’ maybe think of breaking it down!
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Workflow
● Work in branches (feature branches)
● Main branch (master) for tested and clean changes
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Workflow
● Commits have an order, you may need to re-order to have a sensible history
● HEAD of a branch is the last commit
Basic commands
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Starting your repo
● git init - create a git repository
● git add <file> [<file2>...] - add files to the index for commit
● git commit - creates a commit with all the files in the staging area. Prompts
for a command message
○ Pro tip: git commit -m “Short concise message” to make the message faster
● git log - shows all the commits in your current branch
○ Pro tip: git log -p - to show the commit including code changes (diff)
○ Pro tip: git log --oneline - to show the logs in short mode
● git show <revision> - show the diff of a specific commit
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Working area
● git status - shows the status of your working area, files added, changed,
removed, conflicts, files staged…
● git diff - shows all code changes in your working area. You may have
several commits worth of changes
● git add -p - add individual changes to the index
● git diff --cached - show all changes that have been staged. If there’s
something you don’t like you can make changes
● git reset HEAD <file> - remove changes in a file from the index
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Workflow
You have several commits worth of code changes in your working tree
● git diff - see changes
● git add -p - add individual changes
● git diff --cached - double check changes you have staged for commit
● git reset HEAD <file> - remove any changes you don’t like from the
index
● git add -p - add any changes you missed
● git commit - commit changes with a good commit message
● git log -p - double check your commit looks OK
● git commit --amend - change commit at HEAD of current branch
Iterate until you have added all changes
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 1: Starting a repo
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Working on branches
● Branches can be just local
● Very fast to create
● Very fast to switch between
● Very convenient for working
● Modular changes
● Consist of one or more commits
● Can be “moved around”
● Default branch is “master”, like the main trunk of the repository tree
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Branches
● git branch - show all branches in your repo (* star on you current branch)
● git branch <name> - creates branch (but not checked out)
○ Pro tip: follow a good and clear name system that describes the feature you’re developing. If
you are carrying out different independent changes, think of creating different branches and
switch between them. If you’re working with other people it can be useful to use the (?)
name/feature (i.e., violeta/data_validation)
● git checkout <branch_name> - checkout to branch
○ Pro tip: git checkout -b <name> - will create the branch AND checkout to it in one
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Merge
Once your changes are tested and the history on your branch is clean, we can
incorporate them to the main branch “master”.
● git checkout master
● git merge <branch> - merges branch ONTO master
○ You merge a branch onto your current branch, doesn’t have to be master. Make sure you
check what your current branch is!!
NOTE: This can cause merge conflicts. DON’T PANIC
git status will tell us what to do
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 2: Branch and merge
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Merge conflicts
● They can occur when we try to modify the same line in two different ways
● git status - shows which file has the conflict
● Edit file:
○ You can keep the current changes
○ You can keep the old changes
○ Or you can modify them to look how you like
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Revisions
Revisions are revised versions of our code. Each commit represents a revision of our
code.
● git checkout <commit> - checkout a specific revision of our code. We are in a
‘detached HEAD’, we are not in a branch any more.
● We can make experimental changes, build or test our system without impacting any
branches.
● git checkout -b <name> - if we want to save the new commits in a branch
or
● git checkout <previos_branch> - to go back to where we were
○ Pro Tip: you can also use ‘git checkout -’
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Stash
If we have uncommitted changes in our index and we want to checkout a different
revision, they may go into conflict!
● git stash - stashes all changes “away”, leaving a clean working directory
● git stash list - shows all stashed changes
● git stash apply - to restore the changes you saved
● git stash pop - apply changes AND remove them from the list
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Resetting revision
● git reset <commit> - set your current revision and index area to a certain
state
○ --mixed - Resets index but not working tree. Sets HEAD to commit,
discards all history, changes are kept in working tree. (default)
○ --soft - Leaves index and working tree untouched. Sets HEAD to
commit, discards all history, leaves changes staged for commit
○ --hard - Resets both index and working tree. Resets HEAD, history
AND working tree. You lose all code!!!
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 3: stash and reset
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Rebase
Rebase is used to integrate history from one branch into another branch. History is
rewritten, we want to rewrite it to make it better!
● You have a working branch
● Someone else pushes to master in the remote repo
● You want to push your changes, but they may conflict with the remote repo
● You want to update the repo to reflect the other person’s changes
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Rebase
● git rebase <branch> - rebases your current branch over branch. All
commits from branch are now in your history.
○ git checkout branchA
○ git rebase master
There may be conflicts (because we may have changes that
apply to places that are different now due to the new commits!)
BUT
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Resolve conflicts
● git status will tell you which files are in conflict!
● Make relevant changes
● git add <your changes>
● git rebase --continue
If we changed our minds:
● git rebase --abort
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 4: Rebase and solve conflict
Note: commit SHAs change, as history has changed
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Interactive rebase
● More flexible rebase
● Useful for:
○ Integrating changes from code review
○ Making history simple and linear
○ Making every point in history valid
○ Making sure commits don’t undo each other
○ No “oops” commits
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Interactive rebase
● git rebase -i <commit> - rebase all commits newer than <commit>
Opens a list of commits with options to modify, reorder or delete them:
● p (pick) - leave as is (default)
● r (reword) - change message
● e (edit) - modify contents of commit
● s (squash) - combine with previous commit
CAREFUL: rebase is destructive. Make sure you in the correct branch, and back up!
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Splitting commits
● Mark commit for edit
● git reset HEAD^ - reset log to previous commit leaving changes in tree
● git add -p
● git commit
● Repeat until done
● git rebase --continue
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 5: Interactive rebase and split commit
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Debugging
● git blame <file> - shows the commit that introduced each line of a file.
● git bisect - Amazing debugging tool. Prompts you to give a good and a
bad commit, then uses a bisecting algorithm to checkout commits in between.
You then run your test and mark the commit as good/bad. After a few
iterations git tells you which commit introduced the bug!! Magic
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Debugging
● git blame <file> - shows the commit that introduced each line of a file.
● git bisect - Amazing debugging tool. Prompts you to give a good and a
bad commit, then uses a bisecting algorithm to checkout commits in between.
You then run your test and mark the commit as good/bad. After a few
iterations git tells you which commit introduced the bug!! Magic
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Debugging
● git blame <file> - shows the commit that introduced each line of a file.
● git bisect - Amazing debugging tool. Prompts you to give a good and a
bad commit, then uses a bisecting algorithm to checkout commits in between.
You then run your test and mark the commit as good/bad. After a few
iterations git tells you which commit introduced the bug!! Magic
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Debugging
● git blame <file> - shows the commit that introduced each line of a file.
● git bisect - Amazing debugging tool. Prompts you to give a good and a
bad commit, then uses a bisecting algorithm to checkout commits in between.
You then run your test and mark the commit as good/bad. After a few
iterations git tells you which commit introduced the bug!! Magic
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Ranges
● git diff <commit2>..<commit4> - shows all changes between two
commits (older..newer)
○ git diff <commit2>..HEAD - all changes from commit2 until present
● git log <commit2>..<commit4> - shows all commits between commit2
and commit 4
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Remotes
● Remotes are repositories that are backed up in some server (e.g. GitHub )
● Use for collaboration and backup
● git clone <URL> - retrieves a repository into a directory
○ ssh://[user@]host.xz[:port]/path/to/repo.git/
○ git://host.xz[:port]/path/to/repo.git/
○ http[s]://host.xz[:port]/path/to/repo.git/
git is the
baaeest
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Remotes
● You can have several remotes. Default is origin.
○ e.g., If you want to make changes to a repo that you don’t want to be public
● git add remote <name> <remote> - adds a new remote
● git checkout <remote>/<branch> - checks out a branch from a
specific remote
● git remote -v - show tracked remotes
● git rename <old> <new> - change name of remote
$ git remote -v
origin gitlab@gitlab.eps.surrey.ac.uk:violeta/mystery-project.git (fetch)
origin gitlab@gitlab.eps.surrey.ac.uk:violeta/mystery-project.git (push)
upstream git@github.com:alex/mystery-project.git (fetch)
upstream git@github.com:alex/mystery-project.git (push)
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Remotes
● git fetch [<remote>] - retrieves all references, doesn’t modify working
copy
● git pull [<remote> [<branch>]] - retrieves all changes from remote
into your local branch (default: origin, master)
● git push [<remote> [<branch>]] - pushes your local changes to
branch in remote
Attention!! Pull can create conflicts
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 6: Remotes
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
If everything else fails…
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Reflog
Git keeps track of every single branch change, and stores it in the Reference Log
● Each change is an entry in the reflog with a reference
● The state can be restored to any reference
● If you mess up… just reset it back to before it happened!
● Only local
● git reflog - list all entries
● git reset <sha1>
Rebase
Pull
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Demo 7: Reflog
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Resources
● Documentation: https://git-scm.com/
● git <command> --help
● Man pages:
○ man git-log
○ man git-add
○ etc
● Stackoverflow is your friend
● Special thanks to Alan Ott’s talk Git like a Pro:
https://youtu.be/H2annVAHKPE?t=19199
Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
Thank you!!!
● Next Monthly Mini Hack in June - to be announced

More Related Content

Similar to Git Basics walkthough to all basic concept and commands of git

Git tech talk
Git tech talkGit tech talk
Git tech talkrazasayed
 
Beginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitBeginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitRobert Lee-Cann
 
Histedit Mercurial Extension
Histedit Mercurial ExtensionHistedit Mercurial Extension
Histedit Mercurial ExtensionManel Villar
 
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a gitVincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a gitVincitOy
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control systemKumaresh Chandra Baruri
 
Git and github fundamentals
Git and github fundamentalsGit and github fundamentals
Git and github fundamentalsRajKharvar
 
How to git easily in day to-day work
How to git easily in day to-day workHow to git easily in day to-day work
How to git easily in day to-day workAlena Radkevich
 
Git walkthrough
Git walkthroughGit walkthrough
Git walkthroughBimal Jain
 
Git presentation bixlabs
Git presentation bixlabsGit presentation bixlabs
Git presentation bixlabsBixlabs
 
GIT_training_SoftServeBulgaria2016
GIT_training_SoftServeBulgaria2016GIT_training_SoftServeBulgaria2016
GIT_training_SoftServeBulgaria2016Peter Denev
 

Similar to Git Basics walkthough to all basic concept and commands of git (20)

Command line git
Command line gitCommand line git
Command line git
 
Git tech talk
Git tech talkGit tech talk
Git tech talk
 
3 Git
3 Git3 Git
3 Git
 
Beginner's Guide to Version Control with Git
Beginner's Guide to Version Control with GitBeginner's Guide to Version Control with Git
Beginner's Guide to Version Control with Git
 
Gn unify git
Gn unify gitGn unify git
Gn unify git
 
Histedit Mercurial Extension
Histedit Mercurial ExtensionHistedit Mercurial Extension
Histedit Mercurial Extension
 
Git Init (Introduction to Git)
Git Init (Introduction to Git)Git Init (Introduction to Git)
Git Init (Introduction to Git)
 
GIT
GITGIT
GIT
 
git2.ppt
git2.pptgit2.ppt
git2.ppt
 
Git essentials
Git essentialsGit essentials
Git essentials
 
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a gitVincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
Vincit Teatime 2015.2 - Otto Kekäläinen: Don't be a git
 
Git tips
Git tipsGit tips
Git tips
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control system
 
Git and github fundamentals
Git and github fundamentalsGit and github fundamentals
Git and github fundamentals
 
How to git easily in day to-day work
How to git easily in day to-day workHow to git easily in day to-day work
How to git easily in day to-day work
 
GIT Basics
GIT BasicsGIT Basics
GIT Basics
 
Git walkthrough
Git walkthroughGit walkthrough
Git walkthrough
 
Git presentation bixlabs
Git presentation bixlabsGit presentation bixlabs
Git presentation bixlabs
 
Git best practices workshop
Git best practices workshopGit best practices workshop
Git best practices workshop
 
GIT_training_SoftServeBulgaria2016
GIT_training_SoftServeBulgaria2016GIT_training_SoftServeBulgaria2016
GIT_training_SoftServeBulgaria2016
 

More from DivyanshGupta922023

More from DivyanshGupta922023 (17)

(Public) FedCM BlinkOn 16 fedcm and privacy sandbox apis
(Public) FedCM BlinkOn 16 fedcm and privacy sandbox apis(Public) FedCM BlinkOn 16 fedcm and privacy sandbox apis
(Public) FedCM BlinkOn 16 fedcm and privacy sandbox apis
 
DevOps The Buzzword - everything about devops
DevOps The Buzzword - everything about devopsDevOps The Buzzword - everything about devops
DevOps The Buzzword - everything about devops
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applications
 
Next.js - ReactPlayIO.pptx
Next.js - ReactPlayIO.pptxNext.js - ReactPlayIO.pptx
Next.js - ReactPlayIO.pptx
 
Management+team.pptx
Management+team.pptxManagement+team.pptx
Management+team.pptx
 
DHC Microbiome Presentation 4-23-19.pptx
DHC Microbiome Presentation 4-23-19.pptxDHC Microbiome Presentation 4-23-19.pptx
DHC Microbiome Presentation 4-23-19.pptx
 
developer-burnout.pdf
developer-burnout.pdfdeveloper-burnout.pdf
developer-burnout.pdf
 
AzureIntro.pptx
AzureIntro.pptxAzureIntro.pptx
AzureIntro.pptx
 
api-driven-development.pdf
api-driven-development.pdfapi-driven-development.pdf
api-driven-development.pdf
 
Internet of Things.pptx
Internet of Things.pptxInternet of Things.pptx
Internet of Things.pptx
 
Functional JS+ ES6.pptx
Functional JS+ ES6.pptxFunctional JS+ ES6.pptx
Functional JS+ ES6.pptx
 
AAAI19-Open.pptx
AAAI19-Open.pptxAAAI19-Open.pptx
AAAI19-Open.pptx
 
10-security-concepts-lightning-talk 1of2.pptx
10-security-concepts-lightning-talk 1of2.pptx10-security-concepts-lightning-talk 1of2.pptx
10-security-concepts-lightning-talk 1of2.pptx
 
Introduction to Directed Acyclic Graphs.pptx
Introduction to Directed Acyclic Graphs.pptxIntroduction to Directed Acyclic Graphs.pptx
Introduction to Directed Acyclic Graphs.pptx
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
01-React js Intro.pptx
01-React js Intro.pptx01-React js Intro.pptx
01-React js Intro.pptx
 
Nextjs13.pptx
Nextjs13.pptxNextjs13.pptx
Nextjs13.pptx
 

Recently uploaded

RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5T.D. Shashikala
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...ShivamTiwari995432
 
Lesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsxLesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsxmichaelprrior
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringKanchhaTamang
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdfKamal Acharya
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor banktawat puangthong
 
"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.MdManikurRahman
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AISheetal Jain
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfAshrafRagab14
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoninghotman30312
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...MohammadAliNayeem
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New HorizonMorshed Ahmed Rahath
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsKineticEngineeringCo
 

Recently uploaded (20)

RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
 
Lesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsxLesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsx
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and Applications
 

Git Basics walkthough to all basic concept and commands of git

  • 1. secret sauce Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
  • 2. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk What is git? ● Source code management tool my_code.py my_code_NEW.py my_code_NEW2.py my_code_final.py my_code_DEFINITELY_FINAL.py
  • 3. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk What is git? Distributed instead of centralised: ● Each user has a full copy of the repo
  • 4. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk What is git? ● It’s not GitHub! ● GitHub is an internet hosting provider that hosts your code and you can manage it using git
  • 5. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk What is git useful for? ● Reproducibility ● Collaboration ● Debugging
  • 6. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk GUIs This talk is about the commands and concepts. Everything is done on the Linux terminal, but there are lots of GUIs that are helpful to manage your repository. Some GUIs are integrated into your IDE (like VSCode). For more: https://git-scm.com/downloads/guis
  • 7. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Terminology ● Repository (repo): contains the project’s code and changes ● Working copy: your own copy of the repository ● Commit: a git object that represents changes to the code ● Revision: a reference to a git object. In practice, is a version of your codebase ● Working tree or working area: where you work and make changes ● Index or staging area: changes that we want to commit
  • 8. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Development history ● Tracks changes to our source code ○ Like a timeline or tree of changes ● Good and clean history is important: ○ Easier code reviewing ○ More difficult to introduce problems ○ Make debugging much easier ○ Code quality!
  • 9. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Keeping track of changes with commits ● Every change is a commit with: ○ A unique identifier ○ A date ○ An author ○ A clear and concise message ○ A set of changes to the code (diff) ● Good messages for good history: ○ Short description in imperative ○ Blank line ○ Long description ● Commit ID: SHA1 ○ Hash: means it’s unique! ○ It encodes history commit 2bedc34e012778ca0abb70c14e9e40d13a81f53e Author: Violeta Menendez Gonzalez <v.menendezgonzalez@surrey.ac.uk> Date: Wed May 25 15:54:31 2022 +0100 Check if cuda available before using diff --git a/train.py b/train.py index bc31650..10ebd4f 100644 --- a/train.py +++ b/train.py @@ -572,4 +572,5 @@ if __name__ == '__main__': try: main() finally: - print(torch.cuda.memory_summary(abbreviated=True)) + if torch.cuda.is_available(): + print(torch.cuda.memory_summary(abbreviated=True))
  • 10. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk History is editable ● Vital for good history ● Local history is editable: ○ Don’t modify history that has been pushed to the shared repo ● History needs to be clean: ○ Linear: commits don’t depend on later commits, we don’t want commits to break the system ○ Atomic: commits need to be concise, fixing only one bug or implementing only one feature. Pro tip: if the commit message contains the word ‘and’ maybe think of breaking it down!
  • 11. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Workflow ● Work in branches (feature branches) ● Main branch (master) for tested and clean changes
  • 12. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Workflow ● Commits have an order, you may need to re-order to have a sensible history ● HEAD of a branch is the last commit
  • 14. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Starting your repo ● git init - create a git repository ● git add <file> [<file2>...] - add files to the index for commit ● git commit - creates a commit with all the files in the staging area. Prompts for a command message ○ Pro tip: git commit -m “Short concise message” to make the message faster ● git log - shows all the commits in your current branch ○ Pro tip: git log -p - to show the commit including code changes (diff) ○ Pro tip: git log --oneline - to show the logs in short mode ● git show <revision> - show the diff of a specific commit
  • 15. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Working area ● git status - shows the status of your working area, files added, changed, removed, conflicts, files staged… ● git diff - shows all code changes in your working area. You may have several commits worth of changes ● git add -p - add individual changes to the index ● git diff --cached - show all changes that have been staged. If there’s something you don’t like you can make changes ● git reset HEAD <file> - remove changes in a file from the index
  • 16. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Workflow You have several commits worth of code changes in your working tree ● git diff - see changes ● git add -p - add individual changes ● git diff --cached - double check changes you have staged for commit ● git reset HEAD <file> - remove any changes you don’t like from the index ● git add -p - add any changes you missed ● git commit - commit changes with a good commit message ● git log -p - double check your commit looks OK ● git commit --amend - change commit at HEAD of current branch Iterate until you have added all changes
  • 17. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 1: Starting a repo
  • 18. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Working on branches ● Branches can be just local ● Very fast to create ● Very fast to switch between ● Very convenient for working ● Modular changes ● Consist of one or more commits ● Can be “moved around” ● Default branch is “master”, like the main trunk of the repository tree
  • 19. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Branches ● git branch - show all branches in your repo (* star on you current branch) ● git branch <name> - creates branch (but not checked out) ○ Pro tip: follow a good and clear name system that describes the feature you’re developing. If you are carrying out different independent changes, think of creating different branches and switch between them. If you’re working with other people it can be useful to use the (?) name/feature (i.e., violeta/data_validation) ● git checkout <branch_name> - checkout to branch ○ Pro tip: git checkout -b <name> - will create the branch AND checkout to it in one
  • 20. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Merge Once your changes are tested and the history on your branch is clean, we can incorporate them to the main branch “master”. ● git checkout master ● git merge <branch> - merges branch ONTO master ○ You merge a branch onto your current branch, doesn’t have to be master. Make sure you check what your current branch is!! NOTE: This can cause merge conflicts. DON’T PANIC git status will tell us what to do
  • 21. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 2: Branch and merge
  • 22. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Merge conflicts ● They can occur when we try to modify the same line in two different ways ● git status - shows which file has the conflict ● Edit file: ○ You can keep the current changes ○ You can keep the old changes ○ Or you can modify them to look how you like
  • 23. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Revisions Revisions are revised versions of our code. Each commit represents a revision of our code. ● git checkout <commit> - checkout a specific revision of our code. We are in a ‘detached HEAD’, we are not in a branch any more. ● We can make experimental changes, build or test our system without impacting any branches. ● git checkout -b <name> - if we want to save the new commits in a branch or ● git checkout <previos_branch> - to go back to where we were ○ Pro Tip: you can also use ‘git checkout -’
  • 24. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Stash If we have uncommitted changes in our index and we want to checkout a different revision, they may go into conflict! ● git stash - stashes all changes “away”, leaving a clean working directory ● git stash list - shows all stashed changes ● git stash apply - to restore the changes you saved ● git stash pop - apply changes AND remove them from the list
  • 25. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Resetting revision ● git reset <commit> - set your current revision and index area to a certain state ○ --mixed - Resets index but not working tree. Sets HEAD to commit, discards all history, changes are kept in working tree. (default) ○ --soft - Leaves index and working tree untouched. Sets HEAD to commit, discards all history, leaves changes staged for commit ○ --hard - Resets both index and working tree. Resets HEAD, history AND working tree. You lose all code!!!
  • 26. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 3: stash and reset
  • 27. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Rebase Rebase is used to integrate history from one branch into another branch. History is rewritten, we want to rewrite it to make it better! ● You have a working branch ● Someone else pushes to master in the remote repo ● You want to push your changes, but they may conflict with the remote repo ● You want to update the repo to reflect the other person’s changes
  • 28. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Rebase ● git rebase <branch> - rebases your current branch over branch. All commits from branch are now in your history. ○ git checkout branchA ○ git rebase master There may be conflicts (because we may have changes that apply to places that are different now due to the new commits!) BUT
  • 29. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk
  • 30. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Resolve conflicts ● git status will tell you which files are in conflict! ● Make relevant changes ● git add <your changes> ● git rebase --continue If we changed our minds: ● git rebase --abort
  • 31. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 4: Rebase and solve conflict Note: commit SHAs change, as history has changed
  • 32. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Interactive rebase ● More flexible rebase ● Useful for: ○ Integrating changes from code review ○ Making history simple and linear ○ Making every point in history valid ○ Making sure commits don’t undo each other ○ No “oops” commits
  • 33. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Interactive rebase ● git rebase -i <commit> - rebase all commits newer than <commit> Opens a list of commits with options to modify, reorder or delete them: ● p (pick) - leave as is (default) ● r (reword) - change message ● e (edit) - modify contents of commit ● s (squash) - combine with previous commit CAREFUL: rebase is destructive. Make sure you in the correct branch, and back up!
  • 34. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Splitting commits ● Mark commit for edit ● git reset HEAD^ - reset log to previous commit leaving changes in tree ● git add -p ● git commit ● Repeat until done ● git rebase --continue
  • 35. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 5: Interactive rebase and split commit
  • 36. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Debugging ● git blame <file> - shows the commit that introduced each line of a file. ● git bisect - Amazing debugging tool. Prompts you to give a good and a bad commit, then uses a bisecting algorithm to checkout commits in between. You then run your test and mark the commit as good/bad. After a few iterations git tells you which commit introduced the bug!! Magic
  • 37. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Debugging ● git blame <file> - shows the commit that introduced each line of a file. ● git bisect - Amazing debugging tool. Prompts you to give a good and a bad commit, then uses a bisecting algorithm to checkout commits in between. You then run your test and mark the commit as good/bad. After a few iterations git tells you which commit introduced the bug!! Magic
  • 38. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Debugging ● git blame <file> - shows the commit that introduced each line of a file. ● git bisect - Amazing debugging tool. Prompts you to give a good and a bad commit, then uses a bisecting algorithm to checkout commits in between. You then run your test and mark the commit as good/bad. After a few iterations git tells you which commit introduced the bug!! Magic
  • 39. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Debugging ● git blame <file> - shows the commit that introduced each line of a file. ● git bisect - Amazing debugging tool. Prompts you to give a good and a bad commit, then uses a bisecting algorithm to checkout commits in between. You then run your test and mark the commit as good/bad. After a few iterations git tells you which commit introduced the bug!! Magic
  • 40. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Ranges ● git diff <commit2>..<commit4> - shows all changes between two commits (older..newer) ○ git diff <commit2>..HEAD - all changes from commit2 until present ● git log <commit2>..<commit4> - shows all commits between commit2 and commit 4
  • 41. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Remotes ● Remotes are repositories that are backed up in some server (e.g. GitHub ) ● Use for collaboration and backup ● git clone <URL> - retrieves a repository into a directory ○ ssh://[user@]host.xz[:port]/path/to/repo.git/ ○ git://host.xz[:port]/path/to/repo.git/ ○ http[s]://host.xz[:port]/path/to/repo.git/ git is the baaeest
  • 42. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Remotes ● You can have several remotes. Default is origin. ○ e.g., If you want to make changes to a repo that you don’t want to be public ● git add remote <name> <remote> - adds a new remote ● git checkout <remote>/<branch> - checks out a branch from a specific remote ● git remote -v - show tracked remotes ● git rename <old> <new> - change name of remote $ git remote -v origin gitlab@gitlab.eps.surrey.ac.uk:violeta/mystery-project.git (fetch) origin gitlab@gitlab.eps.surrey.ac.uk:violeta/mystery-project.git (push) upstream git@github.com:alex/mystery-project.git (fetch) upstream git@github.com:alex/mystery-project.git (push)
  • 43. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Remotes ● git fetch [<remote>] - retrieves all references, doesn’t modify working copy ● git pull [<remote> [<branch>]] - retrieves all changes from remote into your local branch (default: origin, master) ● git push [<remote> [<branch>]] - pushes your local changes to branch in remote Attention!! Pull can create conflicts
  • 44. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 6: Remotes
  • 45. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk If everything else fails…
  • 46.
  • 47. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Reflog Git keeps track of every single branch change, and stores it in the Reference Log ● Each change is an entry in the reflog with a reference ● The state can be restored to any reference ● If you mess up… just reset it back to before it happened! ● Only local ● git reflog - list all entries ● git reset <sha1> Rebase Pull
  • 48. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Demo 7: Reflog
  • 49. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Resources ● Documentation: https://git-scm.com/ ● git <command> --help ● Man pages: ○ man git-log ○ man git-add ○ etc ● Stackoverflow is your friend ● Special thanks to Alan Ott’s talk Git like a Pro: https://youtu.be/H2annVAHKPE?t=19199
  • 50. Violeta Menéndez González v.menendezgonzalez@surrey.ac.uk Thank you!!! ● Next Monthly Mini Hack in June - to be announced