SlideShare a Scribd company logo
1 of 65
Download to read offline
Version Control using Git
Anoop Kumar Malav
Atlogys Technical Consulting
anoop@atlogys.com
February 8, 2018
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 1 / 65
Git: Original Author
Linus Torvalds
https://en.wikipedia.org/wiki/Linus_Torvalds
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 2 / 65
Overview
1 About Version Control
2 Git Basics
The Three States
Getting a Git Repository
Recording Changes to the Repository
Viewing Your Staged and Unstaged Changes
Committing Your Changes
Undoing Things
Working with Remotes
Tagging
3 Git Branching
Introduction
Branch Merging
Remote Branches
4 Git Tools
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 3 / 65
About Version Control
Version control is a system that records changes to a file or set of files
over time so that you can recall specific versions later.
It allows you to
revert selected files back to a previous state,
revert the entire project back to a previous state,
compare changes over time,
who last modified something that might be causing a problem,
who introduced an issue and when,
and more.
Using a VCS also generally means that if you screw things up or lose
files, you can easily recover.
In addition, you get all this for very little overhead.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 4 / 65
About Version Control: Local VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 5 / 65
About Version Control: Centralized VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 6 / 65
About Version Control: Distributed VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 7 / 65
Git Basics: The Three States
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 8 / 65
Git Basics: Getting a Git Repository
You typically obtain a Git repository in one of two ways:
You can take a local directory that is currently not under version
control, and turn it into a Git repository, or
You can clone an existing Git repository from elsewhere.
In either case, you end up with a Git repository on your local machine,
ready for work.
Example (Initializing a Repository in an Existing Directory)
$ cd /home/user/my_project
$ git init
$ git add *.c; $ git add LICENSE
$ git commit -m ’initial project version’
Example (Cloning an Existing Repository)
$ git clone https://github.com/libgit2/libgit2 mylibgit
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 9 / 65
Git Basics: Getting a Git Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 10 / 65
Git Basics: Getting a Git Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 11 / 65
Git Basics: Recording Changes to the Repository
The lifecycle of the status of your files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 12 / 65
Git Basics: Recording Changes to the Repository
Checking the Status of Your Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 13 / 65
Git Basics: Recording Changes to the Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 14 / 65
Git Basics: Recording Changes to the Repository
Tracking New Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 15 / 65
Git Basics: Recording Changes to the Repository
Staging Modified Files
- Modify previously tracked file called CONTRIBUTING.md
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 16 / 65
Git Basics: Recording Changes to the Repository
Staging Modified Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 17 / 65
Ignoring Files
→ Class of files that you don’t want Git to automatically add or even show you as being
untracked.
→ Setting up a .gitignore file for your new repository before you get going is generally a
good idea so you don’t accidentally commit files that you really don’t want in your Git
repository.
→ In the simple case, a repository might have a single .gitignore file in its root directory,
which applies recursively to the entire repository.
→ However, it is also possible to have additional .gitignore files in subdirectories.
The rules for the patterns you can put in the .gitignore file are as follows:
Blank lines or lines starting with # are ignored.
Standard glob patterns work, and will be applied recursively throughout the entire working tree.
You can start patterns with a forward slash (/) to avoid recursivity.
You can end patterns with a forward slash (/) to specify a directory.
You can negate a pattern by starting it with an exclamation point (!).
Note: GitHub maintains a fairly comprehensive list of good .gitignore file examples for
dozens of projects and languages at https://github.com/github/gitignore
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 18 / 65
Viewing Your Staged and Unstaged Changes
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 19 / 65
Viewing Your Staged and Unstaged Changes
diff options –staged and –cached are synonyms.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 20 / 65
Committing Your Changes
- The simplest way to commit is to type git commit. Doing so launches your editor of
choice.
- Alternatively, you can type your commit message inline with the commit command by
specifying it after a -m flag, like this:
$ git commit -m "Story 182: Fix benchmarks for speed"
- If you want to skip the staging area, Git provides a simple shortcut.
- Adding the -a option to the git commit command makes Git automatically stage every
file that is already tracked before doing the commit, letting you skip the git add part:
$ git commit -a -m ’added new benchmarks’
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 21 / 65
Removing Files
- To remove a file from Git, you have to remove it from your tracked files and then
commit.
- The git rm command does that, and also removes the file from your working directory
so you don’t see it as an untracked file the next time around.
- To keep the file in your working tree but remove it from your staging area, use the
–cached option:
$ git rm –cached README
- This is particularly useful if you forgot to add something to your .gitignore file and
accidentally staged it.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 22 / 65
View Commit History
The most basic and powerful tool to do this is the git log command.
git log branchname
git log filename
git log -n
git log -p
git log –author
git log –grep=pattern
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 23 / 65
View Commit History
git log –all –pretty=oneline –graph –abbrev-commit –decorate
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 24 / 65
View Commit History
There are many graphical interface to view commit history. One of these is gitk.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 25 / 65
Git Basics: Undoing Things
Commit Amend (Redo commit)
One of the common undos takes place when you commit too early and possibly forget to
add some files, or you mess up your commit message.
If you want to redo that commit, make the additional changes you forgot, stage them,
and commit again using the –amend option:
$ git commit –amend
It’s important to understand that when you’re amending your last commit, you’re not so
much fixing it as replacing it entirely with a new, improved commit that pushes the old
commit out of the way and puts the new commit in its place.
Effectively, it’s as if the previous commit never happened, and it won’t show up in your
repository history.
Do not amend commit if you have already pushed your code on remote.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 26 / 65
Git Basics: Undoing Things
Unstaging a Staged File
Let’s say you’ve changed two files and want to commit them as two separate changes,
but you accidentally type git add * and stage them both.
How can you unstage one of the two? The git status command reminds you:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 27 / 65
Git Basics: Undoing Things
Unmodifying a Modified File
Let’s say you don’t want to keep your changes to the CONTRIBUTING.md file.
It’s important to understand that git checkout – <file>is a dangerous command.
Any changes you made to that file are gone.
git checkout command is used in two context. The first is to revert changes as in above.
But git checkout is also used to switch to a branch.
To avoid ambiguity, it is best practice to put in the double dash to indicate that we are
not switching branch and that we want to stay on current branch.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 28 / 65
Git Basics: Working with Remotes
To be able to collaborate on any Git project, you need to know how to manage your
remote repositories.
Remote repositories are versions of your project that are hosted on the Internet or
network somewhere.
Showing Your Remotes
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 29 / 65
Git Basics: Working with Remotes
Adding Remote Repositories
git remote add <shortname><url>
Fetching and Pulling from Your Remotes
Pushing to Your Remotes
git push <remote><branch>
Renaming and Removing Remotes
git remote rename <oldname><newname>
git remote remove <shortname>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 30 / 65
Git Basics: Tagging
→ Git has the ability to tag specific points in history as being important.
→ Typically people use this functionality to mark release points.
Creating Tags
Git supports two types of tags: lightweight and annotated.
A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a
specific commit.
git tag <tagname>
Annotated tags, however, are stored as full objects in the Git database. They contain the
tagger name, email, and date; have a tagging message.
git tag -a <tagname>-m <message>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 31 / 65
Git Basics: Tagging
Listing Your Tags
git tag
Sharing Tags
git push origin <tagname>
If you have a lot of tags that you want to push up at once, you can also use the –tags option to
the git push command.
git push origin –tags
Checking out Tags
If you want to view the versions of files a tag is pointing to, you can do a git checkout, though
this puts your repository in “detached HEAD“ state.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 32 / 65
Git Branching: Killer Feature
The way Git branches
is incredibly lightweight,
making branching operations nearly instantaneous, and
switching back and forth between branches generally just as fast.
→ A branch in Git is simply a lightweight movable pointer to one of the
commits.
→ The default branch name in Git is master.
→ As you start making commits, you’re given a master branch that points
to the last commit you made.
→ Every time you commit, it moves forward automatically.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 33 / 65
Git Branching: Killer Feature
How does Git know what branch you’re currently on? It keeps a special
pointer called HEAD.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 34 / 65
Git Branching
Creating a New Branch
git branch <branchname>
git checkout -b <branchname>
Switching Branches
git checkout <branchname>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 35 / 65
Branch Merging: Fast Forwarding
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 36 / 65
Branch Merging: Fast Forwarding
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 37 / 65
Branch Merging: Merge Commit
git merge <branchtomerge>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 38 / 65
Branch Merging: Merge Commit
When merging a Branch, make sure your working directory is clean. There
is a chance to loosing your uncommited work.
In case of merge conflict people make mistake to commit changes which
are not present in either of the branch.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 39 / 65
Branch Merging: Merge Conflict
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 40 / 65
Branch Merging: Merge Conflict
Resolving conflict using git
git checkout –ours <filename>
git checkout –theirs <filename>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 41 / 65
Branch Merging: Rebase then Fast Forward
git rebase <onbranch>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 42 / 65
Branch Merging: Rebase then Fast Forward
git checkout master; git merge experiment
git checkout experiment; git rebase master
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 43 / 65
Branch Merging: Rebase then Fast Forward
git checkout experiment; git rebase master
git checkout master; git merge experiment
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 44 / 65
Branch Merging: Rebase vs. Merge
The Perils of Rebasing
Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single
line:
Do not rebase commits that exist outside your repository.
If you follow that guideline, you’ll be fine.
In general the way to get the best of both worlds is to rebase local changes you’ve made but
haven’t shared yet before you push them in order to clean up your story, but never rebase
anything you’ve pushed somewhere.
Rule of thumb:
When pulling changes from origin/develop onto your local develop use rebase. (git pull
–rebase origin develop)
When finishing a feature branch merge the changes back to develop. (git merge –no-ff
feature)
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 45 / 65
Git Branching: Remote Branches
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 46 / 65
Git Branching: Remote Branches
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 47 / 65
Git Branching: Remote Branches
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 48 / 65
Git Branching: Remote Branches
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 49 / 65
Git Branching: Remote Branches
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 50 / 65
Staging Patches
It’s also possible for Git to stage certain parts of files and not the rest. For example, if
you make two changes to your simplegit.rb file and want to stage one of them and not
the other, doing so is very easy in Git.
git add -p -A
You have a lot of options at this point. Typing ? shows a list of what you can do:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 51 / 65
Staging Patches
Generally, you’ll type y or n if you want to stage each hunk, but staging all of them in
certain files or skipping a hunk decision until later can be helpful too.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 52 / 65
Git Stashing and Cleaning
Often, when you’ve been working on part of your project, things are in a messy state
and you want to switch branches for a bit to work on something else.
The problem is, you don’t want to do a commit of half-done work just so you can get
back to this point later.
git stash
Apply Stash
git stash apply
Apply and Drop Stash
git stash pop
Cleaning your Working Directory
Dry Run
git clean -d -X -n
Cleanup
git clean -d -X -f
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 53 / 65
Cherry-Pick
$ git cherry-pick e43a6
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 54 / 65
Git Reset
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 55 / 65
Git Reset
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 56 / 65
Git Reset
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 57 / 65
Git Reset
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 58 / 65
Git Revert
$ git revert 8d78c0f
Given one or more existing commits, reverts the changes that the related patches
introduce, and record some new commits that record them.
This requires your working tree to be clean (no modifications from the HEAD commit).
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 59 / 65
Interactive Rebase
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 60 / 65
Undoing Merges: Reset
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 61 / 65
Undoing Merges: Revert
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 62 / 65
Debugging with Git: File Annotation
If you track down a bug in your code and want to know when it was introduced and why, file
annotation is often your best tool.
It shows you what commit was the last to modify each line of any file.
The following example uses git blame to determine which commit and committer was
responsible for lines in the top-level Linux kernel Makefile and, further, uses the -L option to
restrict the output of the annotation to lines 69 through 82 of that file:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 63 / 65
Useful Resources
Documentation Book
https://git-scm.com/book/en/v2/
GitHub Cheat Sheet
https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
Visual Git Cheat Sheet
http://ndpsoftware.com/git-cheatsheet.html
Branching Workflows
https://www.atlassian.com/git/tutorials/comparing-workflows
Best Practices
1 https://www.git-tower.com/blog/version-control-best-practices/
2 https://www.lullabot.com/articles/git-best-practices-workflow-guidelines
3 https://blog.hartleybrody.com/git-small-teams/
4 https://sethrobertson.github.io/GitBestPractices/
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 64 / 65
Thanks for your attention!
Any questions?
Hope you slept comfortably!
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 65 / 65

