Sharing and Updating Projects
git fetch
Description
It is used to download commits, files, and references from a remote repository into our local repository. It updates our local copy of the remote branch but does not merge the changes into working branch. This allows to see what others have been working on without affecting your current work.
Usage
git fetch [<options>] [<repository>] [<refspec>...]
Options
--all
: Fetches updates from all remotes.
--prune
: Removes remote-tracking branches that no longer exist on the remote.
--dry-run
: Shows what would be fetched without actually fetching.
-v
or --verbose
: Provides more detailed output.
--depth=<depth>
: Limits the fetching to a specified number of commits.
git fetch --all
git fetch --prune
git fetch --dry-run
git fetch --verbose
git fetch --depth=1
What It Does
Updates Remote Tracking Branches: Fetches updates from a remote repository and updates the remote-tracking branches in our local repository.
Does Not Merge: It does not change our working directory or current branch. We need to manually merge or rebase the changes if you want to integrate them.
Common Use Cases
-- Fetch Updates from All Remote Repositories
-- Fetches updates from all remote repositories configured in our local repository.
git fetch
-- Fetch Updates from a Specific Remote Repository, in this case, origin
git fetch origin
-- Fetch Specific Branches
-- Fetches updates for the specified branch from the remote repository
git fetch origin branch-name
Example Workflow
-- Check the Current Branch and Status
git status
-- Fetch Updates from the Remote Repository
git fetch origin
-- View the Differences
-- This shows the commits in the origin/main branch
git log origin/main --oneline
-- Merge or Rebase the Changes
-- If we want to incorporate the fetched changes into our local branch, we can merge or rebase them
git merge origin/main
git rebase origin/main
Example Output
When running git fetch

Fetch vs. Pull
Fetch: Downloads changes from the remote repository and updates remote-tracking branches. Does not change our working directory or current branch.
git fetch
Pull: Combines
git fetch
andgit merge
(orgit rebase
). Downloads changes and immediately merges or rebases them into our current branch.
git pull

git pull
Description
It is used to fetch changes from a remote repository and immediately integrate them into your current branch. This command is a combination of git fetch
(which downloads the changes) and git merge
(which integrates the changes). By default, git pull
performs a merge, but it can also be configured to rebase.
Usage
git pull [<options>] [<repository> [<refspec>...]]
What It Does
Fetches Changes: Downloads commits, files, and references from a remote repository.
Integrates Changes: Merges or rebases the fetched changes into the current branch.
Common Use Cases
-- Pull Changes from the Default Remote Repository (origin)
-- This fetches and merges changes from the default remote repository and branch, typically origin/main
git pull
-- Fetches and merges changes from the specified remote repository origin
git pull origin
-- Pull Changes and Rebase
-- Fetches changes and then rebases our local commits on top of the fetched commits instead of merging them
git pull --rebase
Example Workflow
-- Check the Current Branch and Status
git status
-- Pull Changes from the Remote Repository
-- This fetches and merges changes from the main branch of the origin remote repository into our current branch
git pull origin main
-- Resolve Any Conflicts:
-- If conflicts arise during the merge, Git will pause and prompt to resolve them manually. After resolving conflicts, we can continue the merge process by committing the changes.
Example Output
When running git pull

git push
Description
It is used to upload local repository content to a remote repository. By executing this command, we can share our local commits, branches, and tags with others. Essentially, git push
updates the remote repository with our latest work.
Usage
git push [<options>] [<repository> [<refspec>...]]
Options
-all
: Pushes all branches
--tags
: Pushes all tags.
--force
or -f
: Forces the push even if it results in a non-fast-forward merge, which can overwrite remote changes.
--set-upstream
or -u
: Sets the remote branch as the upstream for the current branch, making future pushes easier.
--delete
: Deletes a branch or tag from the remote repository.
git push --all origin
git push origin --tags
git push --force origin branch-name
git push --set-upstream origin branch-name
git push origin --delete branch-name
What It Does
Uploads Local Changes: Pushes commits from your local branch to a corresponding branch on the remote repository.
Creates or Updates Remote Branches: If the remote branch doesn't exist, it will be created. If it exists, it will be updated with your local commits.
Upstream Branch: Setting an upstream branch allows to use git push
without specifying the remote and branch names every time. We can set the upstream branch with:
git push --set-upstream origin branch-name
Force Push: Be careful when using git push --force
as it can overwrite changes on the remote repository, potentially causing data loss for others working on the same repository. Use it only when necessary and ensure you understand the implications.
git push --force origin branch-name
Deleting Remote Branches: To delete a branch from the remote repository, We can use:
git push origin --delete branch-name
Common Use Cases
-- Push Changes to the Default Remote Repository (origin)
-- Pushes the current branch to its upstream branch on the origin remote repository.
git push
-- Push to a Specific Remote Repository
-- Pushes the current branch to the origin remote repository
git push origin
-- Push a Specific Branch
-- Pushes the specified branch to the origin remote repository
git push origin branch-name
-- Pushes all local branches to the remote repository.
git push --all
-- Push Tags
-- Pushes all local tags to the remote repository
git push origin --tags
Example Workflow
-- Make Changes and Commit Locally
echo "Some changes" > file.txt
git add file.txt
git commit -m "Add some changes"
-- Push Changes to the Remote Repository
-- This pushes the main branch to the origin remote repository
git push origin main
Example Output
When running git push

Last updated
Was this helpful?