Mastering Git: Learn What Git Fetch & Git Pull Commands Actually Do!
This video provides a detailed explanation of common Git commands and concepts such as branch creation, cloning repositories, fetching updates, pulling changes, and pushing commits. It focuses on practical usage to manage local and remote branches efficiently, ensuring synchronization between local workspaces and remote GitHub repositories.
1. Git Branch Creation and Remote Branch Tracking
Branching is a fundamental part of Git workflow, allowing multiple parallel developments.
- Creating a new branch locally does not automatically create it on the remote repository.
- When cloning a repository, by default only the default branch (usually
main) is checked out locally. - Other remote branches exist as references but their code/files are not downloaded until checked out.
- Using
git branch -rlists all remote branches known to the system. - Remote branch references are pointers to branches in the remote repository; these do not imply that branches or code exist locally yet.
- To start working on a remote branch locally, checking it out (
git checkout branch-nameorgit switch branch-name) will:- create the branch locally,
- download its code,
- set up tracking with the remote branch.
2. Git Clone and Remote Branch Visibility
Cloning a Git repository copies the full repository but fetches only the default branch's files locally.
- Example: Cloning a repository only downloads the
mainbranch files and sets the local HEAD tomain. - Other remote branches (such as
elastic-test) show up only in remote references. - To work with these remote branches, a developer must explicitly check out the branch after cloning, which downloads the branch data locally.
3. Git Fetch: Synchronizing Branch Information
git fetch synchronizes the local repository with the remote repository's metadata without merging or updating local branches blindly.
- Fetching updates the remote tracking branches (
origin/branch-name) to reflect the latest state of the remote repository. - No local branches are modified when fetching; it only updates the information about remote branches.
- This means new branches appearing remotely after your last fetch will now be visible locally as remote references.
- You can fetch a specific branch (
git fetch origin elastic-test) or fetch all branches (git fetch origin). - Fetch does not bring code changes into your working directory or current branch; it only updates pointers.
4. Git Pull: Fetch Plus Merge (or Rebase)
git pull performs the combined action of fetching updates from the remote and then merging (or rebasing) those into the currently checked out local branch.
- It updates the local branch with the latest commits from the remote, modifying files as necessary.
- If the local branch is behind the remote, pulling applies remote commits.
- Possible merge conflicts can arise if local and remote changes conflict.
- Pull defaults to merging but can be instructed to rebase instead (e.g.,
git pull --rebase). - Pull requires you to be on the branch you want to update.
- Usage example:
git pull origin elastic-testpulls updates for the current checkout of theelastic-testbranch from remote.
5. Git Push: Uploading Local Changes to Remote
After committing changes locally, you upload them to the remote repository using git push.
- Push updates the remote branch to match your local branch’s history.
- It requires authentication (username/password or token).
- Command syntax:
git push origin branch-nameto push a particular branch, or simplygit pushif tracking is set. - A branch can be ahead of the remote branch, indicating commits need to be pushed.
- Pushing synchronizes your work with collaborators via the remote repository.
6. Branch State Indicators and Commands Overview
The video clarifies common Git state messages and commands related to branch synchronization:
Common commands and their brief meanings:
7. Practical Workflow Recommendations
- After cloning, always use
git fetchto sync all remote branch info. - Use
git checkoutorgit switchto start working on any remote branch. - Frequently use
git fetchto update remote branch info without disrupting local work. - Use
git pullto incorporate remote updates into your current working branch. - Commit local changes and push them to remote with
git push. - Keep track of whether your branch is ahead or behind remote to avoid conflicts or stale code.
- Multi-remote and advanced flags exist but basic commands suffice for most workflows.
8. Additional Notes and Concepts
- Remote tracking branches (
origin/branch-name) represent remote branches locally, but they do not contain code until checked out. - Checking out a remote branch automatically creates a local branch that tracks the remote one.
- Detached HEAD state means HEAD points directly at a commit, useful for inspecting specific commit states.
- Pull performs fetch + merge (or rebase) and can generate merge conflicts that need resolution.
- Fetch only updates references; it never modifies local files or branches directly.
- Push requires authentication and pushes committed changes to the remote.
This explanation demystifies several essential Git operations, focusing on managing branches and syncing local and remote repositories. It highlights the distinction between fetching branch references versus pulling changes into the working directory and clarifies how branches transition from remote references to active local branches for development. Understanding this workflow is critical to collaborating effectively using Git and GitHub.