More Related Content

What's hot

Spring Projects Infrastructure
Spring Projects InfrastructureSpring Projects Infrastructure
Spring Projects InfrastructureGunnar Hillert
 
An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)John Anderson
 
Introduction to git, an efficient distributed version control system
Introduction to git, an efficient distributed version control systemIntroduction to git, an efficient distributed version control system
Introduction to git, an efficient distributed version control systemAlbanLevy
 
Git tutorial undoing changes
Git tutorial   undoing changesGit tutorial   undoing changes
Git tutorial undoing changesLearningTech
 
Stable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code ReviewStable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code ReviewLuca Milanesio
 
Git 101 Workshop
Git 101 WorkshopGit 101 Workshop
Git 101 WorkshopJoy Seng
 
Introduction to Github for Team Project
Introduction to Github for Team ProjectIntroduction to Github for Team Project
Introduction to Github for Team ProjectAkhter Al Amin
 

What's hot (20)

Spring Projects Infrastructure
Spring Projects InfrastructureSpring Projects Infrastructure
Spring Projects Infrastructure
 
Git real slides
Git real slidesGit real slides
Git real slides
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)
 
Git presentation
Git presentationGit presentation
Git presentation
 
Git
GitGit
Git
 
Introduction to git, an efficient distributed version control system
Introduction to git, an efficient distributed version control systemIntroduction to git, an efficient distributed version control system
Introduction to git, an efficient distributed version control system
 
