
Source Code Management
Ashish Pandey
@ashishapy
blog.ashishapy.com

Meet-up on 17th July 2016
@ Thoughtworks Pune
Agenda

-
Git Introduction & Basics
-
Remote Repository (Cloud & Self) Hosting
-
Workflows
-
Branching
-
Git for DevOps
-
Github Features

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/


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

$ git config --global alias.<alias-name> <git-command>

Starting a remote repo


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

~/.gitconfig
./.git/


Git Flow

$ git add -A
$ git commit -m "commit message"
$ git push
$ git pull
$ git checkout
$ git status

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

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

After you Push
$ git rebase <base>
Move a branch to a new base commit


Looking at History

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

$ 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

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


remove files from staging area
staging area & working directory

Collaboration

Remote Repository Hosting

















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

Workflows
1. Centralised Workflow
2. Features Branch Workflow
3. Gitflow Workflow
4. Forking Workflow
Just guidelines, you can mix & match
1. Centralised Workflow







Syncing
$ git fetch <remote>
$ git fetch <remote> <branch>
$ git pull <remote>
$ git push <remote> <branch>
$ git push <remote> --all
$ git push <remote> --tags

2. Feature Branch Workflow

Pull / Merge Request
Branch
Develop a feature on a branch. Create a Pull-Request, to get reviewed




Discuss
Discuss and approve code changes
Merge
Merge the branch & optionally delete the branch

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

list all tags
add a new tag
push the tag
checkout code at commit
4. Forking Workflow


Branching
Need to work on a feature that will take some time?
Time to branch out.

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

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

Merge vs Rebase

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

$ 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









Dev

SCM
Build
Package Repo
Deploy
Test
Stage
Prod

JIRA
Confluence
Slack

Automated build & Continuous Integration


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>

Github for Project Management

Add-on Feature on GitHub


