![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2586310/pasted-from-clipboard.png)
Source Code Management
Ashish Pandey
@ashishapy
blog.ashishapy.com
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2273043/pasted-from-clipboard.png)
Meet-up on 17th July 2016
@ Thoughtworks Pune
Agenda
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2811826/pasted-from-clipboard.png)
-
Git Introduction & Basics
-
Remote Repository (Cloud & Self) Hosting
-
Workflows
-
Branching
-
Git for DevOps
-
Github Features
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2712618/pasted-from-clipboard.png)
Distributed Version Control System (DVCS)
Command Line Interface (CLI)
Official Git site is https://git-scm.com
Good place to learn
Learn git in 15 mins https://try.github.io/
Project based learning https://learn.wheelhouse.io/
Learn from Github expert https://services.github.com/
Git tutorial https://www.atlassian.com/git/
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2848463/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2848469/pasted-from-clipboard.png)
Origin
- Git development began in April 2005, after many developers of the Linux kernel gave up access to BitKeeper
- Started as Linux Kernel project
- Torvalds started the project when none of the available free systems met his needs
- Install git from https://git-scm.com/
-
$ git help
-
$ git config --global user.name "Ashish Pandey"
-
$ git config --global user.email ashishapy@gmail.com
-
$ git config --global color.ui true
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
$ git config --global alias.<alias-name> <git-command>
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2848480/pasted-from-clipboard.png)
Starting a remote repo
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2815040/pasted-from-clipboard.png)
Starting a local repo
$ git init
$ git remote add origin https://github.com/ashishapy/git-scm-demo.git
$ git clone https://github.com/ashishapy/git-scm-demo.git
OR
$ git status
$ git remote -v
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
~/.gitconfig
./.git/
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2820137/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2272969/pasted-from-clipboard.png)
Git Flow
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2713184/pasted-from-clipboard.png)
$ git add -A
$ git commit -m "commit message"
$ git push
$ git pull
$ git checkout
$ git status
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
Different ways to add
$ git add <list of files>
$ git add --all / git add -A
$ git add *.txt
$ git add docs/*.txt
$ git add docs/
$ git add "*.txt"
Add all .txt files in the whole directory
git reset HEAD <file>
Add list of files
Add all files
Add .txt of files from current directory
Add .txt of files from docs directory
Add all files from docs directory
Unstage file changes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2713184/pasted-from-clipboard.png)
Make a good commit
Atomic
Consistent
Incremental
Documented
Semantically related changes should not be split across commits
Changes in a commit should be semantically related
A commit should not introduce compilation errors
A commit should not break existing tests nor add a failing one
Commit should be ordered deliberately
Commits should show programmers's thought process
A commit message should include a short summary
A commit description can have longer description
Changes after commit
$ git reset --soft HEAD^
$ git commit --amend -m "new commit message"
$ git reset --hard HEAD^
$ git reset --hard HEAD^^
Undo last commit, put changes into staging
Change the last commit
Undo last commit and all changes
Undo last 2 commits and all changes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2713677/pasted-from-clipboard.png)
After you Push
$ git rebase <base>
Move a branch to a new base commit
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2713184/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2272976/pasted-from-clipboard.png)
Looking at History
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2713827/pasted-from-clipboard.png)
SHA hash
commit message
$ git log --oneline [-p] [--stat] [--graph]
$ git log --pretty="%h %ad- %s [%an]"
Run "git help log" for more options
$ git log --since=1.month.ago --until=2.weeks.ago
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
$ git log file
diffs
$ git diff
$ git diff --staged
$ git diff HEAD
$ git diff 4fb063d..56ght76
$ git diff master new_feature
$ git diff --since=1.month.ago --until=2.weeks.ago
$ git blame index.html --date short
diff between range of SHA
diff between last commit & current state
diff between two branches
time based diff
diff between last commit & staged changes
diff between staged changes & working copy
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
Undoing Changes
$ git checkout <commit> <file>
$ git checkout <commit>
$ git revert <commit>
$ git reset <file>
$ git reset
$ git reset --hard
$ git clean -n
$ git clean -f
$ git clean -df
$ git clean -xf
The git revert command undoes a committed snapshot.
Permanent undo
removes untracked files from your working directory
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2832504/giphy.gif)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2832515/pasted-from-clipboard.png)
remove files from staging area
staging area & working directory
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2282505/pasted-from-clipboard.png)
Collaboration
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724181/pasted-from-clipboard.png)
Remote Repository Hosting
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724489/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724507/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724525/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724507/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830022/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830026/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724525/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830034/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830036/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2724489/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830049/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830051/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830054/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2282613/pasted-from-clipboard.png)
Exclude files
local .gitignore:
In your git repo, create .gitignore file https://github.com/github/gitignore
global .gitignore:
$ git config --global core.excludesfile ~/.gitignore_global
Explicit repository excludes:
In your git repo, open .git/info/exclude
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
Workflows
1. Centralised Workflow
2. Features Branch Workflow
3. Gitflow Workflow
4. Forking Workflow
Just guidelines, you can mix & match
1. Centralised Workflow
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824103/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824103/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824103/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824103/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2829984/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2829984/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2829987/pasted-from-clipboard.png)
Syncing
$ git fetch <remote>
$ git fetch <remote> <branch>
$ git pull <remote>
$ git push <remote> <branch>
$ git push <remote> --all
$ git push <remote> --tags
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830266/syncing.gif)
2. Feature Branch Workflow
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2826216/pasted-from-clipboard.png)
Pull / Merge Request
Branch
Develop a feature on a branch. Create a Pull-Request, to get reviewed
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830325/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830329/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2287048/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830342/pasted-from-clipboard.png)
Discuss
Discuss and approve code changes
Merge
Merge the branch & optionally delete the branch
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735466/pasted-from-clipboard.png)
3. Gitflow Workflow
Tagging
$ git tag
$ git tag -a v0.0.3 -m "release version 0.0.3"
$ git push --tags
$ git checkout v0.0.3
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830351/pasted-from-clipboard.png)
list all tags
add a new tag
push the tag
checkout code at commit
4. Forking Workflow
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2826246/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2303353/pasted-from-clipboard.png)
Branching
Need to work on a feature that will take some time?
Time to branch out.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830378/pasted-from-clipboard.png)
Branching
$ git checkout -b new_branch
$ git checkout master
$ git merge new_branch
$ git branch -d new_branch
Master
Pull-Request
1.
$ git branch meetup_attendance
$ git checkout meetup_attendance
$ git branch
$ git push origin meetup_attendance
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735224/pasted-from-clipboard.png)
OR $ git checkout -b meetup_attendance
2.
# Create a remote branch on Remote Server
$ git pull
$ git branch -r
$ git checkout online_attendance
$ git branch
$ git remote show origin
Branching
Removing Branch
$ git push origin :meetup_attendance
$ git branch -d meetup_attendance
$ git branch -D meetup_attendance
$ git remote prune origin
delete remote branch
delete local branch
clean up deleted remote branches
forcefully delete local branch
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2303353/pasted-from-clipboard.png)
Merge vs Rebase
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2848531/pasted-from-clipboard.png)
Local branches
Extra merge commits are bad
Merge vs Rebase
Extra merge commits are bad
$ git fetch
$ git rebase
- Move all changes to master which are not in origin/master to a temporary area.
- Run all origin/master commits.
- Run all commits in the temporary are, one at a time.
- (no merge commits)
The golden rule of git rebase is to never use it on public branches.
Stashing & Cleaning
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2830693/pasted-from-clipboard.png)
$ git stash $ git stash pop $ git stash list $ git stash drop
Temporary stores all modified tracked files
Restores the most recently stashed files
List all stashed changesets
Discard the most recently stashed changesets
Git for DevOps
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824086/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824103/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824120/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824129/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824136/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824157/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824159/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824181/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824192/pasted-from-clipboard.png)
Dev
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2824353/pasted-from-clipboard.png)
SCM
Build
Package Repo
Deploy
Test
Stage
Prod
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2586310/pasted-from-clipboard.png)
JIRA
Confluence
Slack
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2292099/pasted-from-clipboard.png)
Automated build & Continuous Integration
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2292104/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2848558/pasted-from-clipboard.png)
Waffle
CI Friendly Git Repo
-
Avoid tracking large files in your repo
-
Use shallow clones for CI
-
Choose your triggers wisely
-
Stop polling, start hooking
$ git clone --depth 1 <repo_url>
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2282505/pasted-from-clipboard.png)
Github for Project Management
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2826271/pasted-from-clipboard.png)
Add-on Feature on GitHub
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735205/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2304684/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/447123/images/2735989/pasted-from-clipboard.png)