Git
GitGit
Git
 
Git n git hub
Git n git hubGit n git hub
Git n git hub
 
Git github
Git githubGit github
Git github
 
Git tutorial undoing changes
Git tutorial   undoing changesGit tutorial   undoing changes
Git tutorial undoing changes
 
Git training
Git trainingGit training
Git training
 
Stable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code ReviewStable master workflow with Gerrit Code Review
Stable master workflow with Gerrit Code Review
 
Git & Github
Git & GithubGit & Github
Git & Github
 
Git 101 Workshop
Git 101 WorkshopGit 101 Workshop
Git 101 Workshop
 
Git-r-Done
Git-r-DoneGit-r-Done
Git-r-Done
 
Introduction to Github for Team Project
Introduction to Github for Team ProjectIntroduction to Github for Team Project
Introduction to Github for Team Project
 
setting up a repository using GIT
setting up a repository using GITsetting up a repository using GIT
setting up a repository using GIT
 
Git essentials
Git essentialsGit essentials
Git essentials
 
Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)Introduction to Git (Greg Lonnon)
Introduction to Git (Greg Lonnon)
 

Similar to Git: Version Control and Collaboration

Git tutorial
Git tutorialGit tutorial
Git tutorialmobaires
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | WorkshopAnuchit Chalothorn
 
