Introduction to Git.

The commands you need to get started on GitHub.

Mohammad Anas
5 min readMay 30, 2021
Photo by Roman Synkevych on Unsplash

In this post, I’m going to show you how to install Git on your Local machine and push/pull your codes to your GitHub Repository. So let’s jump right into it and get started!

First of all Install Git on your local system by using your CLI(terminal) by following the instructions in the given link Install Git.

Configure GitHub on your local system

git config global.username “GITHUB_USERNAME”git config global.name “YOUR_NAME”

After configuring GitHub on your local system navigate to your target folder where you have your project file and initialise Git.

git init: command is used to call Git and initialise in that particular directory.

Connecting GitHub Repository with local repository

Create a repository using the web interface and then add that particular repository to your currently initialised git folder

git remote add origin REPOSITORY_LINK

Pull the code from remote repository to make sure both versions (local and remote) are on the same HEAD.

git pull origin master

Git will pull the code from remote repository into your local system.

But first we’ll need to create a Personal Access Token for accessing the repositories. Scroll down to the end of this article for that part.

We can use any editor for changing/updating the contents of the file. For example we can use nano editor.

nano filename.txt

CTRL + O to Write Out, press ENTER and then Ctrl +X to exit the editor

We can display the contents of the file by using cat command.

cat filename.txt

For updating the changes in the file:

git add . : adds the file to the staging areagit commit -m “COMMIT_MESSAGE” : Adds the commit messagegit push: Pushes the changes on the current branch to your remote repository.

git status is used to check the status of that particular directory whether they are added to the staging area or not.

git log command is used to check (history) of all the commits that took place.

It is an ideal practice to PULL your code from the remote repository when working in a team to ensure that all members are working on the same updated version of code.

git pull origin master

Branching

Branching is the concept of creating a copy of the original code into another space for experimenting on it (i.e. updates, changes, error solving etc.). This does not affect the original code (master branch), however it can be merged with the original after thorough testing. Merging is discussed in the following section.

master is the default branch which gets created when the repository is initialised.

git branch : Shows all the branches presentgit branch -b "BRANCH_NAME" : Creates a new branchgit checkout "BRANCH_NAME" : Switches to the specified Branch

In the above image we are in the MLDev branch, changes made here will not affect the master branch. As it is visible we have added line 6 in the file.

Now we’ll shift to the master branch using the git checkout master command.

The moment we shifted to the master branch, the changes made in the MLDev branch disappeared. Now if we want the changes made in the MLDev branch to be displayed in master we have to merge the branches.

Merging

git merge [BRANCH_NAME]: merges the branch with mastergit branch -d [BRANCH_NAME]: deletes the branch

Forking and Pull request

Forking is creating a replica of a repository in your GitHub account while still maintaining a link with the original repository. This allows us to fetch any changes in the original repository to our local repository or to contribute something to the original repository.

Steps for Forking and Opening a pull request

First, go to the repository you are interested in. You’ll see a fork button on the top right corner. On clicking it will make a copy of that code in our repository.

Now copy the link of the forked repository.

Use the git clone [REPOSITORY_ LINK] command to clone the code in your local system.

After adding the repository in the local system we can make changes to the files. Git tracks those changes and we can view it real time by using the git status command.

Next we have to use git add . for adding those files to staging area and then

git commit -m “COMMIT MESSAGE”git push

Earlier, You weren’t able to PUSH the changes just by cloning the code from the Repository But now since the code is a replica in your repository you can perform the PUSH operation.

After PUSHING, if you feel your code is an enhanced version of the original code and adds value to that code then you can open a pull request which’ll send a request to the original owner to include these changes in his repository.

And Yippeeee. You just made your first contribution to Open Source.

Generating a Personal Access Token

To generate a token go to the settings by clicking on your

profile > developer settings > Personal Access tokens.

Generate the token by selecting all the permissions you need and make sure to copy the token and paste it somewhere on your PC for further use.

Now this token is your new “password” for accessing GitHub through the CLI/terminal.

To avoid writing in your username and password every time you PUSH or PULL changes to your repository you can cache your credentials by using the following command.

git config — global credential.helper cache

Now enter a command which requires the use of username and password.

For eg: git pull [BRANCH_NAME]

Now Enter your username and password ( Token you just generated). Git will cache this username and password and you won’t need to enter these again.

--

--

Mohammad Anas
Mohammad Anas

Written by Mohammad Anas

Newbie in the world of extensive coding. Software Engineer, Generative AI, Golang

Responses (2)