Git
Parsa Hejabi


0x00 -Version Control?
Version Control Benefits
- Make backups
- Keep history
- View Changes
- Experiment
- Collaborate
Developers can find out:
- Which changes were made?
- Who made the changes?
- When were the changes made?
- Why were changes needed?
What’s a VCS?
- Version Control System
- Other names: Revision Control, Source Control
- Tracks the history of changes as people and teams collaborate on projects together.
- Functions:
- Allows developers to work simultaneously.
- Does not allow overwriting each other’s changes.
- Maintains a history of every version.
- 2 types: CVCS & DVCS.


Centralized VCS?
- A central server to store all files and enables collaboration.
- If the server goes down for an hour no one during that hour can collaborate.
- If the disk is gone and you don't have backups then...

Distributed VCS?
- Like: Git
- Allow access to every file, branch, iteration of a project
- Don't need a constant connection to a central repository
DVCS
- Speed
- Don't care about network performance
- Offline access
- Everyone has a mirror of project locally
- Open source projects use DVCS!
0x01 - Terms
-
Repository
-
Working Directory
-
Staging Area
- Where files are checked out
- Whenever you commit an operation, Git looks for the files present in the staging area.


-
Commits
-
Current state of the repository
-
consider a commit object as a node of the linked list
-
-
Branches
- By default, Git has a master branch
- Every branch is referenced by HEAD
-
Tags
- meaningful name with a specific version in the repository
- very similar to branches, but the difference is that tags are immutable
-
Clone
-
Pull
-
Push
0x02 -
Let's dive
in the code
[ubuntu ~]$ git --version
git version 1.8.1.2
#SET USERNAME
[jerry@CentOS project]$ git config --global user.name "Jerry Mouse"
#SET EMAILID
[jerry@CentOS project]$ git config --global user.email "jerry@tutorialspoint.com"
# --global
#LIST GIT SETTINGS
[jerry@CentOS ~]$ git config --list

