0% found this document useful (0 votes)
21 views64 pages

EE485 VersionControl

Uploaded by

johnkim7972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views64 pages

EE485 VersionControl

Uploaded by

johnkim7972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

EE485A: Software development environment and tools practice

Lecture 9:
Version Control System: Git
Writing a software

• Maintain a history of updates.


• Day 1: write string.c
• Day 2: modify string.c
• Day 3: add a function in string.c
• Day 4: Something went wrong. I need to get back Day2’s source code !!!

• 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.???!!!

Use git !!!


2
Git
• Distributed Version Control System

• Git is completely different from other


version control systems (rcs, cvs, SVN, Bazaar,...)
• Git stores snapshots, not differences for each version

• Every operation is (nearly) local

• You can work even if you are offline or can’t connect to a VPN

Computer A Server Computer Computer B


Local Repository Remote Repository Local Repository
Version 3 Version 3 Version 3
Version 2 Version 2 Version 2
Version 1 Version 1 Version 1

3
Git installation & setting

• Ubuntu

$ sudo apt-get install git

• Window
• Download at https://help.github.com/articles/set-up-git

4
Git installation & setting

• Configure user information


• Register user name

$ git config --global user.name “Your name”

• Register user email

$ git config --global user.email “Your email address”

5
Repository

6
Repository

• A central place in which an aggregation of data is kept

• Clone an existing repository


• Use “git clone” command to contribute to or join another project

• Same as “checkout” command in Subversion and CVS

• git clone command copies all data from remote git repository to a local repository

Remote
Repository
Local Local
Repository git clone git clone
Repository

your local pc your partner

7
Create a Remote Repository

• Create a repository at https://www.github.com

8
Remote Repository

• Create a repository at https://www.github.com

9
Remote Repository

• Check created remote repository

• Check your remote repository url

Click here to copy url

10
Remote Repository

• Clone remote repository to your local repository

$ git clone <remote repository url>

Remote Repository

EE485

git clone

Local Repository

EE485

local repository that cloned


git repository in remote repository
local repository Local PC

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 add: add changes to the staging area


• Collect changes in staging area before commit

• git add has no effect on git directory

• git commit: Save changes in the staging area to the local repository

Staging Area .git directory


Working Directory
(Index) (local repository)
Staging Changes
file git add Commit Changes

git commit

changes git commit -a

13
git add

$ git add [filename]

• Add changes of the working directory to the staging area (index)

• “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 Area .git directory


Working Directory
(Index) (local repository)

Staging Changes
file git add
Commit Changes

git commit

changes git commit –a

14
git commit (1/2)

$ git commit [option]

• Commit the file: save all changes added to staging area to the local git
repository

• Changes not added to the staging area are not committed

Staging Area .git directory


Working Directory
(Index) (local repository)

Staging Changes
file git add
Commit Changes

git commit

changes git commit –a

15
git commit (2/2)

• -m option

$ git commit –m “commit message”

• Commit message is a description of the corresponding commit

• Commit message usually contains descriptions of changes that occur at that


commit

• Commit message is included in commit history.

• How to Write a Git Commit Message : https://chris.beams.io/posts/git-


commit/

• -a option : Omit staging area. Save all changes to the local git repository

git commit –a -m “fixes the crashing bug with input X”

16
Lookup commit history (1/2)

• When you commit (git commit –m “commit message”), commit history


is saved in chronological order
• When commit, git snapshot is saved
• A snapshot is a photograph that stores all state of a particular point in time

• Each commit has its own hash value (sha-1)

Local Repository
commit commit commit

Date: 2021/05/23 02:12 Date: 2021/05/23 02:17


hash: ca8417da72... hash: 8da6895...
message: first commit message: second commit

17
Lookup commit history (2/2)

• git log: Check the commit history of the local repository

$ git log [option]

• 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

• Change current directory to your local repository.


(Cloned one from remote repository you created)

Cloned local repository

