When you start developing a project and work in a team, you learn and use version control systems to manage your projects efficiently sooner or later.
To understand Git and work with it, you need to understand the workflow of Git and the common errors that you might face while performing Git operations.
Table of Contents
How Git Pull Works?
While working with Git, you maintain two repositories; one is remote that is on Git, while the other one is a local repository that you keep on your local machine.
Now, suppose you have another team member connected with the same repository.
To fetch those changes from Git, you need to go with the pull operation. Pulling the changes from the remote repository is a two-step process.
git fetch
git merge origin/$CURRENT_BRANCH
Pulling is simple as long as you are working with committed changes. However, the problem arises when you try to run git pull on uncommitted changes.
What Causes the Failure of Merging Changes with Git Pull?
It is all okay until you two work on different files of the project, but when you work on the same files of a project at the same time while keeping it uncommitted, and try to pull the changes from the remote repository using command git pull; Git hits you with error: Your local changes to the following files would be overwritten by merge:
This is because both of you have been making changes on your local repositories without committing the work, and Git is now confused with what to overwrite and replace? The problem is, you don’t want Git to pull the changes and deliberately merge them with your version of the project. Instead, you want to pull them.
Scenario#01: When You Don't Care About Local Changes
- Fetching the changes.
- Resetting the local branch to its unmodified state and discard all uncommitted changes.
- Merge the changes.
git fetch
git reset --hard HEAD
git merge origin/$CURRENT_BRANCH
git fetch
git reset --hard HEAD
git merge '@{u}'
Scenario#02: When You Care About the Local Changes.
- Commit them first, and then pull using git pull.
- Stash them.
Stashing is a Git operation that keeps your uncommitted changes stored temporarily for a moment, and you can later bring them back to commit. This can be done using the git stash command
This type of commit remains accessible to Git but doesn’t appear in your current branch. Later on, use the git stash pop command to bring back the stashed changes and commit them. This command will also remove the temporarily kept stash commit.
git fetch
git stash
git merge '@{u}'
git stash pop
Scenario#03: When You Just Want to Merge the Fetched Changes from Remote Repository
Sometimes you are in a situation where you can’t lose your locally made changes (overwriting), neither can’t you stash them.
In such scenarios, you want to compare downloaded changes and local changes and only save the changed area among both. You can achieve this by running the git diff command.
Git Pull Force Command
As it sounds, git force pull isn’t something that can forcefully pull the changes and merge them magically. Instead, it just offers another way to pull the changes.
Traditionally, when you use the git fetch command, it is understood that you will provide the branch names in arguments of the command if you want the changes from one remote branch to some other local branch, i.e., it simply allows you to specify the branches on your own. For example:
git fetch origin/branch-1:my-branch
Conclusion
In this article, we discussed the complications faced while trying to push changes from a local repository to a remote repository on Git, especially when working in a team and on the same files of the project. We analyzed three different scenarios of merging the changes to attain a synchronized state between the local and remote repositories and their solutions. We also discussed the flexibility offered by git pull –force specifically.