# add new group
[root@CentOS ~]# groupadd dev
# add new user
[root@CentOS ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser
# change password
[root@CentOS ~]# passwd gituser
[gituser@CentOS ~]$ pwd
/home/gituser
[gituser@CentOS ~]$ mkdir project.git
[gituser@CentOS ~]$ cd project.git/
[gituser@CentOS project.git]$ ls
[gituser@CentOS project.git]$ git init
Initialized empty Git repository in /home/gituser-m/project.git/
[gituser@CentOS project.git]$ ls
branches config description HEAD hooks info objects refs
CLONE
[jerry@CentOS ~]$ mkdir jerry_repo
[jerry@CentOS ~]$ cd jerry_repo/
[jerry@CentOS jerry_repo]$ git clone gituser@git.server.com:project.git
#WRITE A NEW CODE IN YOUR REPO.
[jerry@CentOS project]$ git status -s
?? string
?? string.c
[jerry@CentOS project]$ git add string.c
#string.c IS ADDED TO THE STAGING AREA
[jerry@CentOS project]$ git status -s
A string.c
?? string
#IF WE OMMIT -m Git will open a text editor where we can write multiline commit message.
[jerry@CentOS project]$ git commit -m 'Implemented my_strlen function'
[master cbe1249] Implemented my_strlen function
1 files changed, 24 insertions(+), 0 deletions(-)
create mode 100644 string.c
[jerry@CentOS project]$ git log
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Implemented my_strlen function
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
[jerry@CentOS project]$ git log
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Implemented my_strlen function
[jerry@CentOS project]$ git show cbe1249b140dad24b2c35b15cc7e26a6f02d2277
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Implemented my_strlen function
diff --git a/string.c b/string.c
new file mode 100644
index 0000000..187afb9
--- /dev/null
+++ b/string.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+int my_strlen(char *s)
+{
+
char *p = s;
+
+
while (*p)
+ ++p;
+ return (p -s );
+
}
+
#Git diff shows '+' sign before lines, which are newly added and '−' for deleted lines.
[jerry@CentOS project]$ git diff
diff --git a/string.c b/string.c
index 187afb9..7da2992 100644
--- a/string.c
+++ b/string.c
@@ -1,6 +1,6 @@
#include <stdio.h>
-int my_strlen(char *s)
+size_t my_strlen(char *s)
{
char *p = s;
@@ -18,7 +18,7 @@ int main(void)
};
for (i = 0; i < 2; ++i)
{
- printf("string lenght of %s = %d\n", s[i], my_strlen(s[i]));
+ printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i]));
return 0;
}
Commit Changes
[jerry@CentOS project]$ git log
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Implemented my_strlen function
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
#The amend operation changes the last commit including your commit message.
#it creates a new commit ID.
[jerry@CentOS project]$ git status -s
M string.c
?? string
[jerry@CentOS project]$ git add string.c
[jerry@CentOS project]$ git status -s
M string.c
?? string
[jerry@CentOS project]$ git commit --amend -m 'Changed return type of my_strlen to size_t'
[master d1e19d3] Changed return type of my_strlen to size_t
1 files changed, 24 insertions(+), 0 deletions(-)
create mode 100644 string.c
Commit Changes
[jerry@CentOS project]$ git log
commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Changed return type of my_strlen to size_t
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
Push
#The Push operation stores data permanently to the Git repository.
#After push operation, other developers can see Jerry’s changes.
[jerry@CentOS project]$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 517 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To gituser@git.server.com:project.git
19ae206..d1e19d3 master −> master
Pull
[jerry@CentOS project]$ git push origin master
To gituser@git.server.com:project.git
! [rejected]
master −> master (non-fast-forward)
error: failed to push some refs to 'gituser@git.server.com:project.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
[jerry@CentOS project]$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From git.server.com:project
d1e19d3..cea2c00 master −> origin/master
First, rewinding head to replay your work on top of it...
Applying: Added my_strcpy function
#CANT PUSH BEFORE PULL
[jerry@CentOS project]$ git push origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 455 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To gituser@git.server.com:project.git
cea2c00..e86f062 master −> master
Stash
[jerry@CentOS project]$ git status -s
M string.c
?? string
[jerry@CentOS project]$ git stash
Saved working directory and index state WIP on master: e86f062 Added my_strcpy function
HEAD is now at e86f062 Added my_strcpy function
#your working directory is clean and all the changes are saved on a stash
[jerry@CentOS project]$ git status -s
?? string
[jerry@CentOS project]$ git stash list
stash@{0}: WIP on master: e86f062 Added my_strcpy function
[jerry@CentOS project]$ git status -s
?? string
Stash
[jerry@CentOS project]$ git stash pop
# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
#
modified: string.c
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#
string
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4)
[jerry@CentOS project]$ git status -s
M string.c
?? string
Move a file in repo
[tom@CentOS project]$ pwd
/home/tom/project
[tom@CentOS project]$ ls
README string string.c
[tom@CentOS project]$ mkdir src
[tom@CentOS project]$ git mv string.c src/
[tom@CentOS project]$ git status -s
R string.c −> src/string.c
?? string
[tom@CentOS project]$ git commit -m "Modified directory structure"
[master 7d9ea97] Modified directory structure
1 files changed, 0 insertions(+), 0 deletions(-)
rename string.c => src/string.c (100%)
[tom@CentOS project]$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To gituser@git.server.com:project.git
e86f062..7d9ea97 master −> master
Rename a file in repo
[jerry@CentOS project]$ pwd
/home/jerry/jerry_repo/project
[jerry@CentOS project]$ ls
README src
[jerry@CentOS project]$ cd src/
[jerry@CentOS src]$ git add Makefile
[jerry@CentOS src]$ git mv string.c string_operations.c
#Git is showing R before file name to indicate that the file has been renamed.
[jerry@CentOS src]$ git status -s
A Makefile
R string.c −> string_operations.c
Remove a file in repo
[tom@CentOS src]$ ls
Makefile string_operations string_operations.c
[tom@CentOS src]$ git rm string_operations
rm 'src/string_operations'
[tom@CentOS src]$ git commit -a -m "Removed executable binary"
[master 5776472] Removed executable binary
1 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100755 src/string_operations
0x03 -
MORE SERIOUS
FIXING Mistakes 😥
FIX: Uncommitted changes
[jerry@CentOS src]$ pwd
/home/jerry/jerry_repo/project/src
[jerry@CentOS src]$ git status -s
M string_operations.c
[jerry@CentOS src]$ git checkout string_operations.c
[jerry@CentOS src]$ git status –s
FIX: Uncommitted changes
[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src
[tom@CentOS src]$ ls -1
Makefile
string_operations.c
[tom@CentOS src]$ rm string_operations.c
[tom@CentOS src]$ ls -1
Makefile
#Git is showing the letter D before the filename.
[tom@CentOS src]$ git status -s
D string_operations.c
[tom@CentOS src]$ git checkout string_operations.c
[tom@CentOS src]$ ls -1
Makefile
string_operations.c
[tom@CentOS src]$ git status -s
FIX: Changes from Staging Area
tom@CentOS src]$ pwd
/home/tom/top_repo/project/src
# Unmodified file
[tom@CentOS src]$ git status -s
# Modify file and view it’s status.
[tom@CentOS src]$ git status -s
M string_operations.c
[tom@CentOS src]$ git add string_operations.c
[tom@CentOS src]$ git checkout HEAD -- string_operations.c
[tom@CentOS src]$ git status -s
0x04 -
branches
Create a branch
[jerry@CentOS src]$ git branch new_branch
[jerry@CentOS src]$ git branch
* master
new_branch
#SWITCH BETWEEN BRANCH
[jerry@CentOS src]$ git checkout new_branch
Switched to branch 'new_branch'
[jerry@CentOS src]$ git branch
master
* new_branch
#Create and Switch Branch
[jerry@CentOS src]$ git checkout -b test_branch
Switched to a new branch 'test_branch'
[jerry@CentOS src]$ git branch
master
new_branch
* test_branch
#delete branch
[jerry@CentOS src]$ git branch
master
new_branch
* test_branch
[jerry@CentOS src]$ git checkout master
Switched to branch 'master'
[jerry@CentOS src]$ git branch -D test_branch
Deleted branch test_branch (was 5776472).
[jerry@CentOS src]$ git branch
* master
new_branch
#rename branch
[jerry@CentOS src]$ git branch
* master
new_branch
[jerry@CentOS src]$ git branch -m new_branch wchar_support
[jerry@CentOS src]$ git branch
* master
wchar_support
#MERGE BRANCHES
[jerry@CentOS src]$ git push origin wchar_support <−−− Observer branch_name
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 507 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
To gituser@git.server.com:project.git
* [new branch]
wchar_support -> wchar_support
[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src
[tom@CentOS src]$ git log origin/wchar_support
commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 16:10:06 2013 +0530
Added w_strlen function to return string lenght of wchar_t string
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 10:21:20 2013 +0530
Removed executable binary
[tom@CentOS project]$ git branch
* master
[tom@CentOS project]$ pwd
/home/tom/top_repo/project
[tom@CentOS project]$ git merge origin/wchar_support
Updating 5776472..64192f9
Fast-forward
src/string_operations.c | 10 ++++++++++
1 files changed, 10 insertions(+), 0 deletions(-)
Git merge? 🤔


Before merge! 👿
- Confirm the receiving branch
- Fetch the latest remote commits
Conflicts!!!
$ mkdir git-merge-test
$ cd git-merge-test
$ git init .
$ echo "this is some content to mess with" > merge.txt
$ git add merge.txt
$ git commit -am"we are commiting the inital content"
[master (root-commit) d48e74c] we are commiting the inital content
1 file changed, 1 insertion(+)
create mode 100644 merge.txt
$ git checkout -b new_branch_to_merge_later
$ echo "totally different content to merge later" > merge.txt
$ git commit -am"edited the content of merge.txt to cause a conflict"
[new_branch_to_merge_later 6282319] edited the content of merge.txt to cause a conflict
1 file changed, 1 insertion(+), 1 deletion(-)
git checkout master
Switched to branch 'master'
echo "content to append" >> merge.txt
git commit -am"appended content to merge.txt"
[master 24fbe3c] appended content to merge.tx
1 file changed, 1 insertion(+)
$ git merge new_branch_to_merge_later
Auto-merging merge.txt
CONFLICT (content): Merge conflict in merge.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: merge.txt
$ cat merge.txt
<<<<<<< HEAD
this is some content to mess with
content to append
=======
totally different content to merge later
>>>>>>> new_branch_to_merge_later
# Change the file to:
#this is some content to mess with
#content to append
#totally different content to merge later
git commit -m "merged and resolved the conflict in merge.txt"
0x05 - Online Repos



0x06 -
Best
Practices
Use .gitignore!
Thank you! 👋
Feel free to contact me on my website!


Git
By Parsa Hejabi
Git
This slide was used to teach Git and CVS for AP students of SBU CE faculty in October 2018.
- 1,114