Git - Working With Remote Repositories

Git - Working With Remote Repositories

How to pull and push the code to the remote repositories.

Hey guys, It's been a while since my last article. I have been kind of busy,🙄 but no worries I am back with one more super simple article from the series Basics of Git, If you are new to my blogs I do recommend reading those.

If you are a consistent reader, you must know on the last article from this series we discuss Git commands to get you started with programming.

In this article, we will be working with the remote repositories and will be learning how to manage the code across the different remote repositories. Till now we learned the Initialization of the git repository and adding the remote repository to that. Then we discussed the staging and committing the changes to the local repository.

Now, the next step is to push our changes to the remote repository so our colleagues/Team Members/Friends can pull those changes to their local repository.

Note - Before pushing your local changes to the remote repository always take the remote changes to your local branch and merge them (if any).

As best practice suggests before pushing our changes to the remote repository, we will first pull the changes to our local repo to avoid code loss (git will prompt you before that so do not worry). There are two ways to pull the code changes from the remote repo to our local repo,

  • git fetch
  • git pull

git fetch

Git fetch command is used to fetch the remote changes from one or more remote repositories or tags, to complete their history.

The syntax to fetch the remote changes or references

$ git fetch <remote>

This command will fetch all the latest files, refs, and the commits that are present in the remote for all the branches, if you want to target a specific branch and fetch only changes related to that branch then you can simply write

$ git fetch <remote>  <branch>

If you do not specify the remote to the git fetch, it will by default it will take the remote origin set in the config.

git pull

The git pull works exactly like the git fetch it first pulls down all the changes from the repositories to the local repo, but the only difference is after pulling the changes it merges all remote changes to the local commits.

You can git pull by writing the following command

$ git pull <remote> <branch>

The above command will be executed in two steps first, a git fetch <remote> <branch> followed by git merge <branch> This will fetch all the changes in the remote repo for a given branch and will merge that into the local commit.

After fetching/Pulling the remote changes you would love to push your local changes to the remote repositories you can do that using the git push command.

git push

The git push command pushes all the local refs to the remote refs, it will basically push all of your local changes to the remote references. The syntax is pretty simple,

git push <remote> <branch>

If you only run git push without specifying remote, it will by default take the local branch's remote/origin and push to it. You can also specify the remote(origin) and the specific branch that you want to push.

This way you can push the local refs to the remote and other people can pull those changes.

This way you can interact with your remote repositories. That is it for this article, Thank you for reading. To get notified about my next article do follow me, till then You can connect me on Twitter or DM on Discord.

Did you find this article valuable?

Support Adarsh Thakur by becoming a sponsor. Any amount is appreciated!