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.
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 status
The following output will appear after executing the command. The output shows that the task is not committed yet.
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.
$ 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.
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 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.
Run the previous commands again to check how the `git reset` command works for the committed task.
$ 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.
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 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.
Now, Run the following commands to check the content of the basic.py file after switching to the main branch.
$ cat basic.py
The following output shows that the content of the basic.py has remained unchanged.
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.