101

 Muhammad Shakhawat Hossain

Agenda

  1. What is Version/Source Control System
  2. Background
  3. Getting Started
  4. Configuration
  5. Creating a local repository
  6. Syncing with remote
  7. Collaboration
  8. Common use cases
  9. Self exploration

 

What is VCS/SCM

java-project

java-project-updated

java-project-latest

java-project-final

java-project-final-1

java-project-final-2

java-project-final-final.zip

Why should I learn git?

  1. An efficient way of managing and sharing source code
  2. Easy for team collaboration
  3. Industry-standard and very widely used
  4. A plus point / mandatory point in the resume

Background

April 2005

  • Take Concurrent Versions System (CVS) as an example of what not to do; if in doubt, make the exact opposite decision
  • Support a distributed, BitKeeper-like workflow
  • Include very strong safeguards against corruption, either accidental or malicious

3 core requirement by

Linus Torvalds

 

Getting started

-> git version
-> git version 2.26.2

Configuration

-> git config --global user.name "Safat Hossain"
-> git config --global user.email "safat@gmail.com"
-> cat ~/.gitconfig
[user]
     name = Safat Hossain
     email = safat@gmail.com
-> cat ~/.gitconfig
-> git config --list
-> cat ~/.gitconfig

Expectation from SCM

git-101 

- Logically keep log of the changes

- Versioning of the changes

- Undo changes

- Share changes with collaborators

- Get code review

- At any point of time, allow to see the change log of the project

- Secure place to host and share source code

Initialization

git-101 

-> git status
-> git init
Initialized empty Git repository in /Users/safat/git-101/.git/
fatal: not a git repository (or any of the parent directories): .git
total 0
drwxr-xr-x    7 safat  120070500   224 May  2 20:08 .
drwxr-xr-x+ 119 safat  120070500  3808 May  2 20:08 ..
drwxr-xr-x    9 safat  120070500   288 May  2 20:08 .git

-> ls -al

Tracking changes: 1

git-101 

-> git status
->
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
frontend.py
backend.py
nothing added to commit but untracked files present (use "git add" to track)
-> DO yourself: Create two files and write few lines of code/text
 
-> git status
-> git status

Tracking changes: 2

git-101 

-> git status
-> git status

On branch master

No commits yet

Changes to be committed:

  (use "git rm --cached <file>..." to unstage)

new file:   frontend.py

Untracked files:

  (use "git add <file>..." to include in what will be committed)

backend.py

-> git add frontend.py

Committing

git-101 

-> git status
-> git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)
backend.py
nothing added to commit but untracked files present (use "git add" to track)
-> git commit -m "Front-end work initial commit"
-> git log
Untracked files:
  (use "git add <file>..." to include in what will be committed)
backend.py
nothing added to commit but untracked files present (use "git add" to track)
Untracked files:
  (use "git add <file>..." to include in what will be committed)
backend.py
nothing added to commit but untracked files present (use "git add" to track)
commit 200b9d5766a36102b1fa51397d41e29d880eba44 (HEAD -> master)
Author: Safat Hossain <safat@gmail.com>
Date:   Sat May 2 20:51:59 2020 +0800 f
 Front-end work initial commitend-end work

Committing: 2 

git-101 

-> Commit backend.py 
-> git log
commit 200b9d5766a36102b1fa51397d41e29d880eba44 (HEAD -> master)
Author: Safat Hossain <safat@gmail.com>
Front-end work initial commit

commit752a7082490cec3b19790e861b5027239db9d6c4(HEAD -> master)
Author: Safat Hossain <safat@gmail.com>
Back-end work initial commit
ront-end work

Checking diff 

git-101 

-> Make some changes in front-end module
-> git diff
 diff --git a/frontend.py b/frontend.py
index e69de29..14b192e 100644
--- a/frontend.py
+++ b/frontend.py
@@ -0,0 +1 @@
+print("some enhancements in the front-end")
Add to staging area -> git add
Commit changes -> git commit -m "Front-end enhancements"
Check git log -> git log

Look back

-> git add
-> git status
-> git commit
-> git log
-> git diff
-> git add
-> git status
-> git commit
-> git log
-> git diff
tracking
staging area

untracked -> tracked -> staged -> committed

Remote repo: 1

Remote repo: 2

Remote repo: 3

Syncing with remote

git-101 

-> git remote -v
-> git remote add origin git@github.com:safat/git-101.git
-> git push origin HEAD

Collaboration: Access

Collaboration: Clone

Branching

-> git branch demo origin/master
-> git branch
master
demo
-> git checkout demo
1. make some changes
2. add to stage
3. commit changes
4. push to remote [git push origin head]
-> git clone git@github.com:safat/git-101.git

Branching

master
commit 1 -> commit 2 -> commit 3
demo                             \
---------------------------------- \ commit 4

Pull request

Review/Merge

Pull changes to local

-> git pull origin master
-> git log
master
commit 1 -> commit 2 -> commit 3 ----------- > merge commit 4 
demo                             \           /
---------------------------------- \ commit 4

Lookback: 2

-> git add
-> git status
-> git commit
-> git log
-> git diff
Create a remote repository
Push local repository to remote
Clone remote repository
Create branch, raise a Pull request
Merge a Pull request
Pull back changes to local repository

Common commands: amend

-> git commit --amend
1. make your changes
2. add to the staging area
3. use amend to amend the changes to the previous commit
4. push the changes to remote
5. if you have pushed the previous commit already to remote, you have to force push now
 
Correct your previous commit
Steps

CC: Drop commits

-> git reset --soft commit_hash
-> git reset --hard commit_hash
Remove a commit

CC: Rewrite history

-> git rebase -i HEAD~n
pick 5d4ac8b Back-end work initial commit
edit a2b4f3d Some front-end enhancements
pick b647a7b Config changes for Demo
1. issue the rebase command, now you will be in a temporary detached HEAD
2. make the changes and add to the stage, -> git add file_namne
3. amend the changes, -> git commit --amend
4. git rebase --continue
Steps

CC: Squash changes

-> git rebase -i HEAD~n
pick 5d4ac8b Back-end work initial commit
s a2b4f3d Minor refactorings in the Controllers
s b647a7b Fix error in HelloController
-> git log

CC: Cherry pick changes

-> git cherry-pick commit_hash
-> git log

CC: Cherry pick changes

-> git cherry-pick commit_hash
-> git log

Self exploration

1. What is inside the .git directory?
2. What is git object and packed-refs
3. What does the commit hash means, can there be hash conflicts?
4. If I create a branch, is it expensive? Does git copy over all the changes? Does it create diff?
5. What is git gc?
6. What is merge conflict, how to resolve it?
7. How to use reflog?
8. How to add more than one remote?


UI Tools

Git - 101

By MUHAMMAD SHAKHAWAT HOSSAIN SAFAT