This slide discusses the most popular distributed version control system that is GIT and it's different commands that we need in our daily operations for project collaboration.
1. GIT >> Version Control System
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
2. Version Control System - VCS
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, see 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.
2
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
3. Local VCS - LVCS
3
Local VCS maintains a simple database that
records all the changes to files under revision
control.
It keeps patch sets (i.e. the difference
between files) in a special format on disk; it
can then re-create what any file looked like at
any point in time by adding up all the patches.
Problem >>
How to collaborate with other developers?
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
4. Centralized VCS - CVCS
Centralized Version Control Systems
(CVCSs) have a single server that
contains all the versioned files, and a
number of clients that check out files
from that central place.
Problem >>
Centralized server is a single point of
failure.
4
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
5. Distributed VCS - DVCS
In a Distributed Version Control Systems (DVCSs)
clients donât just check out the latest snapshot of the
files; rather, they fully mirror the repository, including
its full history.
if any server dies, and these systems were
collaborating via that server, any of the client
repositories can be copied back up to the server to
restore it. Every clone is really a full backup of all the
data.
5
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
6. DVCS >> GIT
⸠Git is a distributed version-control system for tracking changes in source code during software
development.
⸠It is designed for coordinating work among programmers, but it can be used to track changes in any
set of files.
⸠It was created by Linux Development Community (in particular, Linus Torvalds) in 2005 for
development of the Linux kernel when the relationship between the community and the BitKeeper
company broke down.
⸠Git naming:
âš In British slang, unpleasant person
âš Global information tracker
âš Goddamn idiotic truckload of sh*t
6
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
7. GIT
⸠GIT thinks of its data more like a series of snapshots of a miniature filesystem.
⸠With Git, every time you commit, or save the state of your project, Git basically takes a picture of what
all your files look like at that moment and stores a reference to that snapshot.
⸠To be efficient, if files have not changed, Git doesnât store the file again, just a link to the previous
identical file it has already stored.
7
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
8. GIT >> File States
⸠Each file in the working directory can be in 2 states:
âš Tracked â files that were in the last snapshot.
âš Untracked â everything else, not in the last snapshot and not in the staging area.
⸠Tracked file states:
âš Unmodified/Committed â data is safely stored in the local database.
âš Modified â you have changed the file but have not committed it to your database yet.
âš Staged â you have marked a modified file in its current version to go into your next commit snapshot.
8
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
9. GIT >> Stage and Commit Process
⸠Staging the files computes the checksum for each one, stores that version of
the file in the Git repository and adds that checksum to the staging area.
⸠During git commit, git checksums each subdirectory and stores them as a tree
object in the Git repository.
⸠Git then creates a commit object containing metadata and pointer to the
project tree.
9
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
10. GIT >> Multiple Commits Process
⸠The scenario of multiple commits.
⸠Each new commit stores a pointer to the commit that came immediately before it.
10
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
11. 11
GIT Command Line
- only place you can run all Git commands
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
12. GIT CLI >> First-time Setup
12
⸠Version check: $ git --version
⸠Configuration scope:
âš --system : values applied to every user on the system and all their repositories.
âš --global : values specific personally to you and affects all the repositories you work with.
âš --local : values specific to that single repository you are currently using.
⸠Get/Set Configuration variables :
âš To view all the settings: $ git config --list
âš To view all the settings with file path: $ git config --list --show-origin
âš To check a specific variable value: $ git config <key>
âš To set a specific variable value: $ git config --global <key> <value>
⸠First time setup:
$ git config --global user.name âusernameâ
$ git config -- global user.email âuseremail@gmail.comâ
cause every git commit uses your name and email
⸠Getting help: $ git <verb> --help
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
13. GIT CLI >> Getting a GIT Repository
13
⸠Initialize an existing local directory:
Go within that directory and run - $ git init
⸠Clone an existing remote repository:
To clone the remote repository run - $ git clone <url>
To clone the remote repository into your directory run - $ git clone <url> <new-directory-name>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
14. GIT CLI >> Creating a GIT Repository
14
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
15. GIT CLI >> Check/Add/Remove Remote Repository
15
⸠To show all the remote repositories you have configured
$ git remote âv
⸠To add new remote repositories
$ git remote add <shortname> <url>
git clone command implicitly adds the remote for you
⸠To remove remote repository
$ git remote remove <shortname>
⸠To rename remote repository
$ git remote rename <old-shortname> <new-shortname>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
16. GIT CLI >> Fetch/Pull/Push Information
16
⸠To fetch
$ git fetch <remote>
This command only downloads the data to your local repository - it doesnât automatically merge it.
⸠To pull
$ git pull <remote> <branch>
If your current branch(master) is set up to track a remote branch(remote master), then this command
automatically fetch and then merge that remote branch into your current branch.
⸠To push (pull/fetch before push)
$ git push <remote> <branch>
⸠Inspecting a remote,
$ git remote show <remote>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
17. GIT CLI >> How git fetch Works
17
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
18. GIT CLI >> How git fetch Works
18
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
19. GIT CLI >> Recording Repository Changes
19
⸠To get the status of the files
$ git status
⸠To track new file/stage a file
$ git add <file-path>
To add changes from all the tracked and untracked files
$ git add -A
⸠To commit your staged changes (commit records the snapshot you set up in your staging area)
$ git commit âm <commit-message>
To skip the staging area and commit every tracked files(auto staged)
$ git commit âa âm <commit-message>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
20. GIT CLI >> Ignoring Specific Files
20
⸠Create a file named .gitignore and
list the file names/patterns that you donât want GIT to automatically add/even show you as being
untracked.
For example,
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
*.[oa] to ignore any files ending with .o or, .a
!lib.a to track lib.a file even though you are ignoring .a files
/TODO to ignore TODO file in the current directory, not subdirectory
build/ to ignore all files in any directory named build
doc/*.txt to ignore doc/notes.txt file, bit not doc/server/arch.txt, i.e. not within
subdirectory
doc/**/*.pdf To ignore all .pdf files in the doc/ directory and its subdirectory
21. GIT CLI >> Viewing the Changes
21
⸠To see what youâve changed but not yet staged (diff. between staged and modified files)
$ git diff
⸠To see what youâve staged that will go into the next commit (diff. between staged and committed files)
$ git diff --cached
⸠To rename a file in GIT
$ git mv <file_from> <file_to>
⸠To make a tracked file untracked
$ git rm --cached <file-name>
To remove a file from your working directory
$ git rm <file-name>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
22. GIT CLI >> Commit History
22
⸠To list all the commits made in the repository in reverse chronological order,
$ git log
⸠To see some abbreviated stats for each commit
$ git log --stat
⸠To see the last 3 commit history
$ git log -3
⸠To see the difference introduced in each commit,
$ git log -p -2
⸠To show each commit in one line
$ git log --oneline
⸠To see the branch and graph history in the form of a graph
$ git log --pretty=format:â%h %sâ --graph
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
23. GIT CLI >> git log Options
23
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
-<n> Show only the last n commits
--since, --after Limit commits to those made after the specified date
--until, --before Limit the commits to those made before the specified date
--author Only show commits in which the author entry matches the specified string
--committer Only show commits in which the committer entry matches the specified string
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string
24. GIT CLI >> git log Options
24
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
-p Show the patch introduced in each commit
--stat Show statistics for files modified in each commit
--pretty Show commits in an alternate format. Oneline/short/full/fuller/format
--oneline Shorthand for --pretty=oneline --abbrev-commit
--graph Display an ASCII graph of the branch and merge history beside the log output
%H Commit hash %an, %ae, %ad Author name, email and date
%h Abbreviated commit hash %cn, %ce, %cd Committer name, email and date
%P Parent hashes %s Subject
%p Abbreviated parent hashes
25. GIT CLI >> Undoing Things
25
⸠To redo the last commit
$ git commit --amend -m ânew commit messageâ
or
$ git commit âamend
⸠To unstage a staged file
$ git reset HEAD <file-name>
⸠To unmodify a modified file
$ git checkout -- <file-name>
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
26. GIT CLI >> Branching
26
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
⸠To list all the current branches
$ git branch
The * pointed branch indicates the branch currently HEAD points to
⸠To get the list of merged branches
$ git branch âmerged
⸠To get the list of unmerged branches
$ git branch âno-merged
⸠To create a new branch
$ git branch <branch-name>
⸠To switch to new branch
$ git checkout <branch-name>
⸠To remove an already merged branch
$ git branch âd <branch-name>
⸠To remove an unmerged branch
$ git branch âD <branch-name>
27. GIT CLI >> Branching >> initial local repository
27
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
28. GIT CLI >> Branching >> creating a new branch
28
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
$ git branch <branch-name>
29. GIT CLI >> Branching >> switching to a new branch
29
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
$ git checkout <branch-name>
30. GIT CLI >> Branching >> staging & committing in the new branch
30
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
$ git commit âa âm âcommit messageâ
31. GIT CLI >> Branching >> switching back to master branch
31
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
$ git checkout master
32. GIT CLI >> Branching >> staging and committing in master branch
32
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
$ git commit âa âm âcommit messageâ
33. GIT CLI >> Branching >> example-step 1
33
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
34. GIT CLI >> Branching >> example-step 2
34
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Creating a new branch and switching to that branch
$ git checkout -b iss53
or,
$ git branch iss53
$ git checkout iss53
35. GIT CLI >> Branching >> example-step 3
35
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Committing in that new branch
$ git commit -a âm âcommit messageâ
36. GIT CLI >> Branching >> example-step 4
36
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Switching back to master, creating new branch hotfix,
switching to that branch and performing a new commit
there
$ git checkout master
$ git checkout hotfix
$ git branch hotfix
$ git commit -a âm âcommit messageâ
37. GIT CLI >> Branching >> example-step 5
37
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Switching back to branch master and merging with the hotfix branch
$ git checkout master
$ git merge hotfix
38. GIT CLI >> Branching >> example-step 6
38
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Deleting the hotfix branch, switching to iss53 branch and performing new commit there
$ git branch -d hotfix
$ git checkout iss53
$ git commit âa âm âcommit messageâ
39. GIT CLI >> Branching >> example-step 7
39
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Going back to master branch
$ git checkout master
40. GIT CLI >> Branching >> example-step 8
40
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
Merging the master branch with iss53 branch
$ git merge iss53
If conflict occurs, then open and edit the conflicted file
remove the <<<<<<<, =======, >>>>>>> symbols, perform necessary
changes, finally stage and commit the changes
41. GIT CLI >> Revision Selection
41
⸠To show the details of a specific commit
$ git show <SHA-1 hash value of that commit>
⸠To see the log of where your HEAD and branch references have been for the last few months
$ git reflog
⸠To refer to the older commits from the current HEAD position
$ git show HEAD@{5}
⸠To show the first parent of a specific commit
$ git show HEAD^
To show the second parent(meaning other branch parent) of a specific commit
$ git show HEAD^2
⸠To show the first parent of a first parent
$ git show HEAD~2
⸠Difference between the second branch commits with first branch commits
$ git log br1..br2
⸠Difference between both of the branch commits
$ git log br1âŚbr2
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
42. GIT CLI >>Temporarily saving changes at a Branch
42
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. The answer to this issue is the git stash
command.
Stashing takes the dirty state of your working directoryâââthat is, your modified tracked files and staged
changes ââand saves it on a stack of unfinished changes that you can reapply at any time (even on a
different branch).
ď To save the current work
$ git stash push
ď To check all the stashes
$git stash list
ď To apply any certain stash changes to the current branch file
$ git stash apply
or, $ git stash apply stash@{2}
ď To delete a saved stash from the stack
$ git stash drop stash@{1}
ď To apply the stash changes and also delete from the stack at the same time
$ git stash pop stash@{1}
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
43. GIT CLI >>The Role of Reset
43
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
44. GIT CLI >>The Role of Reset
44
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
45. GIT CLI >>The Role of Reset
45
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
46. GIT CLI >>The Role of Reset
46
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
47. GIT CLI >>The Role of Reset
47
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
48. GIT CLI >>The Role of Reset
48
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
49. GIT CLI >>The Role of Reset
49
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
50. GIT CLI >>The Role of Reset
50
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
51. GIT CLI >>The Role of Reset
51
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU
52. GIT CLI >>The Role of Reset
52
Mohammad Imam Hossain | Lecturer, dept. of CSE | UIU