The Hitchhiker’s Guide to Git and GitHub
“It is easy to shoot your foot off with git, but also easy to revert to a previous foot and merge it with your current leg.”
— Jack William Bell
I know.
Git is overwhelming.
All these cryptic commands that start with the word git
and somehow helps you collaborate and ship awesome software.
Story Time : When git feels necessary
Imagine you and your friend Bob just completed a simple course on Web Development. You both are now pretty good with HTML/CSS and some JavaScript.
You both decide to build a simple website together.
Bob said he will create the index.html
page, add some code and email it to you so that you can do the css. The email comes through. You add some css and also make some changes to the index.html
file as you see fit.
While you were doing the css, Bob made some changes and added some code to the index.html
file on his computer, so now you have not even gotten 2% of the project done and both of you already have 3 versions of the same file.
Imagine the chaos if this went on and on just for a week, you will literally be tearing your hair apart just trying to manage all the versions of the code. And that’s just 2 developers working, imagine the chaos and mismanagement if it were 5, 10, 100 ?
That’s exactly the problem Linus Torvalds faced when he was developing the Linux Kernel.
So he built a solution. He named it Git.
Its what today companies with hundreds of thousands of software engineers use to do version control.
Pretty cool eh ?
Git and Teamwork
The greatest power of git is the how much it empowers collaboration.
Git repositories are the directories tracked by git.
Developers make changes to the source code and then commit them — which is equivalent of taking a snapshot on that current version. You can revert back to previous commits if you mess up later.
These commits are pushed to a remote repository hosted on some service like GitHub or GitLab.
So all the developers keep pushing commits to the remote repository and the other developers pull in the latest changes.
There are a lot of other services used alongside git to streamline the process, but this is the most simple explanation of how the cycle looks like.
In this way everyone is up to date with the latest changes and software is built collaboratively commit by commit.
Installing Git
- Download git from https://git-scm.com/downloads
- Read this article if you need help installing git https://www.atlassian.com/git/tutorials/install-git
Basics of Git
If you google the term “git”, google will slap this on your face :
Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers.
In simple words, Git can be used to track changes in any set of files.
There are a few set of commands you will have to understand, then you will be able to be understand the entire git & github workflow.
git init
— initialise an empty git repositorygit add
— stage files for commitgit commit
— commit the changesgit remote
— add links for the remote repositorygit push
— push(upload) the commits from the remote repositorygit pull
— pull(download) the commits from the remote repository
Get your terminals ready, it is time.
git init
Running git init
will initialise the current folder you’re in as an empty git repository. It will also create a sub-directory by the name .git
. This folder is now your local repository.
$ mkdir myproject
$ cd myproject/
$ git initInitialized empty Git repository in /path/to/myProject/.git/
Now all the changes you make to the files in myproject
folder will be tracked by git.
git add
Once you have made some changes and you think you are ready commit your changes, then you have to do something which is called “staging”. You have to stage your changes to commit them.
Run the following commands to stage your changes
$ git add filename // this will stage the fileor $ git add . // this will stage all the changes in the directory
git commit
This is the ninja command. It commits the changes you have staged. Run the following commands to commit your changes.
$ git commit -m "<Enter a message that describes your commit>"
What is GitHub ?
Have a look at this awesome video, it’s a really nice intro to what GitHub is all about.
Git and GitHub
Whatever changes you made just now were in your local repository i.e the folder in your own computer. Now you will most likely need to push these changes to a remote repository which will be hosted on some website like GitHub. We will just consider GitHub right now.
Do the following things :
- Get yourself an account on https://github.com/
- After logging in, Click on the “New Repository” button
- You will most likely see a screen like this
- Click on Create Repository
- Now you will see instructions to add this repository as a remote
Copy that link that ends with .git, Now go back to your terminal and run the following command, paste the link after origin
:
$ git remote add origin https://github.com/username/reponame.git
This will let you push your commits to your github repository, which is now your remote repository
git push & git pull
To actually push your commits to your github repo :
$ git push origin master
You will be prompted to enter your username and password. Enter those and your commits will be pushed.
You can go to your github, click on your repository and you will see your changes there.
If you’re working on a team and someone else pushed some commits on your repo, then you can run the pull
command to get all those commits.
$ git pull origin master
Doing this will download all the commits you didn’t have and get your local repository up to date with the remote one.
More on GitHub and Git
There are 3 more concepts you need to understand to be a champion in using GitHub for your projects.
- Branches
- Forks
- Pull Requests
Branches
Branches are essentially to make a copy of your current master branch so that you can add and test more features without messing with the master branch and later merge your feature branch with your master.
Read this awesome article by Atlassian to understand branches in git.
https://www.atlassian.com/git/tutorials/using-branches
Forks
A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project. Most commonly, forks are used to either propose changes to someone else’s project or to use someone else’s project as a starting point for your own idea.
Read more about Forks at
https://help.github.com/en/github/getting-started-with-github/fork-a-repo
Pull Requests
Pull Requests are a way to notify the owner/maintainer of a project that you have completed the feature you were working on. You write up a description of the changes you and put up a pull request.
The maintainer will either merge your pull request if its good to go.
Read more about Pull Requests at
https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests
Alternatives to GitHub
GitHub is not the only platform that helps you host your git repositories
GitLab
GitLab is really awesome when it comes to CI/CD tools, it has them out of the box. It has a polished UI and you can even download GitLab to spin a personal instance of it.
Read more about GitLab at https://about.gitlab.com/
BitBucket
BitBucket is very similar to GitHub. It is one of the tools of the Atlassian family of products for developer productivity.
Read more about BitBucket at https://bitbucket.org/product/
Resources
- An awesome book on Git : https://goalkicker.com/GitBook/
- DevOps Cycle : https://about.gitlab.com/stages-devops-lifecycle/
Tutorials on Git & GitHub:
If you’re a student
- Have a look at the GitHub Education Pack : https://education.github.com/pack
Do Drop a 👏 if you got some value out of this article.
Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬.
To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