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.
Remote-Tracking Branches: These are branches in your local repository that track the state of branches in the remote repository. For example, origin/main
tracks the main
branch in the origin
remote.
Safe Operation: git fetch
is safe to run at any time as it does not modify your working directory or current branch. It simply updates your remote-tracking branches with any changes from the remote repository.
Comparing Branches: After fetching, you can use git diff
or git log
to see what changes have been made on the remote branches before deciding to merge or rebase.
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.
Fast-forward vs. Merge Commit: If your branch has not diverged from the remote branch, git pull
performs a fast-forward merge. If there are divergent commits, it creates a merge commit.
Conflicts: If there are conflicts during the merge or rebase, Git will stop and prompt you to resolve them manually.
Rebase Option: Using git pull --rebase
helps to maintain a cleaner, linear history by applying your changes on top of the fetched commits.
Customizing Default Behavior: You can set your default pull
behavior to rebase by configuring Git:
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.
In the context of Git, origin/main
refers to two things combined:
Remote repository: The word
origin
is a nickname for the remote repository we cloned our local project from. This could be a service like GitHub, GitLab, or any other Git hosting platform.Branch on remote repository:
/main
refers to themain
branch that exists on that remote repository.
So, origin/main
essentially points to the main
branch on the remote repository that your local copy (main
branch) was originally cloned from. It's a way for our local Git to keep track of its connection to the upstream branch.
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