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
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.
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
Example Workflow
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
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
Example Workflow
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
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.
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:
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.
Deleting Remote Branches: To delete a branch from the remote repository, We can use:
Common Use Cases
Example Workflow
Example Output
When running git push
Last updated
Was this helpful?