0% found this document useful (0 votes)
93 views51 pages

By: Feven Tadesse Title: Trainer at The Coding School: Orange Digital Center

The document provides an overview of a Git training session. It introduces version control systems and why they are useful for collaboration. It then covers the key concepts and commands for using Git, including initializing a repository, tracking and committing changes, viewing commit history, branching, and pushing changes to a remote repository. The training outlines the basics of Git configuration, adding and committing files, switching and creating branches, and resolving issues that may occur when pushing a new local branch to the remote origin.

Uploaded by

Ermias Mesfin
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)
93 views51 pages

By: Feven Tadesse Title: Trainer at The Coding School: Orange Digital Center

The document provides an overview of a Git training session. It introduces version control systems and why they are useful for collaboration. It then covers the key concepts and commands for using Git, including initializing a repository, tracking and committing changes, viewing commit history, branching, and pushing changes to a remote repository. The training outlines the basics of Git configuration, adding and committing files, switching and creating branches, and resolving issues that may occur when pushing a new local branch to the remote origin.

Uploaded by

Ermias Mesfin
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/ 51

2022

GIT

By: Feven Tadesse


Title: Trainer at the coding school

Orange
Digital
Center
About ODC

Orange Venture Africa


THREE DAYS FLOW

POSTMAN
GIT
ZEPLIN

OPEN SOURCE

3 AASTU Training
Objective Of These Session
✔ You will be able to understand the popular version control system, and how to use it

✔ You will be able to work with teams and collaborate on a common project seamlessly without

version issues on codebase

✔ You will be able to work on open source projects with flexibility and security you need

4 AASTU Training
Outline Of The Session
Intro To Version Control System
👉 What is VCS?
👉 Why VCS?
👉 Distributed VCS?
Intro To Git
👉Getting Started With Git
👉Git Hosting Services
Deeper Into Git
👉 Go Local
👉 Git configuration
👉 Git Basics
👉 Working With Branches
👉 Merging and Conflict Resolution
Mini
5 GITTraining
AASTU Project
Rules For Success!
Practice is the best way to learn. So do all the exercises along the way

Ask questions and explore more than what’s on the slide

Check out documentations for the technologies covered

Wear your masks at all times!

6 AASTU Training
Ice Breaker :)

Question ?

1. Have you ever worked on a project where there


were more than 2 people?

2. If so, How did you share codebase updates


with each other?

3. Have you ever heard or used git before?

7 AASTU Training
VERSION CONTROL SYSTEM

8 AASTU Training
What is VCS?
Version control is a process of tracking and managing changes to code

The tool we used is called version control system

Version control system keeps track of


every modification to the code in a special
kind of database

Terms is VC:
Repository - collection of files of various
versions of a project (Local and Remote)
Collaboration - developers working(contributing)
together project in a synchronized way
9 AASTU Training
Why VCS?
Who / Why / When a change is made (A complete long-term change history of every file)

Easy collaboration (Having team members work concurrently is a no-brainer)

Increase traceability (can connect to project management and other tools like trello, jira, slack)

We have two categories: Central and Distributed

10 AASTU Training
Distributed VCS
Each collaborator has a local copy of the repository

He/She can store their changes in local repository (offline)


and push their copies to remote repository

Good example : Git

11 AASTU Training
GIT INTRODUCTION

12 AASTU Training
What is Git?
The most popular distributed version control system

A staggering number of software projects rely on Git

Simple, flexible and secure

13 AASTU Training
Little History
Created by Linus Torvalds in 2005

Torvalds sarcastically quipped about the name git (which means

"unpleasant person" in British English slang):


"I'm an egotistical bastard, and I name all my projects
after myself. First 'Linux', now 'git'."

14 AASTU Training
Getting Started With Git
Git is already installed on Mac OS and Linux

Git installation on Windows:


Head to https://git-scm.com/downloads and download for
windows and install with the recommended settings

We can check the version of git installed:


git --version

15 AASTU Training
Git Hosting Service
What is the difference between git and gitHub or gitLab??

16 AASTU Training
Exploring Gitlab…

Let’s head to https://gitlab.com/ and create an account.

We can now create a blank project with a name ‘git-workshop’


And explore the dashboard of gitlab a bit more …

So now we have created our remote repository!

So shall we have a local copy (clone) of this repo?

Let’s create a new directory on our machine and open the terminal in that directory and type:
git clone https://gitlab.com/Fevtad/git-workshop.git

17 AASTU Training
Go Local
Now we have a local copy(local repo) of our remote repository

So we’ll be playing around with this abit

We will be looking at:

Adding changes to our local repo (git add)


