5 Git Commands When Working With Remote Repositories

Cam Dziurgot
Geek Culture
Published in
6 min readMar 13, 2021

Becoming proficient at git will quickly make you a valuable member of your team. The best way to learn a technology like git, is to your use. Here are few commands you will get you started working with remote repositories.

Photo by Florian Wehde on Unsplash

Prerequisites

In order to use these commands, you will want to have git, and Git Bash, installed on your machine. You can download Git here, it will give you the option to also install Git Bash. You will also want to sign up for a service that officers free online repertoires. GitHub is a popular hosting company, you can signup for a free account that offers free public repositories here.

Once you have git set up on your local machine, and a server to work with, we can get started.

1. git clone

The first command you need in order to start using a remote repository is the git clone command. To run the command, after “clone”, put the URL to the server’s .git file. Services like GitHub will give you the repository URL to copy in order to clone a repository.

GitHub button to get URL for repository.

After you get the URL, and run the command, “git clone <url>”, git creates a new directory to put the content of the repository.

Pulling down a repository from GitHub.

Now you can move into the repository and start working.

2. git remote add

While the clone command is used to get an existing repository from a server, if you want to set up a remote server for a repository you have on your local machine, you will use the remote add command.

The first step you need to do is create a repository on a server. On GitHub, do this going to your Repositories tab in your profile, and click the “New” button.

Click “New” button on the Repositories tab to create new repository.

On your local machine, if you don’t have a repository setup, you can initialized a new repository with the following commands in Git Bash.

$ mkdir foo-bar
$ cd foo-bar
$ git init
$ touch .gitkeep
$ git add .
$ git commit -m "initial commit"

Now, in the repository, you can run the “git remote add <name> <url>”. The name is the name reference for the server, which is typically “origin”. The URL value can be grabbed off the GitHub page where you set up the empty repository. After you add the remote server, you can move you local repository to the server. When you push, you will be asked to authenticate, and after that, you will see a message like the one below.

Running commands to up remote server.

3. git push

The git push command will take any of your local commits, and move them to the remote repository. We used it to move a new repository to the server. There are a few options to know when using “git push”.

In the example above, the “-u” flag was used. That flag is actually short hand for “--setup-upstream”, which sets up you local branch to an associate branch on an indicated remote server.

git push --set-upstream origin master

When that command was run in the example, it was setting the current local branch, which was “master”, to a branch on the remote server that we set up, “origin”, to a branch we called “master”.

The other push option you should know is the “--delete” option. This is used to cleanup old branches on the remote that are not longer needed. Similar to the upstream command, to delete a branch, you indicate the name of the server followed by the name of the branch. Below is an example where the “old-branch” branch is deleted from the server that is called “origin”.

Removing an old branch from the remote sever

4. git fetch

The fetch command is used to update any references to remote branches or tags. This means the you will bring those changes down from the remote server, and the local repository will be aware of the changes that have occurred, but the changes will not be made to the local repository branches.

Let’s say you are working on a project and you are on branch “foo-bar”, which is on you local machine. You have also pushed this branch to the remote, and a friend/co-worker is also working on the branch. The other person working on the branch commits some changes and pushes them to the remote. He then tells you about the changes, and you run the command, “git fetch”. The changes will come down, and you will get a message similar to the following;

Your branch is behind 'origin/foo-bar' by 1 commit, and can be fast-forwarded.

You may have some commits in your local repository on the same branch that you have not yet pushed to the remote repository. In that case, you’d see a message similar to the following.

Your branch and 'origin/foo-bar' have diverged, and have 3 and 1 different commits each, respectively.

The main take away for the “fetch” command is to remember that you have the references of the changes from the remote branch, “origin/foo-bar” in the example, but they are not in your local repository yet. The next command will be used to get those changes into your local repository.

5. git pull

The last command is the pull command. The pull command is used to take any changes that have occurred on the remote repository, and move them into you local repository. When you run “git fetch”, the reference of the changes on the remote come down to you local repository. When you run “git pull”, you actually put the changes into your repository.

It is usually a good practice to follow to run “fetch” before you run “pull”, so you can see what is actually coming into your branch — you can check the remote repositories logs if you want to, but usually seeing if there are any commits should be enough to know what you are putting in your local repository.

As mentioned above, there can be a point when you fetch the latest code for a branch on the remote, and you discover that you local branch the the remote have diverged. In cases like this, you can pull the changes from the remote into you local branch, but in order to do so, you need to create a merge commit. If there are merge conflicts, you will need to resolve them before you are able to pull — merge conflicts happen when the same file has changes on the same line and git doesn’t know what changes should be used.

If there are no merge conflicts, your terminal will open Vim in order to create a merge message. You will see a prompt to enter a commit message.

Merge branch 'foo-bar' of https://github.com/cameronDz/bar-foo-project into foo-bar
# Please enter a commit message to explain why this merge is
# necessary, especially if it merges an updated upstream into a
# topic branch.
# Lines starting with '#' will be ignored, and an empty message
# aborts the commit.

If you have not used Vim before, it can be a bit confusing — in order to keep the default message, you can hit the ESC key, and then enter “:wq” when you see the cursor at the bottom of the terminal. This will save and quit Vim, saving the commit message and completing the merge.

Conclusion

The commands in this article are just the basics for using remote repositories with git — but it is enough to get you started and knowing and understanding them will help you and you team as you write software. The documentation linked has all the various options and a more in depth definition of what exactly is happening in the repositories. The more you use git, the less intimidating and the easier it is to use.

Hope this article helps. Happy coding!

--

--

Cam Dziurgot
Geek Culture

Full stack web developer interested in designing cloud based software systems.