GIT — GITHUB

Mercy Jemosop
13 min readAug 9, 2021

--

learn how to work with Git and GitHub

Introduction

Git is a free open source distributed version control system. It is tracks the contents of a file over time, it facilitates GitHub activities on your laptop or desktop.

GitHub is an online platform that allows you to synchronize your local Git repository onto the web. It is a web hosting service for the source code of software development projects or text based projects that use git The container of the source code of a particular project is called repository.

Git(Locally) is the workflow to get things done and GitHub(server) and GitHub Pages are places to store the work done.

Version control system is a software that maintains a history of what changes have been made to a file overtime. This is important since one may need to go back and review earlier contents of a source code.

Importance of version control

- Allows developers to work simultaneously.
- Does not allow overwriting each other’s changes.
- Maintains a history of every version.

Distributed version control is a type of version control where the complete code base including its full version history is mirrored on every developer’s computer.

- Branching and merging can happen automatically and quickly
- Developers have the ability to work offline
- Multiple copies of the software eliminate reliance on a single backup

Installing Git to your PC.(Ubuntu).

Installing git for windows or research for the best alternative.

The first step is to check if git is already installed in your computer, run the command below in your terminal(CTR+ALT+T to open the terminal). If you get an output of git version number, you already have git installed in your PC, skip this part to the setup section

git --version

Installing GIT using APT

Step 1: Update your local package index.

sudo apt update

Step 2: Install GIT

sudo apt install git

step 3: Check if git has installed successfully

git --version///output should be something like 
git version x.xx.x

installing git from source is also another alternative to install git to your machine.

Setting Up Git

We need to configure git, this will be important because the generated commit messages you make will contain your correct information and support you as you build your software project. This is done by providing our name and email address which will be embedded to our commit message by git.

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
/// optional: automatic command line coloring for Git for easy reviewing git config --global color.ui autoset

run the command below to view configuration

git config --list

To make changes to the details, run the command below.Press CTRL and X, then Y then ENTER to exit the text editor.

nano ~/.gitconfig

This is basically all you need to get started with git in Ubuntu.

Create a GitHub account

We need to create a GitHub account in-order to deploy our project(this can be any project, even a folder with a single hello world file).

Step 1: Follow this link to redirect you to GitHub

Step 2: Type a user name, your email address, and a password.

Step 3: Choose Sign up for GitHub, and then follow the instructions and click the create account button. Follow the additional instructions till you can access your account.

Deploying an existing project

There are two ways to go about this:

Git cheat sheet

a). Adding the changes or updates directly to the main branch(default branch).

All developers work on a single branch with open access to it

This method is easier if you working on a personal project. By default a repository has one branch main when created. This method cannot be used when making project collaboration with a team. Adding changes directly to a branch may result in conflict.

pros

  • It enables them to introduce new improvements quickly and without unnecessary bureaucracy.
  • real continuous integration (with all its benefits like discovering bugs sooner)
  • prevent merge conflicts.
  • less cumbersome and easier to master in git.
  • perfectly fine when you are the only developer.
  • each commit is a small step that minimize the risk of introducing a big bug hard to find the cause in the lot of lines of code changed.
  • require to have good development practices like writing unit tests .

cons

  • not a clear moment when a feature is introduced (should have a look to the feature toggle enabled).
  • The only way to review code in such an approach is to do full source code review
  • Developers that work in such style should be experienced so that you know they won’t lower source code quality.

Step 1: Create a GitHub repository

Repository is a container used to organize a single project.

Create a new repository

  1. In the upper right corner, next to your avatar or indention, click + and then select New repository.

2. Name your repository chat-app(give any relevant name for your project).

3. Write a short description. The description can be long or short depending on the message you are passing.

4.Select Initialize this repository with a README.

5.Click Create repository to get started.

N.B. If you don’t initialize your repository with a read me. This will be your starting page on GitHub. I recommend not initializing your project with your read-me if you not familiar with git and GitHub to get the easy walk through.

N.B the name of the repository is test in the example below, i created 2 different repository for demo but i will be working with chat-app repository. Hope you don't get confused.

This will be the starting page of a repository with read me initialized.

Let’s go back to our machine/PC

step 1: open the terminal and navigate to the project directory.

step 2: Initialize the local directory as a Git repository.

