Version management with Git, GitHub and Co

Git Remote, Push and Pull – Synchronizing Repositories

All videos of the tutorial Version management with Git, GitHub and Co
Jan Brinkmann
By Jan Brinkmann Last updated on

Imagine your code exists only on your laptop. If the hard drive crashes or you want to keep working on a different computer, your project suddenly comes to a standstill. This is exactly where remote servers come into play. Services like GitHub store your Git repositories online, back them up, and make them accessible to others.

Version management with Git, GitHub and Co - 13 repositories on remote servers (clone, remote, push, pull)

The video is only loaded from YouTube when you play it – data (including your IP address) is then transmitted to YouTube/Google. More in the privacy policy

In this tutorial, you will learn how to connect your local project to a remote repository, upload and download changes, and pull someone else's project onto your computer. For this you need an existing local Git repository, a GitHub account, and a terminal. The commands work the same on macOS, Linux, and Windows (e.g., in Git Bash).

Key Takeaways

  • A remote server is added with git remote add. It is usually named origin.
  • git push uploads your commits to the server, git pull fetches changes from there to your computer.
  • With git push -u origin master you set an upstream. After that, git push and git pull are enough without any further arguments.
  • git clone creates a complete copy of someone else's repository including the entire history.
  • Merges occur when there are changes on both sides. To get started, it is enough to confirm them.

Step-by-Step Guide

Step 1: Copy the Repository URL

First, create a new repository on GitHub. On the repository's home page you will find a green button labeled "Code". Click it and copy the URL shown. It points to the address where your project will be reachable later.

Git Remote, Push and Pull – Synchronizing Repositories

Step 2: Add the Remote Server in the Terminal

In your terminal, switch to the folder of your local project. Before you add anything, check whether a remote already exists. You can do this with git remote. If there is no output, no server has been configured yet.

Then you add the server with git remote add origin and the copied URL. The name origin is a convention. It serves as a short name for the server, so you don't have to write it out every time.

Git Remote, Push and Pull – Synchronizing Repositories

Step 3: Check the Configured Connections

With git remote -v you see all configured servers along with their URLs. For origin, two lines appear, one for fetch and one for push. They show that Git stores separate information for downloading and uploading. For everyday use you don't need to understand this separation in detail, but it helps you spot configuration errors.

Step 4: Fetch the Initial Changes with git pull

If the server already contains content, for example an automatically generated README file, you should download it first. A simple git pull does not work reliably at this point, because Git doesn't know which branch on the server belongs to your local branch. You therefore specify the server and the branch directly:

git pull origin master

Git then downloads the changes. In older repositories the default branch is called master, in newer ones often main. Make sure to use the name that actually exists in your case.

Step 5: Confirm a Merge Commit

If both sides have different commits, Git creates a so-called merge when downloading. In this process, the changes from GitHub and your local changes are combined. In the terminal, an editor with a commit message usually opens. You can accept it and save and exit with :wq (in vi).

To get started, this confirmation is enough. Complex merge situations, where the same place in a file was changed on both sides, are a topic of their own that you can explore in more depth later.

Step 6: Set the Upstream and Upload

Now you upload your commits to the server. The command for this is:

git push -u origin master

The -u option sets the upstream. This tells Git that your local branch corresponds to the master branch on origin. From this point on, git push and git pull are enough without any further arguments. Git transfers the commits, and your current state appears on GitHub.

Git Remote, Push and Pull – Synchronizing Repositories

Step 7: Check the result in the browser

Reload the repository page on GitHub. Your files, such as the .gitignore file, should now be visible. Open the file to see that it has exactly the content you created locally.

Git Remote, Push and Pull – Synchronizing Repositories

Step 8: View the history of a file

On GitHub you see not only the current state but also the history. If you click on a file and then on the commit display, you can see all the changes that were ever made to that file, all the way back to the first commit. Each entry shows the message, the time, and the author. This is especially helpful when you want to understand why a line was changed.

Git Remote, Push and Pull – Synchronizing Repositories
Git Remote, Push and Pull – Synchronizing Repositories

Step 9: Download someone else's repository with git clone

Imagine another developer wants to support you and sends you the URL of their project. Switch to a target directory, for example /tmp, and run:

git clone URL

Git then automatically creates a folder with the project name and downloads the complete repository including its history. You don't have to specify the folder name yourself.

Git Remote, Push and Pull – Synchronizing Repositories

Step 10: Check the contents

Use cd to switch into the new folder and use ls to display the files. You can see hidden files like .gitignore with ls -la. The -a option shows all entries that begin with a dot.

Git Remote, Push and Pull – Synchronizing Repositories

Step 11: Commit a change and upload it

Now take on the role of a second developer. You edit the README file and add an explanation of the project. Then you save the change with a commit:

git commit -m "Erklärung" README.md

Then you upload it with git push. On GitHub the new description is visible shortly afterwards. There, Markdown, the markup language of the README file, is automatically converted into formatted text.

Git Remote, Push and Pull – Synchronizing Repositories

Step 12: Fetch the changes in the original project

Switch back to your original project directory. With git log you see the history so far. Then you fetch the other developer's new commits with git pull. Afterwards, the README file shows the change, and both states are in sync again.

Git Remote, Push and Pull – Synchronizing Repositories

With that, you have gone through the most important synchronization workflows. Further topics such as branches, parallel development on multiple branches, and resolving real conflicts are best built up gradually.

Summary – Synchronizing Git repositories with remote, push, pull, and clone

You connect your local project to a server like GitHub with git remote add origin URL. With git pull you download changes, and with git push you upload your commits. The command git push -u origin master sets the upstream, so that short commands are sufficient in the future. With git clone you copy other people's projects completely to your computer. Anyone who masters these basics can back up work, share it in a team, and return to earlier versions at any time.

FAQ

Do I absolutely need GitHub for Git?No, Git also works without GitHub. You can just as well use GitLab, Bitbucket, or your own server as a remote.

What happens if I run git pull without any arguments and no upstream exists?Git displays a notice and does not fully execute the command. Then specify the server and the branch, for example with git pull origin master, or set the upstream with git push -u.

What is a merge commit for?A merge commit combines changes from two sides. It occurs when new commits exist both on the server and locally.

Is a repository on GitHub also a backup?Yes, it is a useful additional backup. In addition, you should regularly check whether all important changes have actually been uploaded.

Can I also view the old history with git clone?Yes, git clone copies the complete history. You can then browse through it with git log.

Jan Brinkmann
Trainer About Jan Brinkmann Web development with passion! Jan has been a developer for over 15 years. All courses by Jan Brinkmann