Git from the trenches
Git from the trenchesGit from the trenches
Git from the trenchesNuno Caneco
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubDSC GVP
 
Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?9 series
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commandsZakaria Bouazza
 
Mastering git - Workflow
Mastering git - WorkflowMastering git - Workflow
Mastering git - WorkflowTahsin Abrar
 
Introducing Git and git flow
Introducing Git and git flow Introducing Git and git flow
Introducing Git and git flow Sebin Benjamin
 
Git slides
Git slidesGit slides
Git slidesNanyak S
 
Git Tutorial A Comprehensive Guide for Beginners.pdf
Git Tutorial A Comprehensive Guide for Beginners.pdfGit Tutorial A Comprehensive Guide for Beginners.pdf
Git Tutorial A Comprehensive Guide for Beginners.pdfuzair
 
git-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfgit-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfmurad khan
 
Git cheat sheet
Git cheat sheetGit cheat sheet
Git cheat sheetLam Hoang
 
Introduction to GitHub, Open Source and Tech Article
Introduction to GitHub, Open Source and Tech ArticleIntroduction to GitHub, Open Source and Tech Article
Introduction to GitHub, Open Source and Tech ArticlePRIYATHAMDARISI
 

Similar to Git: Version Control and Collaboration (20)

Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
 
Git from the trenches
Git from the trenchesGit from the trenches
Git from the trenches
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
 
Github By Nyros Developer
Github By Nyros DeveloperGithub By Nyros Developer
Github By Nyros Developer
 
