Source Code Management
Ashish Pandey
@ashishapy
blog.ashishapy.com
Meet-up on 17th July 2016
@ Thoughtworks Pune
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
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/
$ 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>
$ 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
$ git add -A
$ git commit -m "commit message"
$ git push
$ git pull
$ git checkout
$ git status
$ 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
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
$ 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
$ git rebase <base>
Move a branch to a new base commit
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
$ 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
$ 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
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
1. Centralised Workflow
2. Features Branch Workflow
3. Gitflow Workflow
4. Forking Workflow
Just guidelines, you can mix & match
$ git fetch <remote>
$ git fetch <remote> <branch>
$ git pull <remote>
$ git push <remote> <branch>
$ git push <remote> --all
$ git push <remote> --tags
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
$ 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
Need to work on a feature that will take some time?
Time to branch out.
$ 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
$ 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
Local branches
Extra merge commits are bad
Extra merge commits are bad
$ git fetch
$ git rebase
The golden rule of git rebase is to never use it on public branches.
$ 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
Dev
SCM
Build
Package Repo
Deploy
Test
Stage
Prod
JIRA
Confluence
Slack
Automated build & Continuous Integration
Waffle
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>