Git Tutorial – Part 4: Working with Remote Repositories (Push, Pull, and Fetch)
In this tutorial, you will learn how to:
- Understand local and remote repositories.
- Push local commits to your self-hosted GitLab server.
- Pull the latest changes from GitLab.
- Fetch changes without merging.
- Understand synchronization between local and remote repositories.
- Resolve common push conflicts.
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
- Open your web browser.
- Navigate to your GitLab project.
- Refresh the page.
- 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:
- Downloads the latest changes from the remote repository.
- 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
| Command | Description |
|---|---|
git remote -v | Displays configured remote repositories. |
git push origin main | Uploads commits to GitLab. |
git pull origin main | Downloads and merges changes from GitLab. |
git fetch origin | Downloads changes without merging. |
git log --oneline | Displays a concise commit history. |
Best Practices
- Always run
git pullbefore starting new work. - Commit your work frequently with meaningful commit messages.
- Push your completed work regularly.
- Review incoming changes before making major updates.
- Keep your local repository synchronized with the remote repository.
Summary
In this chapter, you learned how to:
- Understand the relationship between local and remote repositories.
- Push commits to your self-hosted GitLab server.
- Pull the latest changes from GitLab.
- Use
git fetchto download changes without merging. - Resolve common push rejection errors.
- Keep your local repository synchronized with the remote repository.
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:
- Understanding branches
- Creating and switching branches
- Listing branches
- Renaming and deleting branches
- Merging branches
- Branching strategies used in software development
- Working safely without affecting the
mainbranch
Click Here to view the Next tutorial.
Tutorial Index:
- Part 1: Git Installation and Setup Guide
- Part 2: Understanding Git and Connecting to Your Self-Hosted GitLab
- Part 3: Git Workflow – Creating, Staging, and Committing Changes
- Part 4: Working with Remote Repositories (Push, Pull, and Fetch)
- Part 5: Working with Git Branches
- Part 6: Understanding and Resolving Merge Conflicts
