How to Use Git Force Pull Properly

Alex KondratievAlex Kondratiev
MaximMaxim

4 min read

When multiple users are working with the same Git files and folders, you can run into conflict issues that might be tricky.


In most cases, you want to resolve the conflicts manually. However, there might be cases where you want to git force pull to overwrite your local changes. The git pull command might not be enough to force this kind of overwrite. Let’s create a situation where this might happen.

Warning: The below technique to force git pull will delete your local changes. If you’re trying this out in a live environment, you can end up losing your work. The example below starts from scratch, so you can try it out on the test repository before you use it with a real environment.

Initial data

Let's create the following directories:

yaml

1├── repository
2├── user1
3└── user2

Let’s create an empty repository:

shell

1cd repository/
2mkdir myproject.git
3cd myproject.git/
4git init --bare

This is going to be the remote repository.

Now let’s go into the user1 folder, clone the empty repository, and add two files (ReadMe.txt and Image1.txt).

Then, push the change to the remote repository (please use your clone path).

shell

1git clone /Users/_work/LearnGIT/git_force_pull/repository/myproject.git
2touch ReadMe.txt Image1.txt
3git add -A
4git commit -m "Initialization"
5git push origin master

Now let’s go to the user2 folder and clone the remote repository.

git clone /Users/_work/LearnGIT/git_force_pull/repository/myproject.git

Your directories should look as follows:

yaml

1├── repository
2│ └── myproject.git
3│ ├── HEAD
4│ ├── branches
5│ ├── config
6│ ├── description
7│ ├── hooks
8│ ├── info
9│ ├── objects
10│ └── refs
11├── user1
12│ └── myproject
13│ ├── Image1.txt
14│ └── ReadMe.txt
15└── user2
16└── myproject
17├── Image1.txt
18└── ReadMe.txt

Both user1 and user2 have the same Image1.txt and ReadMe.txt files.
[pbutton]

Conflict case

Now let's create the conflict situation. Both users start working on Image2.txt file locally. After a while, user1 commits and pushes the changes to remote repository.

In the user1/myproject folder, the following steps take place:

shell

1echo "USER1" > Image2.txt
2git add -A
3git commit -m "Added Image2.txt (USER1)"
4git push origin master

You can check the history:

shell

1git log --oneline
23713dfc Added Image2.txt (USER1)
31151a79 Initialization

Now let's go to the user2/myproject folder. User2 creates and commits the code locally.

shell

1echo "USER2" > Image2.txt
2git add -A
3git commit -m "Added Image2 (USER2)"

But user2 decides to see if any other user has made changes. So user2 pulls changes and runs into a problem:

shell

1git pull origin master
2remote: Counting objects: 3, done.
3remote: Compressing objects: 100% (2/2), done.
4remote: Total 3 (delta 0), reused 0 (delta 0)
5Unpacking objects: 100% (3/3), done.
6From /Users/_work/LearnGIT/git_force_pull/repository/myproject
7* branch master -> FETCH_HEAD
81151a79..3713dfc master -> origin/master
9Auto-merging Image2.txt
10CONFLICT (add/add): Merge conflict in Image2.txt
11Automatic merge failed; fix conflicts and then commit the result.

At this stage, if you check the history of user2's workspace, you see that user2 has the following log:

shell

1git log --oneline
21e2c8d3 Added Image2 (USER2)
31151a79 Initialization

The conflict can be manually resolved. But user2 decides to get rid of the local changes and start from where user1 left off.

Conflict resolution using git force pull

The following method is the most effective way to force git pull:

shell

1git fetch origin master
2git reset --hard origin/master

(If you are working with branches, use the branch name instead of master branch). Now if you check user2 history, you see that 1e2c8d3 change has been replaced by 3713dfc change (Your local hashes will be different).

shell

1git log --oneline
23713dfc Added Image2.txt (USER1)
31151a79 Initialization

So the git force pull has got rid of user2's local changes and reset it to origin master.
Special Tip: During a continuous integration/continuous delivery (CI/CD) process, the CI/CD system might not pull the latest changes if the workspace is not cleaned properly.

You can use the above method to make sure all of your versioned files are force synced with your remote origin. Also, if you want to get rid of untracked files, you can use the following git command:

git clean -f -d

Warning: The above command will delete all untracked files from your workspace.

Conclusion

Using the above technique to force overwrite your current workspace can save you time. However, use this technique with caution. If you make it a regular habit, you might mistakenly use it and lose your local work unintentionally.

Looking for help with GIT? Drop us a line and get FREE consultation!

Alex Kondratiev

Alex Kondratiev

Founder of ITsyndicate. DevOps Enthusiast with 15+ years of experience in cloud, Infrastructure as Code, Kubernetes, and automation. Specialized in architecting secure, scalable, and resilient systems.

Plan the present.
Build the future.