git init

step 3: Add the files in your new local repository. This stages them for the first commit.

$ git add .

Step 4: Commit the files that you’ve staged in your local repository.

git commit -m "First commit"

Specify the branch to commit the changes.

git branch -M main

Step 5: On your repository, click the drop down on code button. Copy the URL with HTTPS option.

or

Under the quick setup. Copy the remote repository URL.

Step 6: In Terminal, add the URL for the remote repository where your local repository will be pushed and verify the new remote URL.

git remote add origin url

Step 7: Push the changes in your local repository to GitHub.

git push -u origin main

you will be prompted for your username and password and all the changes will be pushed to git.

If you encounter the error below it means that you skipped to specify the main branch and your project will be pushed to the master branch

error: failed to push some refs to 'https://github.com/mercykip/chat-app.git'

solution:

git push -u origin master

YEEEEEEEY we have pushed our project successfully to GitHub. When you reload your GitHub repository, the project on your local machine will have been added.

Update changes to our repository

When you add more features to our project. We need to push the changes to our repository. To update changes follow the steps below.

Step 1: Update our local repository from the central repo.

///navigate to the project directory in your PCgit pull origin main

Step 2: Add the changes introduced to the file

git add .

Step 3: Check the changes you want to commit

git status

Step 4: Commit the changes. Add a commit message to describe the changes being introduced to the repository.

git commit -m "add images"

Step 5: Push the changes to the main repository

git push -u origin main

Step 2. Add the changes in our local machine to our repository.

b. Branching

This is the way to work on different versions of a repository at one time. Create a new branch and add the changes to the main branch by creating a pull request and merging the changes. We use branches to experiment and make edits before committing them to main.You have one main development branch with strict access to it. It’s often called the develop branch.

When you create a branch off the main branch, you will be making a copy of the branch with all the contents it has at the time of creating the new branch.

If someone else made changes to the main branch while you were working on your branch, you could pull in those updates.

pros

  • You could see clearly where the features are introduced in the software (when the branch is merged).
  • That makes code review easier (just before merging or during all the development if you made a pull request early, which is a good practice).
  • Easily reverted if needed (just have to revert the merge commit).
  • Easily do proof of concept (but hard to reintroduce in master)
  • Only working workflow when doing remote or opensource development that require code review or CI results before merging commits (but should not necessarily be used in companies because it is successful in open source world)

cons

  • merge conflict.
  • Understanding git flow maybe a bit challenging.

instead on adding our changes directly to main(default) branch, we will create a branch and then add the changes to the branch . This changes will then be merged to main branch.

Add an existing project to GitHub

git init
git add .
git commit -m "my commit"
git branch -M main
git remote add origin <remote repository URL>
git push -u origin <remote branch name>

This method is similar to the one above but the only difference is that, We will introduce a branch to be adding changes to the main branch instead of pushing the changes directly.

SKIP THE STEP BELOW IF YOU HAVE ONE BRANCH MAIN(or master) IN THE PROJECT REPOSITORY LIKE THE EXAMPLE BELOW

If after pushing your code your repository has two branches main and master and the main branch does not contain the project folder but the master, follow the steps below to make main the default and delete master.

NB. You can avoid this by running the command below after the commit message as the procedure above.

git branch -M main

The repository will have two branches. The project will be contained in the master branch.

To change the default branch from master to main

Step 1 — Move the ‘master’ branch to ‘main’

The argument -m will transfer all of the commit history on the ‘master’ branch to your new ‘main’ branch so nothing gets lost.

/// create main branch locally, taking the history from master
git branch -m master main

Step 2 — Push ‘main’ to remote repo

Push your new ‘main’ branch up to GitHub and tell the local branch to start tracking the remote branch with the same name.

/// push the new local main branch to the remote repo (GitHub)
git push -u origin main

Step 3 — Point HEAD to ‘main’ branch

Change HEAD, the pointer to the current branch reference.At this stage if ‘master’ was your default branch you cannot remove it without first changing HEAD.

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

Check whether the HEAD is pointing to main which now frees you up to delete ‘master’.

git branch -a

Step 4 — Change default branch to ‘main’ on GitHub site