16 Git
16 Git16 Git
16 Git
 
Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?
 
1-Intro to VC & GIT PDF.pptx
1-Intro to VC & GIT PDF.pptx1-Intro to VC & GIT PDF.pptx
1-Intro to VC & GIT PDF.pptx
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commands
 
Mastering git - Workflow
Mastering git - WorkflowMastering git - Workflow
Mastering git - Workflow
 
Do you git it
Do you git it Do you git it
Do you git it
 
Introducing Git and git flow
Introducing Git and git flow Introducing Git and git flow
Introducing Git and git flow
 
Git slides
Git slidesGit slides
Git slides
 
Git Tutorial A Comprehensive Guide for Beginners.pdf
Git Tutorial A Comprehensive Guide for Beginners.pdfGit Tutorial A Comprehensive Guide for Beginners.pdf
Git Tutorial A Comprehensive Guide for Beginners.pdf
 
git-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfgit-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdf
 
Git cheat sheet
Git cheat sheetGit cheat sheet
Git cheat sheet
 
Introduction to GitHub, Open Source and Tech Article
Introduction to GitHub, Open Source and Tech ArticleIntroduction to GitHub, Open Source and Tech Article
Introduction to GitHub, Open Source and Tech Article
 

More from Atlogys Technical Consulting

BDD and Test Automation Tech Talk - Atlogys Academy Series
BDD and Test Automation Tech Talk - Atlogys Academy SeriesBDD and Test Automation Tech Talk - Atlogys Academy Series
BDD and Test Automation Tech Talk - Atlogys Academy SeriesAtlogys Technical Consulting
 
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)Atlogys Technical Consulting
 
Infinite Scaling using Lambda and Aws - Atlogys Tech Talk
Infinite Scaling using Lambda and Aws - Atlogys Tech TalkInfinite Scaling using Lambda and Aws - Atlogys Tech Talk
Infinite Scaling using Lambda and Aws - Atlogys Tech TalkAtlogys Technical Consulting
 
Atlogys - Don’t Just Sell Technology, Sell The Experience!
Atlogys - Don’t Just Sell Technology, Sell The Experience!Atlogys - Don’t Just Sell Technology, Sell The Experience!
Atlogys - Don’t Just Sell Technology, Sell The Experience!Atlogys Technical Consulting
 

More from Atlogys Technical Consulting (20)

Latest UI guidelines for Web Apps
Latest UI guidelines for Web AppsLatest UI guidelines for Web Apps
Latest UI guidelines for Web Apps
 
Discipline at Atlogys
Discipline at AtlogysDiscipline at Atlogys
Discipline at Atlogys
 
Reprogram your mind for Positive Thinking
Reprogram your mind for Positive ThinkingReprogram your mind for Positive Thinking
Reprogram your mind for Positive Thinking
 
Docker @ Atlogys
Docker @ AtlogysDocker @ Atlogys
Docker @ Atlogys
 
Tests for Scalable, Fast, Secure Apps
Tests for Scalable, Fast, Secure AppsTests for Scalable, Fast, Secure Apps
Tests for Scalable, Fast, Secure Apps
 
Atomic Design with PatternLabs
Atomic Design with PatternLabsAtomic Design with PatternLabs
Atomic Design with PatternLabs
 
Guidelines HTML5 & CSS3 - Atlogys (2018)
Guidelines HTML5 & CSS3 - Atlogys (2018)Guidelines HTML5 & CSS3 - Atlogys (2018)
Guidelines HTML5 & CSS3 - Atlogys (2018)
 
Rabbit MQ - Tech Talk at Atlogys
Rabbit MQ - Tech Talk at Atlogys Rabbit MQ - Tech Talk at Atlogys
Rabbit MQ - Tech Talk at Atlogys
 
BDD and Test Automation Tech Talk - Atlogys Academy Series
BDD and Test Automation Tech Talk - Atlogys Academy SeriesBDD and Test Automation Tech Talk - Atlogys Academy Series
BDD and Test Automation Tech Talk - Atlogys Academy Series
 
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)
QA Best Practices at Atlogys - Tech Talk (Atlogys Academy)
 
