Getting Started With Basics Of Git Commands And How To Start Contributing.…

Kavya Bisht
AnitaB.org Open Source
9 min readMay 3, 2020

Nowadays if you look around you [i mean online…!!] you will get to know that many organizations and many other online hackathons are more focusing on these GitHub contributions by the contributors around the world.

I have attended several sessions for beginners where I was told with the ways to start contributing to various open-source projects. Now, from being a participant in various online competitions like Hacktoberfest , GSSOC — Girslscript Summer Of Code to being a mentor in Google Code-in [under AnitaB.org], I got to learn so much and now, I feel like I have some sort of experience which I can share with my fellow ones to start their journey as well in open source.

After, interacting with so many newbies who want to take my advice to start their journey in open-source as well, I finally thought to write this new article for them. Although I would not go too deep in its theoretical part [for that many other tutorials and articles are already there] as in this article my intention is to help you start contributions without much effort….

Now, What exactly are git and GitHub? :

Github is a remote code-hosting platform for various Git repositories where in your files are hosted which you can further share with anyone..

Git is a distributed version control system for tracking those files which you are hosting on Github . Basically, In Git there are some commands which you have to write in your git bash terminal [similar to your windows command prompt] and with the help of these commands you can track the versions of your code in your local machine . However, if you want a remote backup of your code or want to share it with others for any sort of collaboration then you can push it to Github.

Now, If you are not satisfied with the above definitions and want to know more about version control, Git or Github then can you refer to this article !!

Now to start writing git commands you need to set up or go through the installation process of git for which you can follow this article

For setting up the Github account you can follow this article

Now we are ready with everything so, let's start your journey …..

Note:- [Assuming that you have already installed everything [that too without any error]]

Now you can start to choose which project you want to contribute to.

So, to make things easier for you lets take an example, let's say you found one project/repo named TestGit and you want to add/edit let's say it’s README file…so, to add your file/edit this existing one into your repo you need to follow the following steps :

Step-0: To start your contribution, you first need to create an issue or you use the existing issue in the parent repo [of any organization] by commenting/asking on that issue created by others. In my case, I am just editing the existing READme file So, for that, I will create an issue in the parent repo, describing what I want to add in that repo [as a contributor] and then the maintainer of that repo will assign me that issue :

