Chapter 1 - Version Control with Git
Chapter 1 - Version Control with Git
with Git
Basics of git,
integrating git with
GitHub and equivalent
platforms
Introduction
In this course, will cover the basic functions of Git and how it can be integrated
with GitHub (or equivalent platforms).
You’ll practice using Git commands in the terminal to add version control to your
project, allowing you to switch between different versions of your progress and
branch off to create new features.
Introduction
By the end of this course, you’ll be able to:
• Create branches in your Git project so you can collaborate with others.
• Use Git commands to make sure your local copy of code matches the remote
copy on GitHub.
• Be familiar with Git rebase, GitHub repository settings, and how to keep a
remote repository organized.
• A version control (or revision or source control) system is any system that
manages multiple versions of files(e.g. source code, reports, videos, etc)
• Version control is essential for team projects and also very useful for individual
projects.
• Some other version control systems are CVS, Subversion and Mercurial. CVS and
Subversion are centralized while Mercurial and Git are distributed.
Why version control?
• For working by yourself:
• Gives you a “time machine” for going back to earlier versions
• Gives you great support for different versions (standalone, web app, etc.)
of the same basic project
• Enter these lines (with appropriate changes), should be done just once:
git config --global user.name "your intended name"
git config --global user.email youremail@provider.com
• If you want to use a different name/email address for a particular project, you
can change it for just that project
• cd to the project directory
• Use the above commands, but leave out the --global
• ls -a
• git status
• git status
• git add .
• git status
Delete Files
• touch newfile.txt
• ls -a
• git status
• rm newfile.txt
• git status
• ls -a
• git status
• git add .
• Create a .gitignore file (config/ - specify files and folders you want to ignore)
• git status
• ls -a
• git add .
• git status
02
Git
branching
What are branches?
• In Git, a branch is a new/separate version of the main repository
• Take an example of Thierry Henri, Nadia and Zida working on a group project
together. Their repo will have one main version (main branch) but the different
team members need to work on different parts at the same time.
• Instead of modifying the same files and messing up the report, each person
takes a copy (creates a branch) and works on their section separately:
• Thierry works on the introduction (intro-branch)
• Nadia works on the presentation (presentation-branch)
• Zida worjs on the conclusion (conclusion-branch)
• Once everything is done, they bring back their changes, merge them into the
main project and nothing gets lost or overwritten.
Working with branches – creating and
•
switching
Show the branches on your repo: git branch (it will highlight the branch you’re
currently on.)
• Switch back to the main branch: git checkout main (or master, depending)
• Can you see the index.html file there?
• List the content of the repository. Have you seen the html file now?
• Switch back to the feature/web-interface branch, is it different from the main?
• Once you’ve merged a branch into main (and are no longer in need of it), you
can delete it using the git branch –d branch_name command.
• Delete the feature/web-interface branch.
• NB: there are 2 ways to delete a branch git branch –d branch_name or git
branch –D branch_name. How are they different?
Working with branches – rebasing
• Both git merge and git rebase are used to integrate changes from one branch
into another.
• While on the main branch, create a new branch called feature, and switch to it
(git checkout –b feature)
• Create a file, file.txt and put the text “Feature work” in it. Make a commit
• Create another file, file2.txt and put the text “Another file in my path”. Make a
commit
• Create one more file (I promise this is the last one) called file3.txt put the text
“April Fools Day?”. Make a commit
• Create one more file called file4.txt, put the text “Fooled you didn’t I 😏”. Make a
commit
Working with branches – rebasing
• At this point in time, the feature branch should have 4 commits.
NB: HEAD~1 simply refers to the commit hash of the previous commit.
These commands can work using the hash of any commit.
Undoing mistakes – git revert and commit
--amend
Git revert Git commit --amend
• Git revert is used to undo a • A convenient way to modify the
commit without changing most recent commit: git commit
history: git revert --amend
<commit_hash>
• Lets you combine staged
• It creates a new commit that changes with the previous
undoes the changes from the commit instead of creating an
specified commit. entirely new commit.
• This is the safest option to use in • Can also be used to simply edit
the case of a collaborative the previous commit message
project with multiple
collaborators.
Working with
remote
repositories
Git vs Gitlab. Pushing, pulling,
handling conflicts
Git vs (GitHub & Gitlab)
● Git vs (GitHub and Gitlab)
○ Git is a version control system that helps you track changes to your files
locally (on your computer).
● GitHub and GitLab are remote hosting platforms for git repositories. They add
collaboration tools on top of git, such as:
○ A central place to store and share code (remote repositories).
○ At the gathering, everyone can taste each other’s food, or even mix
dishes together (collaborate on projects, submit pull/merge requests).
GitHub vs Gitlab
● GitHub and Gitlab are both git repository hosting platforms.
● They both provide a web application (hosted by them) that users can use to
interact with the repositories hosted there.
● Gitlab is the only one of the two that can be self-hosted. Gitlab is an
application that you can download and run on your machine whereas GitHub
is only available online.
○ The --bare flag tells Git that this repository will not contain working files
(since a remote repo is usually just for storing changes).
● Our remote repo is currently empty, let’s push some code to it: git push
origin main
● Now we want to add some files, it is best practice to create a new branch
whenever you’re working on a feature. Create a new branch called feature-
branch
● Ok, now we want to simulate pulling from the remote. Switch to the main
branch (git checkout main)
● Pull changes from the remote’s feature-branch: git pull origin feature-branch
(this is not the ideal way but for explanation purposes...)
○ This command merges the remote’s feature-branch into the local repo’s
main branch
Working with remote repos
● We will create a merge conflict and then resolve it. We will do this by
modifying a file in both the main and feature-branch and trying to merge the
two.
● Switch to feature-branch
○ Modify file.txt different and commit
echo "This line is from feature-branch." >> file.txt
git add file.txt
git commit -m "Update file from feature-branch"
Working with remote repos
● Merge branches
○ Switch back to main (git checkout)
○ Attempt to merge feature-branch: git merge feature-branch. Git will show
a merge conflict
○ Push the resolved changes to the remote: git push origin main
Working with remote repos
● Managing remotes
○ Adding remotes: git remote add remote_name remote_url e.g. git remote
add origin ../git-remote
○ Changing a remote’s url: git remote set-url remote_name new_url e.g. git
remote set-url origin ../git-remote/remote-repo
● There’s still a world of git commands and functionalities out there but we have
covered the most essential. The rest you shall encounter on your
codeventures.