Change the default branch in GitHub to something other than ‘master’. Check whether your default branch has been changed to main branch/the branch you want to set to default. If not click on the switch Icon and choose the branch you need. Mine is already selected so I will skip that step.To find this configuration go to "Settings" -> "Branches" on your repository.

Step 5 — Delete ‘master’ branch on the remote repo

git push origin --delete master

Check if master has been deleted

git branch -a

Create a merge request(MR)

Update changes to main using a branch

A branch is a version of your repository, or in other words, an independent line of development.Branches serve as an abstraction for the edit/stage/commit process.

List all the existing branches in our repository.An asterisk will appear next to the currently active branch.

git branch

if you are not in the main branch(your default branch)

git checkout master

get the latest updates

git pull origin master

To create a new branch, this will create a new branch mirroring the commits on the currently active branch.

git branch new-branch

To start working on the new branch we first need to move from our present branch to the new branch.This will change the active branch to the new branch.

git checkout new-branch

You can also create a new branch and checkout into the new branch with one command

git checkout -b newbranch

Check if we are in the new branch.An asterisk will appear next to the currently active branch.

git branch

Let’s introduce changes to the code or add a file

Add the changes

git add .

check the changes to be added

git status

Commit the changes

git commit -m "commit message"

N.B When working with a team it is good practice to have one commit message on a branch. The commit message should describe what that branch handles e.g test,fix a bug, add a feature etc.

When committing more updates to the branch. Use git amend which writes the very last commit with any currently staged changes

git commit --amend

Push the changes in your local repository to GitHub

git push origin -u await -f

Here is a summary of creating a branch (await, you can give it any name)

Merge our branch to main (default branch)

Step 1: checkout/switch to the default branch

git checkout main

Step 2: Pull the latest changes from the remote repository.

git pull origin main

Step 3:Merge the new branch to main

git merge await

Step 4: Push the changes in your local repository to GitHub

git push origin main

Delete branches after being merged

Once work is completed on a feature, it is often recommended to delete the branch.

Deleting a branch LOCALLY

Git will not let you delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting.

-d option will delete the branch only if it has already been pushed and merged with the remote branch.

-D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

delete the merged branch locally

git branch -d await

Delete a branch that is not merged

git branch -D branchname

Deleting a branch REMOTELY

git push <remote> --delete <branch>.
example: git push origin --delete fix/authentication

or

git push <remote> :<branch>example: git push origin :fix/authentication

Merge summary

merge conflicts

A merge conflict occurs when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

Conflict dividers

======= this indicates the center of conflict

<<<<<<< HEAD the contents between center and head indicates contents in the current branch main which the HEAD ref is pointing to.

>>>>>>> this indicates the new_branch_to_merge_later indicates the contents that is present in your merging branch.

Fetch the latest changes from origin and re-base branch

A rebase is what you do when you combine a commit or series of commits to a new commit. It is similar to merging in that it moves changes from one branch to another.

The git re-base command can result in merge conflicts. This can happen if you are working on a branch that has not been merged with the master branch in a long time. You should ideally use rebase to merge the histories of two branches when there are only a few changes to make.

step 1:checkout to the default branch

git checkout main

Step 2: Pull the latest changes added to main branch

git pull origin main

Step3: checkout to the new branch

git checkout new-branch

Step 4: Re-base the branch against main branch

git rebase main

In case you encounter a conflict, resolve the conflict

git add .
git rebase --continue

Step 5: Push the changes to the remote branch

git push -u origin newbranch -f

or

git add .&& git commit --amend &&git push -u origin newbranch  -f

Stash changes and apply to a new branch

Scenario. If you have created a new branch and started working and updating your code but later you realize you didn’t checkout to your branch and the changes are in your default branch. Don’t panic, follow the steps below to move the changes to your new branch

/// main or undesired branch
git stash
git checkout -b xxx
/// new branch to add changes
git stash pop

Add the changes to the new branch and push changes to the remote repository.

Update the changes to the new branch and push changes to the remote repository.

It is good practice to have one commit message to describe your branch or what you working on.

Commit amend allows you to edit a commit message. If your commit message is okay and does not need any change CTR+O and press enter to exit. Your changes will be pushed with the original commit message or an update of the original commit message. So you will still end up with one commit message describing your branch.

--

--

Mercy Jemosop

Software Developer. I am open to job referrals. connect with me on twitter @kipyegon_mercy