Understanding Git Basics- Part-1

Tharuja Sandeepanie
5 min readFeb 12, 2020

--

In this article , I’m focusing on working with Git repositories, which would be needed for you if you are a programmer and still haven’t used Git as the version control system.

First, you need to understand the git working flow. When working with git , there are 3 types of repositories you have to deal with.

1- Local repository — When you are working on your local machine that working directory is your local repository.

2-Remote repository — It is a common repository that all your team members can use to exchange their changes. Such remote repositories are GitHub and BitBucket.

3-Upstream repository — It is the Parent/Original repository from where you forked your repository. Then , forked one will act as a remote repository.

Fork

Forking is getting a copy of an upstream repository for you. So you can do your own changes freely without affecting the original repository. You can fork a repository as below in the upstream repo.

Once you forked a repository you will have an individual copy of it in your GitHub account.

Clone

After forking the upstream repository , you can make a copy of it into your local machine by cloning the remote repository. Go to your remote repository and copy the link as shown below. Then give the following command in terminal in where you want to create the local repository.Make sure that you have installed Git into your machine before.

git clone <copied link>

Now you will have a local repository in your machine, which is an exact copy of your remote repository and you can do any of your changes as you want in your local repository.

Commit and Push

There are 3 GIT modes for your changes — unstaged, staged & committed.

1- When we modify files or add new files, they are in the unstaged mode. Using “git add” those changed files will be moved to staged mode, which means into the queue to be committed later.

git add .   //to add all the changed files
git add <file-name> //to add a specified file

Here we have an option to return back to unstaged mode, so you can do further new changes or undo previous changes.

2- Then after, we can commit the files , which were added to staged mode using below command. It will create a new revision with a log.

git commit -m “commit message”    // if commit message is not      given, text editor will be appeared to give the message.git commit -a    //stages the changes of all tracked files and commits them. (git add -u + git commit)

3 - Now, you can push these committed files to your remote repository using “git push” . (As you created this local repo using cloning your remote repo, no need to add the remote repo here. Name origin refers the remote repo.)

git push -u origin <branch-name>

This will create such a branch in your remote repository, if there was no such branch before. If you are publishing a local branch for the first time for the remote, the “-u” option is helpful. It will create a tracking connection between the local and the newly created remote branch. After having a tracking connection, you can use only git push command without options.

git push

Send Pull Requests

After adding your changes to your remote repository, to merge those changes to the parent/upstream repository , you should create a pull request as shown below.

1-Go to your remote repo and checkout to the branch to which you pushed your changes and give “new pull request”.

2- Give “create pull request” and make sure that you send the PR to correct base repo/branch. You can see your individual changes here.

3- Give a name to your PR and create the PR.

Once you created a PR , a member from the upstream repo, will initiate a build of your pull request and can review your PR and then if there are no any conflicts , your PR can be merged to upstream repository successfully.

Syncing your repository with upstream repository

As you wanted to merge your changes to upstream repo , there will be so many other members whose PRs might have been merged already to upstream repo after you forked it. So, if they have done a change in a file in where you also changed later , it will cause merge conflicts.

To avoid these conflicts, you need to sync your local repo with upstream repo with any later changes made in the upstream repo, before starting your changes. Once you forked and clone a project, no need to repeat these steps, instead you can do sync with few steps.

1- First, we need to add the parent repository as an upstream remote for our repo.

  • Copy the URL of upstream repo.
  • Inside the local repo directory give below command:
git remote add upstream <URL>

Give “git remote” to see your current remotes.

2- After that, update your local repo using following steps. It will pull down any changes in upstream repo and will do the syncing. Before updating, you need to commit or stash all the changes if you did any.

  • Fetch all of the branches from the upstream repository. This will display the branches that were downloaded:
git fetch upstream     , or 
git fetch upstream <branch> //only fetch the specified branch.
  • Switch to your branch that need to do sync and do merge.
git checkout <branch>
git merge upstream/<branch>

Or, instead of doing both fetch and merge, you can simply use following “git pull” to do the fetch and merge at once.

git pull upstream <branch-name>

Merge will fail if any conflicts occurred here. Then you have to resolve them and do commit.

--

--