Git-Distributed Version Control System part-4
Git-Distributed Version Control System part-4
Continue learning Git-Distributed Version Control System part-4 in this advanced guide. Explore branching, merging, and squashing with real-world Git workflow examples.
Git – git sync with Github (pull,fetch,merge,push)
Git Sync with GitHub
To sync your local repository with GitHub, you use commands that help you:
Download changes from GitHub → your local machine
Upload your local changes → GitHub
The key commands are:
Command | Direction | Purpose |
---|---|---|
git fetch | GitHub → Local | Gets changes from remote, does not merge |
git pull | GitHub → Local | Gets changes and merges them into your branch |
git merge | Local only | Merges one branch into another |
git push | Local → GitHub | Sends your local commits to GitHub |

1. git fetch
git fetch origin
Downloads the latest changes from the remote (GitHub).
Does not merge them automatically.
Useful when you want to review changes before merging.
2. git pull
git pull origin main
Combines
fetch
+merge
.Fetches changes from GitHub and automatically merges into your current local branch.
If conflicts occur, Git will ask you to resolve them manually.
3. git merge
git merge origin/main
Used to manually merge changes from one branch into another.
Useful after
git fetch
when you want full control of when to merge.
4. git push
git push origin main
Uploads your local commits to GitHub.
Others will then see your updates on the GitHub repository.
Typical Sync Workflow:
Check your status:
bashgit status
Fetch latest changes from GitHub:
bashgit fetch origin
Merge those changes into your branch:
bashgit merge origin/main
Or just pull directly:
bashgit pull origin main
Make your changes, stage and commit:
bashgit add .
git commit -m "Your message"Push your changes to GitHub:
bashgit push origin main
Summary Chart
Action | Command |
---|---|
Download changes only | git fetch origin |
Download & merge changes | git pull origin branchname |
Combine branches locally | git merge branchname |
Upload changes to GitHub | git push origin branchname |
Git – Branches
What are Git Branches?
A branch in Git is like a separate timeline of your code where you can work on new features or fixes without affecting the main codebase (usually called the main
or master
branch).
Think of it like creating a copy of your code to experiment safely.
Why Use Branches?
Work on new features without breaking the main code.
Fix bugs separately.
Work in teams, with each developer on a different branch.
Test or experiment without risk.
Default Branch:
When you create a new Git repository, a default branch is created called:
main
(in newer Git versions)master
(in older versions)
Common Git Branch Commands
Action | Command | Description |
---|---|---|
View all branches | git branch | Lists all local branches |
Create a new branch | git branch branch-name | Creates a new branch |
Switch to another branch | git checkout branch-name | Switches to the branch |
Create and switch at once | git checkout -b branch-name | Creates and switches to the branch |
Merge a branch into current | git merge branch-name | Merges that branch into your current branch |
Delete a branch (after merging) | git branch -d branch-name | Deletes a local branch |
Push a branch to GitHub | git push origin branch-name | Sends the branch to remote (GitHub) |
Example Workflow:
You’re on the
main
branch.You create a new branch for a feature:
bashgit checkout -b feature-login
Do your work and commit changes.
Switch back to
main
:bashgit checkout main
Merge your feature branch:
bashgit merge feature-login
Delete the feature branch (optional):
bashgit branch -d feature-login
Visual Example:
main
└───●───●───●────────────▶
\
feature-login
└──●───●──▶
Remote Branches (GitHub):
After creating a local branch, you can push it to GitHub:
git push origin branch-name
To get branches from GitHub:
git fetch
git checkout branch-name
Summary:
Branches help you work in isolation.
Merging combines code from different branches.
Common branch types:
main
,feature/*
,bugfix/*
,hotfix/*
Git – Remote Branches
What are Remote Branches in Git?
A remote branch is a branch stored on a remote repository, like GitHub, GitLab, or Bitbucket.
It reflects the state of a branch on that remote server – often used to collaborate with others.
Key Concepts:
Term | Meaning |
---|---|
origin | The default name of the remote (usually GitHub) |
main or master | The main branch |
origin/main | Remote branch on GitHub |
main | Your local branch |
Difference Between Local and Remote Branches:
Local Branch | Remote Branch |
---|---|
Exists on your computer | Exists on GitHub (or another remote) |
You can edit directly | You must push changes to update it |
You create it with git branch | Created with git push or when cloned |
Common Git Remote Branch Commands
Action | Command | Description |
---|---|---|
View all branches (local + remote) | git branch -a | Shows all branches |
View only remote branches | git branch -r | Shows only remote branches |
Push a local branch to remote | git push origin branch-name | Sends branch to GitHub |
Track a remote branch | git checkout --track origin/branch-name | Creates a local branch linked to remote |
Fetch remote branches | git fetch | Downloads new branches/updates |
Pull changes from remote | git pull origin branch-name | Updates your local branch |
Delete remote branch | git push origin --delete branch-name | Deletes branch from GitHub |
Example Workflow:
Step 1: Create a local branch
git checkout -b feature-login
Step 2: Push it to GitHub
git push origin feature-login
Now GitHub will have a remote branch called origin/feature-login
.
Collaboration Tip:
If someone else created a branch and pushed it, you can see and use it like this:
git fetch
git checkout branch-name
or
git checkout -b branch-name origin/branch-name
Deleting Remote Branch:
If a feature is done and merged, you might want to remove the remote branch:
git push origin --delete feature-login
Summary:
Task | Command |
---|---|
See remote branches | git branch -r |
Push a branch to GitHub | git push origin branch-name |
Get new branches | git fetch |
Use a remote branch | git checkout branch-name |
Delete a remote branch | git push origin --delete branch-name |
For More Learning Click Here
Learn the DevOps to start click here
Git-Distributed Version Control System part-3
Git-Distributed Version Control System part-5
Next