Infinite Scaling using Lambda and Aws - Atlogys Tech Talk
Infinite Scaling using Lambda and Aws - Atlogys Tech TalkInfinite Scaling using Lambda and Aws - Atlogys Tech Talk
Infinite Scaling using Lambda and Aws - Atlogys Tech Talk
 
How Solr Search Works
How Solr Search WorksHow Solr Search Works
How Solr Search Works
 
Wordpress Tech Talk
Wordpress Tech Talk Wordpress Tech Talk
Wordpress Tech Talk
 
Tech Talk on ReactJS
Tech Talk on ReactJSTech Talk on ReactJS
Tech Talk on ReactJS
 
Atlogys Academy - Tech Talk on Mongo DB
Atlogys Academy - Tech Talk on Mongo DBAtlogys Academy - Tech Talk on Mongo DB
Atlogys Academy - Tech Talk on Mongo DB
 
Atlogys Tech Talk - Web 2.0 Design Guidelines
Atlogys Tech Talk - Web 2.0 Design GuidelinesAtlogys Tech Talk - Web 2.0 Design Guidelines
Atlogys Tech Talk - Web 2.0 Design Guidelines
 
Firebase Tech Talk By Atlogys
Firebase Tech Talk By AtlogysFirebase Tech Talk By Atlogys
Firebase Tech Talk By Atlogys
 
Atlogys - Don’t Just Sell Technology, Sell The Experience!
Atlogys - Don’t Just Sell Technology, Sell The Experience!Atlogys - Don’t Just Sell Technology, Sell The Experience!
Atlogys - Don’t Just Sell Technology, Sell The Experience!
 
Smart CTO Service
Smart CTO ServiceSmart CTO Service
Smart CTO Service
 
Atlogys Technical Consulting
Atlogys Technical ConsultingAtlogys Technical Consulting
Atlogys Technical Consulting
 

Recently uploaded

My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