Committing these changes (git commit)
Checking the status of our git (git status)
Different states of our changes (untracked, staged, modified)
Pushing changes to remote repo (git push)

18 AASTU Training
Git Basics
We can head to ‘git-workshop’ folder and create an html file ‘my_html’and type
<html>
<body>
<h1>This is the first clone</h1>
</body>
</html>
Question : If I add another line of code like : <h1>This is another line</h1>. Save and close the file how
can I go back to the previous version?

Can git actually help with tracking every version of all of my files in a folder??

19 AASTU Training
Cont …
Every single version of every file in a repository can be tracked using git.

Let’s see how!

Can we now try to check the status of our folder by typing:


git status
We now have one untracked file “my_html” in my local repo
So now how do I tell git to track this file?
git add my_html.html or
git add .

20 AASTU Training
Cont...

Now let’s check the status of our git again. How do we that?

You can now see my_html.html in green.


Our file is being tracked by git.

So now we shall tell our local repo about our change right?

git commit -m “created my_html file”

Did you get this message:


*** Please tell me who you are.

You should tell git who you are, meaning you should
configure your git
21 AASTU Training
GIT CONFIGURATION

22 AASTU Training
Git Configuration

We have local and global configuration

Local - Scoped to that repo

Global - Scoped to the user

This time let’s have our local configuration:


git config user.name "YOUR NAME"
git config user.email "YOUR EMAIL"
Let’s check if our configuration is saved:

git config --list

23 AASTU Training
Git Basics

Now we can go ahead and commit our change:

git commit -m “created my_html file”


You can see we have:
1 file changed, 5 insertions(+)
Shall we now see our commit history(all commits that are made)
git log
Let’s examine the results:

commit f5bc4064d926b14a8a0721bc5733790d4b69e329 (HEAD -> master)


Author: Feven <[email protected]>
Date: Fri Mar 5 10:18:58 2021 +0300
initial commit
24 AASTU Training
Cont…
What happens if we have same message for our commit message?
We have the hash value, author, timestamp and message

Let’s check our git status again. What do we see?

Your branch is ahead of 'origin/master' by 1 commit.


(use "git push" to publish your local commits)

nothing to commit, working tree clean

So now our local repo has all our changes. So we can ‘push’
our local changes to the remote repo

25 AASTU Training
What did we learn so far

Git add - start tracking untracked file


- Add any changes to the staging area
Git commit - Add this changes to the local repo

But we still didn’t ‘push’ our changes to the remote repo

Let’s do the following:


git push

Let’s again check our status:


Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean

Can we now reload our gitlab page and see if there are changes?
26 AASTU Training
GIT BRANCHING

27 AASTU Training
Branch It Out
A separate and independent line of development in a repository

All collaborators can contribute to their branch

Changing one branch doesn’t affect another branch

There is a default branch which is created when creating


a repo which is ‘main’ branch

So let’s see on which branch we’re working on:


git branch
* shows the current branch we’re on

We can also check our branches on gitlab.


28 AASTU Training
Cont...
Let’s go ahead and create our own branch
git branch NEW-BRANCH-NAME
This will copy everything from the current branch(master)

Now we should switch to our new branch to start


working on it
git checkout BRANCH-NAME

Let’s check our branches again:


git branch

This is how mine looks like:


* dev-fev
master

29 AASTU Training
Deeper Into Branches
Let’s make some changes to our branch :
<html>
<body>
<h1>This is the first clone</h1>
<h1>This is my dev-fev branch</h1>
</body>
</html>

Let’s save and check our status