First Go to Issues [of organization's/parent/remote repo] then click that green button of **New Issue** :

Creating New Issue

Next, fill up that issue template first about why you are creating or describe your issue and then click **Submit Issue** [Note that, some repositories have specific templates so, you need to follow that only. Here, there is no such template]:

Submitting New Issue

Step-1: Once you are assigned for that issue [by maintainer] then next, you need to fork that repo into your repo [this will create a new repo in your Github and will indicate this as a forked one] :

Go to Fork button at the top right corner of that repo :

Forking the repo

Then, you will see that fork sign at the top left corner [that is a forked repo] and then you need to go to clone button of your forked repo :

Forked Repo

Step-2: Then, Go to Clone or Download Button [green one] then copy that clone link.

Step-3: Open your Git Bash, then go to your desktop [or any other folder where you want to place your downloaded/cloned file] by using the following command :

cd desktop 

Step-4: Then, by using the following command in your git bash, you will be able to clone that file locally :

git clone "link you copied paste here"
I have cloned in my desktop

Step-5: Then, go into that folder which you cloned by *cd* command and then add that code or any changes you want to do in that file with the help of any editor [In my case, I am just making a small change in readme file by using atom editor].

Step-6: Now, it is always good to make a new branch every time if you are contributing to any open source project as this will not mess up the things you are putting up in the remote repo (i.e. repo of that organization which is parent repo) :

git checkout -b "your_branch_name"
Creating a new branch to add changes

Note: Don’t use space while naming a new branch.

Step-7: Now, to see what’s going on in your working directory, run the following command :

git status 

this command will show you all the files that you have modified as an untracked/not staged for commit :

Shows the file which I modified

Step-8: Now, next you need to add all the modified files into the Staging area [wherein you collect all the changes you want to put into repo] by using the following command :

git add "modified file"

Step-9: Next, you need to commit all your added files with the appropriate message. this will be added into your local repository [the forked repo] :

git commit -m "any meaningful message" 

Step-10: Next, you need to add your modified files [which you committed] from your forked/local repo to the remote repo [the original/parent repo] so, for that you need to **Push** them :

git push origin "your branch name"
Pushed Modified File Into Repo

Step-11: It’s almost done, next you will see the message on the remote repo with the green button “Compare & Pull Request”. You need to click that and then this opens a new PR where you need to write the description with the reference to that issue which you created [fixes: #1 — in my case] and then click that green button at the bottom of that saying “Create a pull request” that’s it !! :

Pushed Branches are Shown in your repo/forked repo
Creating a new PR

Yay!! you made your first contribution in the open-source world! Cheers…

Now, Next, are some of the steps which can follow to enhance both your PR & Contribution.

Step-12: Now, suppose one of the maintainers of the remote repo told you to do some changes in your pushed files so, for that, you again have to make changes locally and then follow all the steps from step-7 to step-10

[also, make sure you use that same branch for making changes, if not then type: ```git checkout “branch name”``` → this will bring that earlier same branch which you used to push your changes] :

Pushed new changes — Follow all the steps from step-7 to step-10 — After making changes [& saving them]

Step-13: Now, Suppose after making changes, you added your new commits and now, you can see that there are many commits :

there are two commits instead of one.

so, there is one option if you want that all the changes or all the commits should squash into one or gets combine into one then for that use the following command :

git rebase -i HEAD~n

where ’n’ will be the no. of previous commits you want to squash together. In my case, n is 2 as there are two commits which I want to squash together :

Using Rebase Command

Step-14: Next, this will open the vim [or nano if you opt this during installation] editor for you where you will see all the ’n’ commits which have committed into the remote repo.

Then type “cw”, this will allow you to edit in the editor.

Now, just change that “pick” to “s” / “squash” across that commit which you want to combine/squash with the previous commit and this will result in one commit rather than many into your sent PR.

Now, after editing, you need to save and exit so, press “esc” on your keyboard and then type “:wq” , this will save your edits and exit.

Note: I am using Vim editor, if you have nano editor then use these commands.

Then, after this, a commit message editor will get open so you can change the commit message. For that again type “cw” on your keyboard and then change that message “#This is a combination of 2 commits” to your desired message [in my case, I writing #Adding some changes into readme file] and then again type “esc” & “:wq” :

Vim Editor Will Get Open On Typing Rebase Command [will show those two commits]
Changing “pick” of second commit to “s” /”squash” [to get meld/merge into the previous commit]
Shows Commit Message Which You Need To Change According to What you want for the merged commit to have
Added My Message In Place Of Default One
Successfully Rebased After Modification Of Commit Message

Then push your changes with the following command:

git push -f origin <branch-name>
Pushed The Rebased Changes Into The Remote Repo

Now, you can see there is only one commit :

Both Commits Are Merged/Squashed Together Into One

Step-15: Next, if you want to drop/delete some irrelevant commits which you committed by mistake [which will happen as we are human!!!] then, just follow the previous step-14, and you just need to change “pick” to “d” / “drop” across those commits which you want to delete while editing.

Step-16: Now, suppose maintainers said you to make some changes and then you thought to make those changes after some 10 days or so[due to busy schedules] then if you go to your forked repo, you would see some message :

Just a Random Example To Show Message

so, this means, many contributors have already made so many commits in those 10 days when you were busy so, this means that you need to update/sync your forked repo with original/parent repo. so, for that, just “cd” into the directory of the cloned repo and run:

git remote add upstream "https://github.com/<parent/original-repo-name.git> 

To verify that parent is added as remote [i.e. upstream] or not, type :

git remote -v 
Here origin is your forked repo and upstream is for parent repo

Then, fetch the upstream repo by the following command :

git fetch upstream

Then, this will fetch all those commits which are different from your earlier fork and will be in separate branches in your local environment so, to merge all of them together run :

git merge upstream/master    

then, at last, you need to run :

git push 

this command will update your forked repo and then, as a result, you will see “This branch is even (with Parent repo)” message in your forked repo.

I hope all of the above steps will be helpful for you to start your first contribution to open source.

These are the very basic tools/commands which you can use to start your contributions but don’t forget to download this cheatsheet if you want to master some more git commands and enhance your git skills.

If you like this article, then don’t forget to give a clap and also, share it with others as this will motivate me to write more such articles for you … :)

Keep Coding & Do continue your open-sourcing !!!!

You can get in touch with me on Twitter, Github, and Linkedin.

--

--

Kavya Bisht
AnitaB.org Open Source

Data Scientist 💼 | AI/ML/DL Enthusiast |Open source contributor | GCI’19 Mentor under Systers/AnitaB.org | GSSoC’20 Mentor at Girlsscript foundation.