Working like a “Master” with git

Chameera Rupasinghe
5 min readOct 8, 2019

--

If you are working on a project which is been contributed by many others, here is a nice way to do it without a hassle. When everyone is pushing their changes to the same branch, you have to stay updated while contributing to the code without destroying it.

Getting along with a remote repository

Starting from zero

Important: You should have installed git on your local machine before executing git commands. Find the guide for installing git at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

First of all, you have to create a remote repository if there is no existing work done by others. The easiest way to create a remote git repository is logging into your VCS or the version control system (Github, Gitlab, Bitbucket, etc.) account and creating a new repository with the aid of the web interface. Then you can clone the newly created repo to a local directory using the HTTPS or SSH link of the repo available at the web interface. You can use “git clone” command in the terminal after navigating to the local directory.

$ git clone https-or-ssh-link-to-your-remote-repo

Important: If you want to use SSH, you have to set up the SSH keys on your local machine and the version control system first. Follow the steps at, https://build-me-the-docs-please.readthedocs.io/en/latest/Using_Git/SetUpSSHForGit.html

If the cloning is successful goto the working with branches section of this article.

If you have developed your code locally

If you have done your work beforehand without making a remote repository, then you have to push your code to a new remote repository. First, create a new repository using the web interface of your VCS. Then navigate to the local directory where your code is using the terminal and execute the following command.

$ git init

The command above will initialize the local directory as a local git repository. Then use the following command to add the files in your directory to the local repository.

$ git add .

Then you’ll have to commit your added files. Use a commit-massage to describe what you have done at this point. eg: First commit.

$ git commit -m "commit-message"

Now you have to set up the remote address where your code will be saved to. In this case, you have to add the URL (HTTPS or SSH link) of your newly created remote repository.

$ git remote add origin remote-repository-URL

To verify whether you have set up the remote origin properly, you can use the following command.

$ git remote -v

Finally, you have to push your code to the remote repository you have just finished setting up. Use the following command.

$ git push origin master

Now you are good to go. Continue from the working with branches section.

If you have to work with a code in a remote repository.

If you are asked to work in an existing project or you are contributing to a project as a freelancer, you have to clone the code in the remote repository to your local machine. First, create a local directory where you plan to work. Then clone the remote repository using its remote URL.

$ git clone https-or-ssh-link-to-the-remote-repo

Important: Make sure you have permission to contribute to the remote repository. If not, talk to the owners of the remote repository.

If you have permission, now everything should be set for the interesting part.

Working with branches

When you are contributing to a remote repository it is vital to stay updated while doing your work in it. Git will help you with this, only if you know how to get that help.

Work in a separate local branch

As a good practice, you can work in a different local branch other than the “master” branch. After setting up the connection with the remote repo as mentioned in the first section, you can branch out to a new local branch.

$ git checkout -b new-branch-name

Do all your work in this branch while keeping the local master branch synced with the remote repo or the branch you are supposed to push your work.

Staying updated

While you are working on the code, others might have done some changes and pushed to the remote repo. It is important to grab these changes into your local repo to avoid annoying merge conflicts when you are going to submit your work. To do this first you have to keep your changes safely if you are not ready to commit it. Use the “stash” command to remove your changes temporarily.

$ git add .
$ git stash

Now check out to your local master branch.

$ git checkout master

Now pull the code from the remote repository. This will bring new changes if there are any. (Replace the bold text with the relevant name of the branch)

$ git pull remote-branch-name

Then you can use the “rebase” command in your woking local branch to apply the newly received changes to the working local branch.

$ git checkout working-local-branch
$ git rebase master

Now the working local branch has the latest update of the code. Now you can restore the stashed changes to the working local branch. Use the following command.

$ git stash pop

This might cause some minor merge conflicts. Use a git merge tool to resolve the conflicts. Read the documentation on git merge tool here https://git-scm.com/docs/git-mergetool

Now you know how to stay updated. Let’s see how to push your changes to the remote branch without getting into trouble.

Push your work to remote

To push your work to the remote branch in a remote repository, follow the commands given below. You should be in your working local branch. (Replace the bold text with the relevant name of the branch)

$ git add .
$ git commit -m "commit-message"
$ git checkout master
$ git pull remote-branch-name
$ git checkout working-local-branch
$ git rebase master
$ git checkout master
$ git merge working-local-branch

Now the local master branch is ready to push. It has the latest update from the remote repo as well as the new commit of your work. Now push the code in the master branch to the remote repository.

$ git push origin remote-branch-name

After this, your changes will be successfully added to the relevant branch of the remote repository. Now you can continue to work on your working-local-branch.

$ git checkout working-local-branch

I hope this article was helpful to enhance your VCS experience and work with less trouble.

--

--

Chameera Rupasinghe

Senior Software Engineer @WSO2 | Computer Science and Engineering