Git files and folders of any git project can be shared by multiple git users who are work on a team. When multiple users try to work on the same file or folder, then conflict arises. Suppose, when the two users are working on the same file of a repository, and if one user modifies the file after the modification done by another user, then the modification done by the first user will be lost by the last update. This problem can be solved manually. The user has to git force pull for overwriting the changes in the local repository without affecting the changes done in the local repository that are not pushed. `git pull` command is not able to solve this problem. The way to use git force pull by `git fetch` and `git reset` commands have shown in this tutorial.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require to create a GitHub account to check the commands used in this tutorial.

Create a local and remote repository

You have to create a local repository and publish the repository in the remote server to test the commands used in this tutorial.

Overwrite local changes for forcing git pull:

The `git fetch –all` command downloads all content of the remote repository into the local repository but doesn’t merge the content with the local repository. After executing the fetch command, if the `git reset` command is executed with the –hard option, then all the matching files and folders of the local repository will be overwritten by the content of the remote repository. All the uncommitted and committed local changes that are not pushed will be deleted for the –hard option. This problem has been described in this part of the tutorial by using a local repository named python published before in the remote server.

Open the basic.py file from the remote repository to check the content of the file. The following image shows that the file contains four lines of script to add two numbers.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-1.png" data-lazy- height="477" src="data:image/svg xml,” width=”897″>

Force git pull for uncommitted changes:

Now, open the basic.py file of the local repository in any text editor and modify the file with the following content. Save the file and quit from the editor.

print(“Adding three numbers”)


a=10


b=20


c=30


print(a b c)

Run the following commands to add the modified basic.py file in the local repository and check the repository status.

$ git add basic.py


$ git status

The following output will appear after executing the command. The output shows that the task is not committed yet.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-2.png" data-lazy- height="194" src="data:image/svg xml,” width=”814″>

Run the following commands to check the content of the basic.py file before pulling the content of the remote repository and check the content of the basic.py after pulling forcefully.

$ cat basic.py


$ git fetch –all


$ git reset –hard origin/main


$ cat basic.py

The following output shows that the content of the basic.py file has been overwritten by the content of the basic.py file of the remote server, and the modified content has been lost.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-3.png" data-lazy- height="333" src="data:image/svg xml,” width=”814″>

Force git pull for committed changes:

Again, open the basic.py file of the local repository in any text editor and modify the file with the following content. Save the file and quit from the editor.

print(“Subtracting two numbers”)


a = 50


b = 20


print(a – b)

Run the following commands to add the modified basic.py file in the local repository, commit the task and check the repository status.

$ git add basic.py


$ git commit -m “basic.py has updated”


$ git status

The following output shows that the modified basic.py file is added and committed with a commit message. The current working tree is clean now.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-4.png" data-lazy- height="205" src="data:image/svg xml,” width=”818″>

Run the previous commands again to check how the `git reset` command works for the committed task.

$ cat basic.py


$ git fetch –all


$ git reset –hard origin/main


$ cat basic.py

The following output shows that the remote file’s content has overwritten the content of the local file again. So, the `git reset` command works the same for both committed and uncommitted tasks.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-5.png" data-lazy- height="316" src="data:image/svg xml,” width=”814″>

Save local changes before forcing git pull:

The overwriting problem can be solved by creating a new branch. Commit all changes to the repository before running the pull commands. Again, open the basic.py file of the local repository in any text editor and modify the file with the following content. Save the file and quit from the editor.

print(“Multiply two numbers”)


a=10


b=20


print(a * b)

Run the following commands to check the branch list, switch to a new branch, and check the content of the basic.py file after executing the pull commands.

$ git branch


$ git checkout -b new-branch


$ git fetch –all


$ git reset –hard origin/main


$ cat basic.py

The following output shows that the content of the basic.py file has overwritten for the new branch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-6.png" data-lazy- height="363" src="data:image/svg xml,” width=”818″>

Now, Run the following commands to check the content of the basic.py file after switching to the main branch.

$ git checkout main


$ cat basic.py

The following output shows that the content of the basic.py has remained unchanged.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-use-Git-Force-Pull-7.png" data-lazy- height="184" src="data:image/svg xml,” width=”817″>

Conclusion:

The problem of pulling the git repository forcefully and how to solve this problem has been explained in this tutorial by using a local and remote demo repository. But this solution will not work for the uncommitted changes of the local repository. So, you have to commit all changes or run the `git stash` command before pulling the git repository forcefully.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/channel-logo-150×150.jpg609fd20fd27c0.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.