Git and GitHub. Why use them as a developer?

Demilade Leshi
8 min readJun 14, 2024

--

GitHub is a hosting platform where you can collaborate with others on your Git repositories.

GitHub Image

What then is Git?

Git is open source and free source control management[SCM].

Let’s assume I had a cooking website and I said “My eggs are from chickens that listen to the guitar every day”. But after some time, I could check it out again and change that to “My eggs are from finely bred chickens”. At this point, I could create another version or branch of the website and update the information there. Once I am satisfied, I can make a commit and my changes will be updatedon the website as well.

But say my egg supplier then says the chickens do listen to the guitar every day. I do not need to bother myself with changing the information manually, I can simply revert to my previous commit. This is the beauty of Git. I can revert to previous versions, and compare differences between versions, I can also see who changed what if I am working on a collaboration.

Git Installation

Go to git-scm.com.

Click on Downloads, select your operating system, and begin the download.

If you already have Git installed, you can get the latest development version via Git itself by running:

git clone https://github.com/git/git

Git Set Up

I am making use of the terminal provided by Git during installation, Git Bash. But Git can be run on any terminal on any Operating System including Powershell on windows.

  • Specify your name and email. Git needs to know who makes a change when something happens.
git config --global user.name "your name"
git config --global user.email "your email @mail.com"
  • Set the default branch name.
git config --g global init.default branch master
  • If you are ever confused about the use of a Git command, you can simply run:
#git name-of-command -h 
git config -h
usage: git config [<options>]

Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object

Action
--get get value: name [value-pattern]
--get-all get all values: key [value-pattern]
--get-regexp get values for regexp: name-regex [value-pattern]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value-pattern]
--add add a new variable: name value
--unset remove a variable: name [value-pattern]
--unset-all remove all matches: name [value-pattern]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
--fixed-value use string equality when comparing values to 'value-pa
  • If you need more detailed help, run:
#git name-of-command help
git config help

This opens up a manual that is hosted on your computer which you can access even if you are on the go.

To begin converting your project to a Github repository,

  • Navigate to your repository by using the cd command
 cd C:/Users/demil/Desktop/CODE/mygithubproject
  • Turn the repo into a Git repository by typing: git init
 git init
Initialized empty Git repository in C:/Users/demil/Desktop/CODE/mygithubproject/.git/
  • To view the status of your git repository, type: git status
 git status
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
noofchickens.txt

This says that all my files are untracked. This means that if I make chnages to the files, Git won’t know of care. How then do we track files?

We simply run git add with the file name. To track my index.html file, I run: git add index.html

We can then run git status again to check the current status of our repo

 git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html

Untracked files:
(use "git add <file>..." to include in what will be committed)
noofchickens.txt

If for whatever reason, I’d like to untrack index.html, I can run git rm(remove) — cached then specify the file name. Then run git status again to check the status of the repo.At this point the repo will have the same status as when it was just initialized.

  • If there is file I do not want anyone to have access to, infact I would like Git to totally ignore it, we create a new file called .gitignore in the root of our directory.

Here we specify all files we want to ignore. I would like to ignore my “noofchickens” file. In this case, I’d like to simply ignore all txt files.

#ignore all .txt files
*.txt

This is ideal for log files, auto-generated files, or too large files that you don’t want to include as part of your project.

  • We can also track all files at the same time by running: git add . or git add -A or git add — all. All these would track all the files in the repository.

Currently, all files are in an environment called staging. Staging is simply where your file sits until you are ready to commit them. To remove them from this environment, we need to make a commit. Now what does it mean to commit? To commit, you are taking a snapshot of your repo at that point in time. Basically what your files look like at that time. It is something like writing in a history book. If you ever need to go back to that point in the future, you can always do that.

  • To commit, we use the commit command:
 git commit -m "first commit"