• Create a file “test.c” and write down the following code

1 #include <stdio.h>
2 int main()
3 {
4 printf(“Hello World\n”);
5 return 0;
6 }

20
Practice (Cont.)

• Check the file status of the local repository

$ git status

File test.c‘s status is untracked


Because test.c is not stored in the staging area by the git add command

21
Practice (Cont.)

• Add file ‘test.c’ to the stating area of the local repository

$ git add . Includes changes below


current directory

test.c is now added to the staging area

22
Practice (Cont.)

• Modify file ‘test.c’ like below

1 #include <stdio.h> 1 #include <stdio.h>


2 int main() 2 int main()
3 { 3 {
4 printf(“Hello World\n”); 4 printf(“Hi World\n”);
5 return 0; 5 return 0;
6 } 6 }

• Check file status of local repository (git status)

Not staged because git add was not executed after modification

23
Practice (Cont.)

• Add modified test.c to staging area and commit


• Set the appropriate commit message after the option –m

• Check commit history (git log)

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.)

• Again, add and commit test.c and check commit history

26
Delete a file

• Delete a file in local repository (committed file)

$ git rm [file name to delete]

• Delete test.c

• Check file status of local repository.

27
Git Branch

28
Branch

• Copy the entire code of a project and develop independently


regardless of the original code

• Each branch is not affected by the other


• Parallel development is possible

Your colleague’s work


Add a new function
Branches

Fix a bug

Merge Merge Merge

29
Git Branch

• Git branch is a file that stores a particular commit hash value (40 characters)

• Git branch points to a particular commit. Easy to make and erase

• 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

• Easy to merge branches

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

‘*’ means the branch pointed by HEAD

$ git log --decorate


HEAD
master

b02425... 587bea...

“first “second commit”


commit”

32
Practice: Create a branch

• You can create a branch by

$ git branch [branch_name]

• Create a local branch of name “testing”


• Master branch and testing branch point to the second commit (587bea…)

HEAD
master

b02425... 587bea...

“first “second commit”


commit”

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

$ git checkout [branch name]

• Move to “testing” branch


master

b02425... 587bea...

“first “second commit”


commit”

testing

HEAD

34
Practice: Create the new branch

• Create the real branch: “commit” in testing branch

You can omit staging area (git add command) by using ‘-a’ option with ‘-m’ option

• Check commit history option ‘oneline’ shows each


commit in a single line

master testing

b02425... 587bea... 09bbb7f

“first “second commit” “made a change”


commit”
HEAD
35
Practice: Move to the other branch

• Move to master branch

HEAD

master testing

b02425... 587bea... 09bbb7f

“first “second commit” “made a change”


commit”

36
Practice: Delete a local branch

• Delete a local branch

$ git branch –d [branch_name]

$ git branch –D [branch_name]

Use –D option when the branch is not


• Delete “testing” branch merged with master

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

$ git checkout [main branch] // move to main branch


$ git merge [branch to merge with main branch]

• Fast forward merge: merge the branch with its parent


• 3-way merge: merge the two siblings

39
Fast-forward merge

• Merge by moving main branch to point to the latest commit

master Fast forward merge master master

Commit to testing testing


Merge Into
Commit to
Merge In

40
Practice: Fast Forward Merge

$ git checkout master


$ git branch testing
$ git checkout testing
$ //modify test.c
$ git add .
$ git commit –m “modify test.c”

HEAD

master testing

b02425... 587bea... fdbc227

“first commit” “second commit” “modify test.c”

41
Practice: Fast Forward Merge (Cont.)

1. Move HEAD to master branch


HEAD 2. Merge testing to master by using “git merge”
master testing
$ git checkout master
b02425... 587bea... 7acaa62 $ git merge testing

“first commit” “second commit” “modify test.c”

HEAD

master testing

b02425... 587bea... fdbc227

“first commit” “second commit” “modify test.c”

42
Practice: Fast Forward Merge (Cont.)

