Site icon Student Projects Live

Working with Remote Repositories (Push, Pull, and Fetch)

Understanding and Resolving Merge Conflicts in GIT

Git Tutorial – Part 4: Working with Remote Repositories (Push, Pull, and Fetch)

In this tutorial, you will learn how to:


Understanding Local and Remote Repositories

When you create a commit, it is stored only on your local computer.

To share your changes with your team, you must push the commits to the remote repository hosted on your organization’s GitLab server.

Working Directory
        │
        ▼
Staging Area
        │
        ▼
Local Repository
        │
 git push
        ▼
Remote Repository (GitLab)

Similarly, when other developers push their changes, you must pull them to update your local repository.


Step 1: Check the Current Status

Before pushing changes, verify your repository status.

git status

Example output:

On branch main

nothing to commit, working tree clean

Step 2: View the Configured Remote Repository

To see the remote repository connected to your local repository:

git remote -v

Example output:

origin  https://gitlab.yourcompany.com/training/gittutorial.git (fetch)
origin  https://gitlab.yourcompany.com/training/gittutorial.git (push)

What is origin?

origin is the default name Git assigns to the remote repository when you clone a project.


Step 3: Push Your Changes

Push your local commits to GitLab.

git push origin main

Example output:

Enumerating objects: 5

Counting objects: 100%

Writing objects: 100%

To https://gitlab.yourcompany.com/training/gittutorial.git

abc1234..def5678 main -> main

Your commits are now available on the GitLab server.


Step 4: Verify the Changes in GitLab

  1. Open your web browser.
  2. Navigate to your GitLab project.
  3. Refresh the page.
  4. Verify that:
    • The new files are visible.
    • The latest commit appears in the commit history.
    • The repository has been updated.

Step 5: Simulate Another Developer’s Change

Suppose another developer updates the repository and pushes new commits.

Your local repository is now behind the remote repository.

Run:

git status

You may see:

Your branch is behind 'origin/main' by 1 commit.

Step 6: Pull the Latest Changes

Download and merge the latest changes from GitLab.

git pull origin main

Example output:

Updating abc1234..def5678

Fast-forward

README.md

1 file changed

Your local repository is now synchronized with the remote repository.


Understanding git pull

git pull performs two operations:

  1. Downloads the latest changes from the remote repository.
  2. Automatically merges those changes into your current branch.

In simple terms:

git pull
=
git fetch
+
git merge

Step 7: Fetch Without Merging

Sometimes you want to download updates without immediately merging them.

Use:

git fetch origin

This downloads new commits but does not change your working files.

To view the downloaded commits:

git log origin/main --oneline

You can compare your branch with the remote branch before deciding to merge.


Step 8: Understanding Synchronization

The following diagram illustrates how synchronization works:

Developer A
      │
      ▼
Git Push
      │
      ▼
GitLab Server
      ▲
      │
Git Pull
      │
      ▼
Developer B

All team members synchronize through the GitLab server.


Step 9: Common Push Error

Suppose another developer pushes changes before you.

When you attempt to push, Git may display:

! [rejected] main -> main (non-fast-forward)

error: failed to push some refs

This happens because your local repository is not up to date.


Step 10: Resolve the Push Conflict

First, download the latest changes:

git pull origin main

If Git merges successfully:

git push origin main

If there are merge conflicts, Git will ask you to resolve them before pushing.

Merge conflicts are covered in a later chapter.


Step 11: Verify the Commit History

Display the commit history:

git log --oneline

Example:

7d82ab3 Update README

3d5f872 Add notes

1c8a452 Initial commit

This confirms that your repository contains the latest commits.


Frequently Used Remote Commands

CommandDescription
git remote -vDisplays configured remote repositories.
git push origin mainUploads commits to GitLab.
git pull origin mainDownloads and merges changes from GitLab.
git fetch originDownloads changes without merging.
git log --onelineDisplays a concise commit history.

Best Practices


Summary

In this chapter, you learned how to:

You can now confidently exchange changes between your Windows 11 computer and your organization’s GitLab server.


Next Tutorial

The next chapter will cover one of the most important Git concepts:

Click Here to view the Next tutorial.


Tutorial Index:

Exit mobile version