[master (root-commit) c888e01] first commit
3 files changed, 15 insertions(+)
create mode 100644 .gitignore
create mode 100644 index.html
create mode 100644 noofchickens.txt

Now the files are part of the Git record. We can type git status and see that there are no files to commit.

 git status
On branch master
nothing to commit, working tree clean
  • After we have made several commits or changes to the git log, we might want to review all the different commits we’ve made. We do this by running git log. Each commit has a unique identifier, the name and email of the author as well as the date and time the commit was created. All commits are listed from most recent to oldest.
$ git log
commit 61701b494d835268ba18c5017a5b403012aa1e80 (HEAD -> master)
Author: Leshi Taiwo <98643006+demi05@users.noreply.github.com>
Date: Fri Jun 14 13:30:26 2024 +0100

added new file

commit c888e01968bf4ea1d154bcaabc7c1bfac3753830
Author: Leshi Taiwo <98643006+demi05@users.noreply.github.com>
Date: Fri Jun 14 13:01:51 2024 +0100

first commit
  • To view an abbreviated form of the log, we run git log — oneline
$ git log --oneline
61701b4 (HEAD -> master) added new file
c888e01 first commit

Branches

So far, we have worked only in the master branch. But we can set up other branches to work in. What then are these other branches? Another branch in your repo is simply a copy of the main file you are working in(ours being master). The new branch has all the same entries as your main branch. Branches are useful when you are working on a feature or fixing a bug. You can make all your changes in a separate branch and it will not affect your main branch. Once you are satisfied, you can then merge back into your main branch.

This is how software development usually works especially if there are many developers working on a project. They create a branch to work on their separate features and merge back into the main branch once they are satisfied.

  • To create a new branch, we run git branch filename. We run : git branch mynewbranch. We then run git branch to check if the branch has been successfully created.
$ git branch
* master
mynewbranch

The asterisk specifies the active branch. To chnage branch, we run git checkout filename. To change to the new branch we just created, we run git checkout mynewbranch. We then run git branch once again to confirm the active branch.

$ git branch
master
* mynewbranch

I have made changes to myrecipe.html in mynewbranch. Now I need to merge these chnages back into master.

 git merge -m "merge file back into master" mynewbranch
Updating 61701b4..5e17585
Fast-forward (no commit created; -m option ignored)
myrecipe.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

Make sure to track the file using git add, make a new commit using git commit -m “added changes to my recipes” and switch back to the master branch before merging.

Github

We have been working on our local machine, but we can also host our repo on the cloud. And one of the best ways to do this is by using GitHub. Not only does it offer a cloud repository, you can also manage your project, track issues an deven assign bug fixes and features if you are working in a team.

To get started, go to github.com, enter your email address and sign up.

Once you have signed up, you can then create your repo by clicking the new button on the left hand side of the screen. Go ahead to fill in the necessary details like the name, an optional description, determine if you’d like a public or private repo. You can also add a gitignore file just like we did above and also add a license, then you click Create Repository.

We see two options we can choose from, we go with the second one since we already have an existing project.

 git remote add origin https://github.com/demi05/mygithubproject.git
git branch -m main
git push -u origin main
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (11/11), 1.90 KiB | 974.00 KiB/s, done.
Total 11 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/demi05/mygithubproject.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
  • The first command menas we are establishing a remote connection with GitHub and we are calling this remote connection origin. We also have the link to the githu repo.
  • The second command sets the target ranch to main
  • Lastly, we push all the contents of our local repo to the cloud using git push.

Conclusion

Git is a must-have tool for developers. It’s got great version control, collaboration, and project management capabilities. With Git, you can track changes, revert to previous versions, and work on different features or bug fixes independently. And if you host your repositories on GitHub, you’ll level up your collaboration game with cloud storage, project management tools, and issue tracking. Mastering Git and making the most of GitHub will streamline your workflows, keep your code in check, and help your team work together like a well-oiled machine. The result? More successful and productive projects all around.

--

--