• Delete “testing” branch

$ git branch –d testing

HEAD

master

b02425... 587bea... fdbc227

“first commit” “second commit” “modify test.c”

43
Merge: 3-way merge

• Merge the siblings

• After performing 3-way merge, new commit which is called merge commit
is created

common master master


ancestor Commit to
Merge Into

Merge
commit
Commit to 3-way merge
Merge In
testing testing

44
Practice: 3-way merge

git branch testing //make a branch “testing” ‘test.c’ in master


//modify test.c in the master branch 1 #include <stdio.h>
2 int main()
git add .
3 {
git commit –m “bye world” printf(“bye
World\n”);
git checkout testing //move HEAD to testing branch 4
return 0;
5
//modify test.c }
6
git add .
git commit –m “hi world” //commit
‘test.c’ in testing
1 #include <stdio.h>
int main()
master 2
3 {
printf(“hi
World\n”);
4
return 0;
5
}
587bea... fdbc227 “bye world” 6

... testing
“second commit” “modify test.c”

“hi world”

45
Practice: 3-way merge (Cont.)

• Do 3-way merge

• Move to master branch

• Merge testing branch into master branch


3-way merge
master

$ git checkout master


$ git merge testing
587bea... fdbc227 “bye world”

... 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

‘test.c’ in testing Conflict


1 #include <stdio.h>
2 int main()
3 {
printf(“hi
World\n”);
4
return 0;
5
}
6

47
Handling Conflict (Cont.)

$ git status

• Mark conflicted files as 'unmerged’

48
Handling Conflict (Cont.)

• How to resolve the conflict


• Modify source code directly
• Open ‘test.c’ in master branch

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

• Push your local branch to your remote repository


• Assume that the local branch has been closed from the remote repository

$ git push [remote repository name] [branch name]

• Check remote repository name

• When you clone a repository from the git remote server, git automatically names
the remote repository “origin” by default

54
Push (Cont.)

• Push local master branch to remote repository

$ git push origin master

your user name (id)

your password

55
Clone
• Copies all data from the remote git repository to a local repository

$ git clone <remote repository url>

• When you clone the remote repository, git creates origin/master pointer in the
local repository. The origin/master pointer is a remote branch

• Master branch in local points to origin/master

git server master

0b743 a6b4c f4265

git clone

Local Computer origin/master Remote branch


0b743 a6b4c f4265

master Local branch

56
Remote branch
• Check remote branch list

$ git branch -r

• Lookup local and remote branches

$ 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

Local Computer origin/master


0b743 a6b4c f4265

master

58
Synchronize with the Remote Server (Cont.)

• Synchronize the local repository with the remote repository

$ git fetch origin


Someone updated

git server master

0b743 a6b4c f4265 190a3

git fetch origin

Local Computer origin/master

0b743 a6b4c f4265 190a3

master

59
Synchronize with the Remote Server (Cont.)

• Contents from the remote repository can be integrated into the local master
branch by performing merge

$ git checkout master


$ git merge origin/master

origin/master
Local Computer

0b743 a6b4c f4265 190a3

master

• git pull performs fetch and merge automatically

$ git pull

60
Git workflow
• Git Data Transport Commands

commit -a
push
add commit

Local Remote
Workspace Index
Repository Repository

pull / rebase

checkout HEAD fetch

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”

• Create two branches with “name” and “hello”, respectively


1. Create the “name” branch, go to that branch, and write a file named “name.c”

2. Commit the change with commit –m “name”

3. Create the “hello” branch, go to that branch, and write a file named “hello.c”

4. Commit the change with commit –m “hello”


5. Merge the change in the “name” branch to the “master” branch and delete the “name” branch

6. Merge the change in the “hello” branch to the “master” branch and delete the “hello” branch

7. Push the result to your remote repository (push)

• 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

• Pro Git (Scott Chacon, 2012-04-09)


• https://git-scm.com/book/en/v2

• 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

You might also like