Git for Open-Source
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:
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
# 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.
Things to note in a commit:
- Commit has a message. It says what is the purpose of the changes.
- 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!
- A unique hash is generated for each commit, to identify the commit.
- 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?
- makes changes in the code
- selected files to be included in the current commit
- create the commit
If only changes in files are saved then how do we get the complete codebase?
- Git actually forms a tree with our commits.
- 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!
- In the above picture you can see that commits are laid on top of each other.
- The tree is rooted in remote named “origin”. We can have multiple remotes!
- We have 3 commits. You notice the top commit is gold colored. Thats because our “head” lies there.
- “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”.
- 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
pull — get new commits from online repository
clone — creating a copy of online repository in local machine
Git commands
1) create a new git repository
git init2) 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 status7) commit the staged files
git commit -m <commit message>8) view commits history
git log --oneline10) 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>