Fetching, Pulling, and Pushing Changes
git fetch, git pull, git push, remote tracking branches, origin/main, upstream tracking
Fetching, Pulling, and Pushing Changes
When working with remote repositories, three commands manage synchronization: git fetch, git pull, and git push. Understanding the difference between them is essential for avoiding conflicts.
git fetch
git fetch origin downloads all new commits and branches from the remote but does not modify your local branches. It updates remote tracking branches like origin/main. Use fetch to see what others have done without affecting your work:
git fetch origin
git log origin/main --onelinegit pull
git pull is essentially git fetch followed by git merge. It downloads remote changes and immediately merges them into your current branch. If you prefer rebase-style integration, use:
git pull --rebase origin maingit push
Push your local commits to the remote:
git push origin mainIf someone else pushed while you were working, your push will be rejected. You must first pull their changes, resolve any conflicts, and then push again.
Remote Tracking Branches
Remote tracking branches like origin/main are local read-only references to the state of remote branches at last fetch. They help you compare your work with the remote without being online.
