Git Push vs Pull: What's the Difference?
Table of Contents
git push sends your local commits to a remote repository. git pull brings remote commits into your current local branch. They move Git history in opposite directions, but pull also integrates what it downloads, which is where the comparison gets interesting.
All the content from our Boot.dev courses are available for free here on the blog. This one comes from the "GitHub" chapter of Learn Git. If you want to try the far more immersive version of the course, do check it out!
Git Push vs Pull
| Command | Direction | What it changes |
|---|---|---|
git push |
Local to remote | Updates a branch on the remote repository |
git pull |
Remote to local | Fetches remote commits, then merges or rebases them into your current branch |
Both commands work with a Git remote, usually one named origin.
What Does git push Do?
The git push command pushes (sends) local changes to any remote. To push your local main branch's commits to the remote origin's main branch, run:
git push origin main
You need to be authenticated with the remote to push changes. You can also push a local branch to a remote with a different name:
git push origin local_branch:remote_branch
It's less common to do this, but nice to know. If this is a branch's first push, git push -u can save its upstream so later commands can omit origin and the branch name.
To delete a remote branch, use the --delete flag:
git push origin --delete remote_branch
What Does git pull Do?
Fetching is nice, but most of the time we want the actual file changes from a remote repo, not just the metadata. Run:
git pull origin main
git pull fetches updates from origin/main and then integrates them into your current branch. The integration can be a merge or a rebase, depending on your arguments and configuration.
If your current branch has an upstream, the remote and branch are optional:
git pull
Pull Is Fetch Plus Integration
git fetch downloads commits and updates remote-tracking refs, but it doesn't merge anything into your current branch. That's why it's safe to run often. You get new information without touching your working files.
git pull follows that fetch with a merge or rebase. If you want explicit control when things get weird, use two commands:
git fetch origin
git merge origin/main
You can use git rebase instead of merge if you prefer a linear history.
If you prefer rebase (as I do), then set this config globally:
git config --global pull.rebase true
All your future pulls will integrate changes with a rebase operation by default.
How Do You Merge a Remote Branch Into Local main?
Once you've fetched:
git switch main
git merge origin/main
If your local main has no extra commits, this is often a fast-forward merge.
That means Git just moves your local branch pointer forward, no extra merge commit needed.
After that, plain git log on local main should match the history you saw in origin/main.
A Practical Push and Pull Workflow
When I'm working by myself, I usually stick to a single branch, main:
- Make changes to files.
git add .(orgit add <files>if I only want to add specific files).git commit -m "a message describing the changes".git push origin main.
On a team, pull the latest accepted work before starting a feature branch, then push that branch and open a pull request. Git and GitHub covers that workflow in detail.
Frequently Asked Questions
What is the difference between git push and git pull?
git push sends local commits to a remote branch. git pull fetches remote commits and integrates them into your current local branch.
Should I git pull before git push?
Pull first when the remote branch may contain commits you do not have locally. Integrate those changes before pushing your own.
Does git pull overwrite local changes?
Not normally. Git integrates remote commits with your local branch, but uncommitted or conflicting changes can prevent the pull or require conflict resolution.
Is git pull the same as git fetch?
No. git fetch only downloads commits and updates remote-tracking refs. git pull fetches and then merges or rebases into the current branch.