Git: Version Control and Collaboration

  • 1. Version Control using Git Anoop Kumar Malav Atlogys Technical Consulting anoop@atlogys.com February 8, 2018 Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 1 / 65
  • 2. Git: Original Author Linus Torvalds https://en.wikipedia.org/wiki/Linus_Torvalds Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 2 / 65
  • 3. Overview 1 About Version Control 2 Git Basics The Three States Getting a Git Repository Recording Changes to the Repository Viewing Your Staged and Unstaged Changes Committing Your Changes Undoing Things Working with Remotes Tagging 3 Git Branching Introduction Branch Merging Remote Branches 4 Git Tools Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 3 / 65
  • 4. About Version Control Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 4 / 65
  • 5. About Version Control: Local VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 5 / 65
  • 6. About Version Control: Centralized VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 6 / 65
  • 7. About Version Control: Distributed VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 7 / 65
  • 8. Git Basics: The Three States Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 8 / 65
  • 9. Git Basics: Getting a Git Repository You typically obtain a Git repository in one of two ways: You can take a local directory that is currently not under version control, and turn it into a Git repository, or You can clone an existing Git repository from elsewhere. In either case, you end up with a Git repository on your local machine, ready for work. Example (Initializing a Repository in an Existing Directory) $ cd /home/user/my_project $ git init $ git add *.c; $ git add LICENSE $ git commit -m ’initial project version’ Example (Cloning an Existing Repository) $ git clone https://github.com/libgit2/libgit2 mylibgit Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 9 / 65
  • 10. Git Basics: Getting a Git Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 10 / 65
  • 11. Git Basics: Getting a Git Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 11 / 65
  • 12. Git Basics: Recording Changes to the Repository The lifecycle of the status of your files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 12 / 65
  • 13. Git Basics: Recording Changes to the Repository Checking the Status of Your Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 13 / 65
  • 14. Git Basics: Recording Changes to the Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 14 / 65
  • 15. Git Basics: Recording Changes to the Repository Tracking New Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 15 / 65
  • 16. Git Basics: Recording Changes to the Repository Staging Modified Files - Modify previously tracked file called CONTRIBUTING.md Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 16 / 65
  • 17. Git Basics: Recording Changes to the Repository Staging Modified Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 17 / 65
  • 18. Ignoring Files → Class of files that you don’t want Git to automatically add or even show you as being untracked. → Setting up a .gitignore file for your new repository before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository. → In the simple case, a repository might have a single .gitignore file in its root directory, which applies recursively to the entire repository. → However, it is also possible to have additional .gitignore files in subdirectories. The rules for the patterns you can put in the .gitignore file are as follows: Blank lines or lines starting with # are ignored. Standard glob patterns work, and will be applied recursively throughout the entire working tree. You can start patterns with a forward slash (/) to avoid recursivity. You can end patterns with a forward slash (/) to specify a directory. You can negate a pattern by starting it with an exclamation point (!). Note: GitHub maintains a fairly comprehensive list of good .gitignore file examples for dozens of projects and languages at https://github.com/github/gitignore Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 18 / 65
  • 19. Viewing Your Staged and Unstaged Changes Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 19 / 65
  • 20. Viewing Your Staged and Unstaged Changes diff options –staged and –cached are synonyms. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 20 / 65
  • 21. Committing Your Changes - The simplest way to commit is to type git commit. Doing so launches your editor of choice. - Alternatively, you can type your commit message inline with the commit command by specifying it after a -m flag, like this: $ git commit -m "Story 182: Fix benchmarks for speed" - If you want to skip the staging area, Git provides a simple shortcut. - Adding the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part: $ git commit -a -m ’added new benchmarks’ Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 21 / 65
  • 22. Removing Files - To remove a file from Git, you have to remove it from your tracked files and then commit. - The git rm command does that, and also removes the file from your working directory so you don’t see it as an untracked file the next time around. - To keep the file in your working tree but remove it from your staging area, use the –cached option: $ git rm –cached README - This is particularly useful if you forgot to add something to your .gitignore file and accidentally staged it. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 22 / 65
  • 23. View Commit History The most basic and powerful tool to do this is the git log command. git log branchname git log filename git log -n git log -p git log –author git log –grep=pattern Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 23 / 65
  • 24. View Commit History git log –all –pretty=oneline –graph –abbrev-commit –decorate Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 24 / 65
  • 25. View Commit History There are many graphical interface to view commit history. One of these is gitk. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 25 / 65
  • 26. Git Basics: Undoing Things Commit Amend (Redo commit) One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the –amend option: $ git commit –amend It’s important to understand that when you’re amending your last commit, you’re not so much fixing it as replacing it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place. Effectively, it’s as if the previous commit never happened, and it won’t show up in your repository history. Do not amend commit if you have already pushed your code on remote. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 26 / 65
  • 27. Git Basics: Undoing Things Unstaging a Staged File Let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 27 / 65
  • 28. Git Basics: Undoing Things Unmodifying a Modified File Let’s say you don’t want to keep your changes to the CONTRIBUTING.md file. It’s important to understand that git checkout – <file>is a dangerous command. Any changes you made to that file are gone. git checkout command is used in two context. The first is to revert changes as in above. But git checkout is also used to switch to a branch. To avoid ambiguity, it is best practice to put in the double dash to indicate that we are not switching branch and that we want to stay on current branch. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 28 / 65
  • 29. Git Basics: Working with Remotes To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Showing Your Remotes Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 29 / 65
  • 30. Git Basics: Working with Remotes Adding Remote Repositories git remote add <shortname><url> Fetching and Pulling from Your Remotes Pushing to Your Remotes git push <remote><branch> Renaming and Removing Remotes git remote rename <oldname><newname> git remote remove <shortname> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 30 / 65
  • 31. Git Basics: Tagging → Git has the ability to tag specific points in history as being important. → Typically people use this functionality to mark release points. Creating Tags Git supports two types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a specific commit. git tag <tagname> Annotated tags, however, are stored as full objects in the Git database. They contain the tagger name, email, and date; have a tagging message. git tag -a <tagname>-m <message> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 31 / 65
  • 32. Git Basics: Tagging Listing Your Tags git tag Sharing Tags git push origin <tagname> If you have a lot of tags that you want to push up at once, you can also use the –tags option to the git push command. git push origin –tags Checking out Tags If you want to view the versions of files a tag is pointing to, you can do a git checkout, though this puts your repository in “detached HEAD“ state. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 32 / 65
  • 33. Git Branching: Killer Feature The way Git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast. → A branch in Git is simply a lightweight movable pointer to one of the commits. → The default branch name in Git is master. → As you start making commits, you’re given a master branch that points to the last commit you made. → Every time you commit, it moves forward automatically. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 33 / 65
  • 34. Git Branching: Killer Feature How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 34 / 65
  • 35. Git Branching Creating a New Branch git branch <branchname> git checkout -b <branchname> Switching Branches git checkout <branchname> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 35 / 65
  • 36. Branch Merging: Fast Forwarding Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 36 / 65
  • 37. Branch Merging: Fast Forwarding Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 37 / 65
  • 38. Branch Merging: Merge Commit git merge <branchtomerge> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 38 / 65
  • 39. Branch Merging: Merge Commit When merging a Branch, make sure your working directory is clean. There is a chance to loosing your uncommited work. In case of merge conflict people make mistake to commit changes which are not present in either of the branch. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 39 / 65
  • 40. Branch Merging: Merge Conflict Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 40 / 65
  • 41. Branch Merging: Merge Conflict Resolving conflict using git git checkout –ours <filename> git checkout –theirs <filename> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 41 / 65
  • 42. Branch Merging: Rebase then Fast Forward git rebase <onbranch> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 42 / 65
  • 43. Branch Merging: Rebase then Fast Forward git checkout master; git merge experiment git checkout experiment; git rebase master Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 43 / 65
  • 44. Branch Merging: Rebase then Fast Forward git checkout experiment; git rebase master git checkout master; git merge experiment Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 44 / 65
  • 45. Branch Merging: Rebase vs. Merge The Perils of Rebasing Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single line: Do not rebase commits that exist outside your repository. If you follow that guideline, you’ll be fine. In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere. Rule of thumb: When pulling changes from origin/develop onto your local develop use rebase. (git pull –rebase origin develop) When finishing a feature branch merge the changes back to develop. (git merge –no-ff feature) Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 45 / 65
  • 46. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 46 / 65
  • 47. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 47 / 65
  • 48. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 48 / 65
  • 49. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 49 / 65
  • 50. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 50 / 65
  • 51. Staging Patches It’s also possible for Git to stage certain parts of files and not the rest. For example, if you make two changes to your simplegit.rb file and want to stage one of them and not the other, doing so is very easy in Git. git add -p -A You have a lot of options at this point. Typing ? shows a list of what you can do: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 51 / 65
  • 52. Staging Patches Generally, you’ll type y or n if you want to stage each hunk, but staging all of them in certain files or skipping a hunk decision until later can be helpful too. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 52 / 65
  • 53. Git Stashing and Cleaning Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. git stash Apply Stash git stash apply Apply and Drop Stash git stash pop Cleaning your Working Directory Dry Run git clean -d -X -n Cleanup git clean -d -X -f Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 53 / 65
  • 54. Cherry-Pick $ git cherry-pick e43a6 Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 54 / 65
  • 55. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 55 / 65
  • 56. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 56 / 65
  • 57. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 57 / 65
  • 58. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 58 / 65
  • 59. Git Revert $ git revert 8d78c0f Given one or more existing commits, reverts the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit). Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 59 / 65
  • 60. Interactive Rebase Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 60 / 65
  • 61. Undoing Merges: Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 61 / 65
  • 62. Undoing Merges: Revert Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 62 / 65
  • 63. Debugging with Git: File Annotation If you track down a bug in your code and want to know when it was introduced and why, file annotation is often your best tool. It shows you what commit was the last to modify each line of any file. The following example uses git blame to determine which commit and committer was responsible for lines in the top-level Linux kernel Makefile and, further, uses the -L option to restrict the output of the annotation to lines 69 through 82 of that file: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 63 / 65
  • 64. Useful Resources Documentation Book https://git-scm.com/book/en/v2/ GitHub Cheat Sheet https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf Visual Git Cheat Sheet http://ndpsoftware.com/git-cheatsheet.html Branching Workflows https://www.atlassian.com/git/tutorials/comparing-workflows Best Practices 1 https://www.git-tower.com/blog/version-control-best-practices/ 2 https://www.lullabot.com/articles/git-best-practices-workflow-guidelines 3 https://blog.hartleybrody.com/git-small-teams/ 4 https://sethrobertson.github.io/GitBestPractices/ Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 64 / 65
  • 65. Thanks for your attention! Any questions? Hope you slept comfortably! Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 65 / 65