GitHubBeginner to IntermediatePractical Guide
Imagine you are developing a banking application on your laptop. Your teammate has updated the customer validation logic and pushed the changes to the team's GitHub repository. Meanwhile, you have completed a new feature locally.
How do you download your teammate's latest changes? How do you share your own work? And how can you check what has changed remotely without immediately modifying your local branch?
This is where Git remote operations become important. Git provides three commands that developers use regularly: push, pull and fetch.
- git push: Sends your local commits to a remote repository.
- git fetch: Downloads remote updates without integrating them into your current branch.
- git pull: Fetches remote updates and integrates them into your current branch.
- Remote-tracking branches help you understand the state of branches on the remote repository.
1. What Is a Git Remote?
A Git remote is a reference to another repository. It usually points to a repository hosted on a platform such as GitHub, GitLab or Bitbucket.
Your local repository stores your project files, commits and branches. A remote repository provides a shared location where team members can exchange their commits.
For example, a team may use a GitHub repository named banking-loan-service. Each developer clones this repository and works on their own local copy.
2. Understanding Remote Names and URLs
When you clone a repository, Git normally creates a remote named origin. This is simply the default name given to the repository from which you cloned.
Check the configured remote repositories using:
git remote -v
The output displays the remote name and its URLs. Git can use separate URLs for fetching and pushing.
Useful remote commands
# Display configured remotes git remote -v # Display remote details git remote show origin # Add another remote git remote add upstream https://github.com/original/banking-loan-service.git # Change the origin URL git remote set-url origin https://github.com/company/banking-loan-service.git # Rename a remote git remote rename upstream source # Remove a remote reference git remote remove source
3. Git Push — Upload Local Commits
The git push command sends commits from your local repository to a remote repository. This is how you share your completed work with other developers.
Example: Push a feature branch
Suppose you are implementing a loan eligibility feature. You create a branch, make changes and commit them locally.
# Create and switch to a feature branch git switch -c feature/loan-eligibility # Stage the changes git add . # Create a local commit git commit -m "Add loan eligibility validation"
At this point, the commit exists in your local repository. It has not yet been uploaded to GitHub.
Push the branch to the remote repository:
git push -u origin feature/loan-eligibility
What does -u mean?
The -u option is shorthand for --set-upstream. It configures the local branch to track the corresponding remote branch.
After setting the upstream branch, you can normally use the shorter commands:
git push
Git knows which remote branch to use based on the upstream configuration.
Common push variations
# Push the current branch to its upstream git push # Push a specific branch git push origin feature/loan-eligibility # Push and configure upstream tracking git push -u origin feature/loan-eligibility # Push all local branches git push --all origin # Delete a remote branch git push origin --delete feature/loan-eligibility
4. Git Fetch — Download Remote Updates
The git fetch command downloads new commits, branch information and other relevant references from a remote repository. It updates your remote-tracking references but does not automatically merge those changes into your current local branch.
This is useful when you want to inspect your teammates' changes before integrating them.
Example: Fetch the latest changes
git fetch origin
Git has downloaded the latest information and updated the remote-tracking references. Your current local branch has not been merged with these updates.
Inspect fetched changes
# Show local and remote-tracking branches git branch -a # Compare current branch with remote main git diff HEAD origin/main # Show commits available remotely but not locally git log HEAD..origin/main --oneline # Show commits in either side that differ git log --oneline --left-right HEAD...origin/main
The expression origin/main refers to your local remote-tracking reference for the remote's main branch. It is not a live connection to GitHub; it reflects the most recent information fetched from that remote.
Fetch a specific branch
git fetch origin main
This fetches the specified branch from the remote and updates the corresponding remote-tracking reference. It does not automatically switch your current branch or merge the changes.
5. Git Pull — Fetch and Integrate Changes
The git pull command retrieves changes from a remote repository and integrates them into your current local branch.
Conceptually, a pull performs two operations:
- Fetch updates from the remote repository.
- Integrate the fetched updates into the current branch, using the configured pull strategy.
With the default merge strategy, Git performs a fetch followed by a merge.
# Pull from the configured upstream branch git pull # Pull from a specific remote and branch git pull origin main
This illustrative output shows a fast-forward update. Git moved the current branch forward because the remote branch contained commits that followed the current local branch history.
Pull with merge or rebase
Git supports different ways to integrate fetched changes.
# Fetch and merge git pull --no-rebase # Fetch and rebase local commits git pull --rebase # Fetch without integrating git fetch origin
With merge, Git combines the histories and may create a merge commit. With rebase, Git replays your local commits on top of the updated upstream branch.
6. Push vs Pull vs Fetch — Key Differences
| Command | Direction | What it does | Integrates into current branch? |
|---|---|---|---|
| push | Local → Remote | Uploads local commits. | No |
| fetch | Remote → Local | Downloads remote updates and updates remote-tracking references. | No |
| pull | Remote → Local | Fetches and integrates remote changes. | Yes |
7. Visualising the Push, Fetch and Pull Workflow
Consider a local branch and a remote branch that are initially in sync. The developer creates a commit locally, while a teammate publishes another commit remotely.
8. Remote-Tracking Branches Explained
When you fetch from origin, Git updates references such as origin/main and origin/feature/loan-eligibility.
These are called remote-tracking branches. They represent the last fetched state of branches on the remote repository.
# Local branches git branch # Remote-tracking branches git branch -r # Both local and remote-tracking branches git branch -a
For example, you might see:
The local main branch and origin/main are separate references. Fetching updates the remote-tracking reference; it does not automatically move your local branch.
9. Handling Common Remote Operation Problems
Problem 1: Push rejected because the remote contains new commits
This commonly happens when another developer pushes commits to the same branch before you push your own work. Your local branch is now behind or has diverged from the remote branch.
# Fetch and integrate remote changes git pull --rebase # Resolve conflicts if Git reports any # Then continue the rebase when required git rebase --continue # Push your updated branch git push
If conflicts occur, edit the affected files, stage the resolved files using git add, and continue the rebase. If the rebase becomes confusing, you can cancel it with git rebase --abort.
Problem 2: Authentication failed
GitHub does not accept your account password for Git operations over HTTPS. Depending on your setup, use a personal access token through a secure credential manager or configure SSH authentication.
git remote -v
Problem 3: No upstream branch configured
If Git does not know which remote branch your local branch should track, configure it during the first push:
git push -u origin feature/loan-eligibility
Problem 4: Merge conflicts during pull
A conflict can occur when local and remote changes affect the same parts of a file and Git cannot automatically combine them.
Open the conflicting files, review both changes, edit the file to the intended final version, then stage the resolution. The remaining steps depend on whether you are merging or rebasing.
# Check which files have conflicts git status # After editing and resolving the files git add LoanService.cs # Complete the merge git commit
When resolving a rebase conflict instead, use git rebase --continue rather than creating a merge commit.
10. Recommended Daily Workflow
A disciplined remote workflow helps avoid unnecessary conflicts and keeps your branch updated.
- Check your current branch and working tree.
- Fetch the latest remote information.
- Review the changes available on the remote.
- Integrate the relevant updates into your feature branch.
- Make and test your changes locally.
- Commit meaningful changes.
- Push your branch and open a pull request.
# Check the current state git status # Download remote updates git fetch origin # Switch to your feature branch git switch feature/loan-eligibility # Integrate the latest main branch git rebase origin/main # Stage and commit your changes git add . git commit -m "Improve loan eligibility validation" # Publish your branch git push
11. Best Practices
- Fetch regularly to stay aware of changes made by teammates.
- Review remote changes before integrating them when working on sensitive features.
- Use meaningful branch names and commit messages.
- Push feature branches regularly so work is shared and backed up remotely.
- Never force-push shared branches without team agreement.
- Use pull requests for review instead of directly pushing unfinished work to protected branches.
- Follow a consistent merge or rebase policy across the team.
- Keep credentials and access tokens out of source code.
12. Hands-On Practice
Practise remote operations using a test repository. Do not experiment on a production repository.
- Create a repository on GitHub and clone it locally.
- Create a branch named feature/customer-audit.
- Add a file named audit-notes.md and commit it.
- Push the branch using git push -u origin feature/customer-audit.
- Make a separate change on GitHub, such as updating the README.
- Run git fetch origin and inspect the remote updates.
- Integrate the relevant changes using your chosen team strategy.
- Push your latest work and verify it on GitHub.
Learning outcome: You will understand how local and remote branches exchange commits and how fetch differs from pull.
13. Interview Questions and Answers
1. What is the difference between git push and git pull?
Answer: Push uploads local commits to a remote repository. Pull downloads remote changes and integrates them into the current local branch.
2. Does git fetch modify your current working files?
Answer: Fetch updates remote-tracking references and downloads objects. It does not automatically merge those changes into your current branch or modify its working files.
3. What is the purpose of the -u option in git push?
Answer: It sets the upstream tracking branch. Future push and pull commands can use that tracking relationship without requiring the remote and branch names each time.
4. What is origin/main?
Answer: It is a remote-tracking reference representing the main branch of the remote named origin as of the latest fetch. It is not the same as your local main branch.
5. What is the difference between git pull and git pull --rebase?
Answer: A pull using the merge strategy integrates fetched changes through a merge. A pull with --rebase replays local commits on top of the updated upstream branch. The resulting history can differ, so teams should agree on a workflow.
14. Conclusion
Git remote operations are essential for everyday team development. Push helps you publish your work, fetch lets you inspect remote updates safely, and pull helps you integrate those updates into your current branch.
Understanding the difference between these commands, remote-tracking branches and upstream configuration will help you collaborate confidently and avoid common repository problems.
Practise these commands with a test repository until the workflow becomes part of your daily development routine.