EE485 VersionControl
EE485 VersionControl
Lecture 9:
Version Control System: Git
Writing a software
• Collaborate
• Student A and B starts to write a code together.
• A: write function A() in string.c.
• B: write function B() in string.c.
• A and B work independently for two weeks.
• Merge A’s code and B’s code.???!!!
• You can work even if you are offline or can’t connect to a VPN
3
Git installation & setting
• Ubuntu
• Window
• Download at https://help.github.com/articles/set-up-git
4
Git installation & setting
5
Repository
6
Repository
• git clone command copies all data from remote git repository to a local repository
Remote
Repository
Local Local
Repository git clone git clone
Repository
7
Create a Remote Repository
8
Remote Repository
9
Remote Repository
10
Remote Repository
Remote Repository
EE485
git clone
Local Repository
EE485
11
File management
12
git add vs git commit
• A staging area (index) exists between the working directory & local git
repository
• File changes are queued in the staging area before stored in local repository
• git commit: Save changes in the staging area to the local repository
git commit
13
git add
• “git add .”: only includes changes of the current directory in which git
add command is executed
• “git add –A”: save all changes of the working directory to the staging
area regardless where you are
Staging Changes
file git add
Commit Changes
git commit
14
git commit (1/2)
• Commit the file: save all changes added to staging area to the local git
repository
Staging Changes
file git add
Commit Changes
git commit
15
git commit (2/2)
• -m option
• -a option : Omit staging area. Save all changes to the local git repository
16
Lookup commit history (1/2)
Local Repository
commit commit commit
17
Lookup commit history (2/2)
• Prints commit hash value, author, commit message, commit data in chronological
order
• -p option: shows the diff result of each commit
• It is useful to quickly look up what your colleague has committed
• --stat option: shows which files have been modified, how many files have been
changed, and how many lines have been added or deleted
18
File status
• File status
• Untracked: Status not under version control
• Unmodified: Status that is under version control but has not changed since the
previous version
• Modified: Status that the contents of the file being versioned have changed
• Staged: Status that the file is saved to the index (staging area)
19
Practice: add, commit, and check the file status
1 #include <stdio.h>
2 int main()
3 {
4 printf(“Hello World\n”);
5 return 0;
6 }
20
Practice (Cont.)
$ git status
21
Practice (Cont.)
22
Practice (Cont.)
Not staged because git add was not executed after modification
23
Practice (Cont.)
24
Practice (Cont.)
• Modify test.c like below
1 #include <stdio.h> 1 #include <stdio.h>
2 int main() 2 int main()
3 { 3 {
4 printf(“Hi World\n”); 4 printf(“Good World\n”);
5 return 0; 5 printf(“Bye World\n”);
6 } 6 return 0;
7 }
• You can compare the committed file with the file which is not staged by
$ git diff
+ : added code
- : deleted code
25
Practice (Cont.)
26
Delete a file
• Delete test.c
27
Git Branch
28
Branch
Fix a bug
29
Git Branch
• Git branch is a file that stores a particular commit hash value (40 characters)
• Git branch does not need to copy the whole project like other version control systems
• For each commit, git stores a pointer pointing to the previous commit
new branch
f30ab...
61204... 34ac2...
...
pointer pointing
6344e...
commit
previous commit main branch
30
Git Branch
• Master branch
• created by default when git repository is created
• HEAD
• HEAD always points to a local branch that is currently being worked on
• Git figures out which branch is being worked on by HEAD
HEAD
new branch
Branch always points Branch that is now being working
to the lastest commit f30ab... on
Local Repository
61204... 34ac2...
Default branch
6344e... Automatically created when git
commit repository is intialized
master
31
Practice: check the branches
• Use the same local git repository (EE485) with the previous practice
• Check branch list of local repository
$ git branch
b02425... 587bea...
32
Practice: Create a branch
HEAD
master
b02425... 587bea...
testing
33
Practice: Move to other branch
• git branch only creates a new branch but does not move to the created
branch
• Move the HEAD to the other branch
b02425... 587bea...
testing
HEAD
34
Practice: Create the new branch
You can omit staging area (git add command) by using ‘-a’ option with ‘-m’ option
master testing
HEAD
master testing
36
Practice: Delete a local branch
HEAD
master
b02425... 587bea...
Note that when deleting a branch, HEAD must be
moved to another branch
“first “second commit”
commit”
37
Merge
38
Merge
• Merges the the multiple branches with different updates into a single branch
39
Fast-forward merge
40
Practice: Fast Forward Merge
HEAD
master testing
41
Practice: Fast Forward Merge (Cont.)
HEAD
master testing
42
Practice: Fast Forward Merge (Cont.)
HEAD
master
43
Merge: 3-way merge
• After performing 3-way merge, new commit which is called merge commit
is created
Merge
commit
Commit to 3-way merge
Merge In
testing testing
44
Practice: 3-way merge
... testing
“second commit” “modify test.c”
“hi world”
45
Practice: 3-way merge (Cont.)
• Do 3-way merge
... testing
“second commit” “modify test.c”
“hi world”
Conflict occurs in test.c
46
Handling Conflict
• Conflict: two branches modify the same part of the same file and attempt
to merge
‘test.c’ in master
1 #include <stdio.h>
2 int main()
3 {
printf(“bye
World\n”);
4
return 0;
5
}
6
47
Handling Conflict (Cont.)
$ git status
48
Handling Conflict (Cont.)
master branch
divider line
testing branch
• To resolve the conflict, choose from the top or the bottom contents, or create a
new content
49
Handling Conflict (Cont.)
• Modify test.c
• Open ‘test.c’ in master branch
‘test.c’ in master
1 #include <stdio.h>
$ git add .
2 int main()
$ git commit –m “merge testing branch”
3 {
printf(“bye
4 World\n”);
5 printf(“hi
World\n”);
6
return 0;
7
}
HEAD
master
587bea... fdbc227 “bye world”
... testing
“second commit” “modify test.c” “merge testing branch”
“hi world”
50
Push/pull
51
Remote Repository
• git push: sends all files in the local branch to the remote git
repository
• Data is sent only when the local branch is pushed explicitly
• git clone: copies all files from the remote git repository to a local
repository
• git fetch: git downloads the changes from the remote to the local
repository
• git pull: performs ‘git fetch’ and merges the changes with the
local master branch
52
Contribute to a project
Remote Repository
3. Send changes to
remote server
git push
git clone
1. Clone remote repository
to local computer
Local Repository
git add
git commit 2. Modify project
Staging area
53
Push
• When you clone a repository from the git remote server, git automatically names
the remote repository “origin” by default
54
Push (Cont.)
your password
55
Clone
• Copies all data from the remote git repository to a local repository
• When you clone the remote repository, git creates origin/master pointer in the
local repository. The origin/master pointer is a remote branch
git clone
56
Remote branch
• Check remote branch list
$ git branch -r
$ git branch -a
57
Synchronize with the Remote Server
• When someone pushes data to the remote server, the history between team
members is different
Someone updated
master
git server
0b743 a6b4c f4265 190a3
Different History
master
58
Synchronize with the Remote Server (Cont.)
master
59
Synchronize with the Remote Server (Cont.)
• Contents from the remote repository can be integrated into the local master
branch by performing merge
origin/master
Local Computer
master
$ git pull
60
Git workflow
• Git Data Transport Commands
commit -a
push
add commit
Local Remote
Workspace Index
Repository Repository
pull / rebase
checkout
diff HEAD
diff
61
Assignment
62
Assignment (due Next Friday, 10:25 AM)
• Create a git account at github.com if you do not already have one and create ‘EE485’
repository at github.com
1. Clone your remote EE485 repository to your local computer. Get into the EE485 directory in your local
repository. Create a file with your “studentID_name”.c
2. Commit the created file to your local repository using add & commit –m “first”
3. Create the “hello” branch, go to that branch, and write a file named “hello.c”
6. Merge the change in the “hello” branch to the “master” branch and delete the “hello” branch
• After each of the steps above, execute “git log --oneline --graph” and provide the result of
the execution. Submit your report (PDF) that includes the captured image of each step.
63
Reference
• Git guidance
• http://rogerdudler.github.io/git-guide/
• http://rogerdudler.github.io/git-guide/index.ko.html (Korean)
• https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud
• https://backlog.com/git-tutorial/
• https://backlog.com/git-tutorial/kr/ (Korean)
• Git branch
• http://learnbranch.urigit.com (need to disable ad blocker to load)
64