Git for Open-Source

Naveen Sundar
NITRR Open Source
Published in
4 min readApr 8, 2020

This story is part 3 of Open Source 101 Series

Previous story: Understanding an open-source project

Open-source software development requires you to think the git commands in terms of remotes and branches. Which normally, we dont worry about, since we use one remote and one branch for personal projects.

Believe me when I say “Yet another git tutorial is the last thing we wanted to create”. People often accumulate bad habits when using git, like messing up the branch history, wierd workarounds to solve conflicts, deleting local codebase + fresh cloning — to list a few. This + next set of articles will help you make better decisions while using git.

I recommend you go through this git tutorial after reading the article:

Git & GitHub Tutorial for Beginner- Net Ninja Youtube

Repository — A place to store code

  • Online repository — Like projects in github
  • Local repository — The projects in your local machine.

Remote — URL of the repository

remote
# Example: Remote of mentorship-backend project of anitab-org 
https://github.com/anitab-org/mentorship-backend.git

A “commit” in git is the way of saving changes you made to the files.

An example commit

Things to note in a commit:

  1. Commit has a message. It says what is the purpose of the changes.
  2. The author and timestamp of the commit is noted. So he/she is credited for that piece of code. If somebody steals your code you can argue that you made the commit earlier!
  3. A unique hash is generated for each commit, to identify the commit.
  4. A commit stores only the changes in the files and file creation/deletion. Not the entire contents of files. As we can see that plus+(green lines) are added to the file and minus- (red lines) are deleted from the file. Also we can see that, there is total of +54 and -9 among 5 files included in that commit.

How to create a commit?

Commit Procedure
  1. makes changes in the code
  2. selected files to be included in the current commit
  3. create the commit
Wooof! Thats a lot to take in! You can relax with this cat picture 😃

If only changes in files are saved then how do we get the complete codebase?

git branch example
  1. Git actually forms a tree with our commits.
  2. Git stores our commits in a sequential order called “branch”. The changes are put “one on top of another” to create the complete codebase. We can have multiple branches!
  3. In the above picture you can see that commits are laid on top of each other.
  4. The tree is rooted in remote named “origin”. We can have multiple remotes!
  5. We have 3 commits. You notice the top commit is gold colored. Thats because our “head” lies there.
  6. “Head” is the commit+branch combination on top of which the developer is “currently working on”. Our head is on top of “commit 3” of branch “master”.
  7. You can see that selected files and altered files lie on top of “commit 3”. This is because when “staged files” are commited it becomes “commit 4”. Also when we “stage the files” it becomes “selected” and ready to “commited”.

push — upload the commits to online repository

push workflow

pull — get new commits from online repository

clone — creating a copy of online repository in local machine

clone workflow

Git commands

1) create a new git repository
git init
2) clone a online repository into local machine
git clone <remote-url>
3) add a new remote
git remote add <remote-name> <remote-url>
4) staging specific files
git add <space separated list of files>
5) staging all files
git add .
6) view file changes
git status
7) commit the staged files
git commit -m <commit message>
8) view commits history
git log --oneline
10) push the commits to online repository
git push <remote-name> <branch-name>
11) pull the commits from online repository
git pull <remote-name> <branch-name>

In the next article we will learn how to use “git branches the right way”. Follow our publication for more open source articles. :)

--

--

Naveen Sundar
NITRR Open Source

Full Stack Developer. Open Source Contributor. Space Enthusiast. Viva la SpaceX.