(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: my-html.html

30 AASTU Training
Cont…
So we need to add this change and then commit:
git add .
git commit -m “created new branch”

So the next step will be pushing our changes to remote repo


git push

Do we have an issue??

fatal: The current branch dev-fev has no upstream branch.


To push the current branch and set the remote as upstream, use

git push --set-upstream origin dev-fev

31 AASTU Training
Cont...
Since we don’t have our ‘dev-fev’ branch remotely we need
to create the branch and push to that branch using:
git push --set-upstream origin dev-fev

This is the result:


Branch 'dev-fev' set up to track remote branch 'dev-fev' from 'origin'
What does that mean?
"origin" is a shorthand name for the remote repository that a project was originally cloned from

Shall we checkout to our master branch and see how our html file is affected?

We can rename and also delete our branch like this:


git branch -m CURRENT-NAME NEW-NAME // to update branch
git branch -d CURRENT-NAME // to delete branch
32 AASTU Training
GIT BRANCHING BEST PRACTICES

33 AASTU Training
Best Practices While Working
With Branches
Master/Main branch is the default branch created when you
first create your repo

You can of course rename or delete the master branch but


it is NOT recommended

When working on your repo don’t directly use the master branch.
Create another branch and push your changes there.
You can finally merge your changes with the master.

This will take us to our next topic MERGING

34 AASTU Training
Let’s Bring It Together
We have been working on our new branch for a while now

Now what if I want to incorporate changes in this branch


to master branch

Here is where merging comes in

Taking changes from one branch and applying it to another is called merging

So let’s merge our new branch (source) to our master branch(destination)

35 AASTU Training
Cont...
To merge I need to first be in my destination branch
For our case it is our master branch. So make sure you have checked out
to your master branch. Then:
git merge SOURCE-BRANCH-NAME

Let’s now see what we have in our master branch:

<html>
<body>
<h1>This is the first clone</h1>
<h1>This is my dev-fev branch</h1>
</body>
</html>
36 AASTU Training
Cont...
If we check our git status we can see that our local branch is ahead
of our remote by 1 commit. Why?

So when we merge this is what happens:


Common commit point will be searched in both branches
Then the destination branch is updated with the commits after that point
So the destination branch will have the latest commit of the source branch

This is called fast-forward merge.


So we can just go ahead and push these changes to our remote master branch.
Let’s git log and see our commit history

You can see that the last commit message to our master branch is same as our new branch
37 AASTU Training
GIT CONFLICTS

38 AASTU Training
Resolve Some Conflicts
What is conflict?
When Git doesn’t know how to merge two branches
Why conflict?
Different changes on same file and same line
Changing a file on one branch and that file is deleted in another
How to resolve?
1) I can abort the merge
git merge --abort
2) Or I can fix it manually. We’re about find that out

So ready to create conflict

39 AASTU Training
Cont...
Since our last activity was merging the two branches, their
my_html.html is the same

So let’s checkout to our new branch and make this change:


<html>
<body>
<h1>This is the first clone</h1>
<h1>This is dev-fev conflict</h1>
</body>
</html>
Let’s add and commit our change

40 AASTU Training
Cont...
And for our master branch:
<html>
<body>
<h1>This is the first clone</h1>
<h1>This is master conflict</h1>
</body>
</html>
Let’s also add and commit our change

Now let’s do what we did earlier. Let’s merge the new branch
to our master branch. How do we do that?

41 AASTU Training
Cont...
Did you see something like this:
<html>
<body>
<h1>This is the first clone</h1>
<<<<<<< HEAD(Current Change)
<h1>This is master conflict</h1>
=======
<h1>This is dev-fev conflict</h1>
>>>>>>> dev-fev(Incoming Change)
</body>
</html>
You can also see the different options just under the first <h1>
to accept current change, to accept incoming change,...
42 AASTU Training
Cont...
Yeah! We have successfully created a conflict!

So I can select from the options or I can edit my code manually

For me I have decided to keep my current changes. So my my_html looks


like this:
<html>
<body>
<h1>This is the first clone</h1>
<h1>This is master conflict</h1>
</body>
</html>
Finally I can add and commit my changes
43 AASTU Training
GIT PULL

44 AASTU Training
Wanna Guess What Pull Does
Obviously you are not the only one working on this project
So let’s say Feven pushed a change to the master branch. How would
you have that change locally?

You PULL!

And it is as simple as doing:


git pull
What About Fork?
If I am the owner of a repository do you think I will allow you
to push as you want? NO
But you can actually create your own copy of the repo on remote side
That way you can pull and push as if it’s your own
45 This is called Forking
AASTU Training
MINI PROJECT

46 AASTU Training
What We’re Doing
● Team of two
● Work on a project - Ecommerce site
● The site includes three pages
○ Landing page (both team members)
○ product list page (one team member)
○ customer list page (one team member)

● Here are the steps


○ Create a git repository per team
○ Clone to that repository
○ Create your own branches for development
○ Start building your website
○ Make sure to add, commit, push and merge with the master branch
○ Number of commits and commit messages count

47 AASTU Training
TAKEAWAYS??

48 AASTU Training
TakeAway
You can realize how powerful git is in resolving all your misery!

Many people can work simultaneously on one project without causing


chaos.

Here are some big advantages of Git:


Fast
Distributed
Gives lots of freedom
Easy collaboration
Doesn’t lose data (every history recorded)

Therefore git is a skill that every developer needs to have


under their belt
49 AASTU Training
Resources
✔ Git Official Site
https://git-scm.com/

50 AASTU Training
Orange Digital Center
Ethiopia
Orange
Digital
Center

You might also like