Git pull force to overwrite local files

Let’s consider a simple scenario, There are many team members working with the same files and folders and you run into a merge conflict when you use git pull. In most cases you resolve the merge conflicts manually. However in some cases you need to use git pull force to overwrite you local changes.

How to force git pull to overwrite local files

Important :-If you have made any local changes, force git pull will overwrite your local files and the changes you made will be lost. Even if you use with or without –hard option, any local commits that haven’t pushed to the server will be lost.

If you have any files that are not tracked by Git, these files won’t be affected.

First step is to fetch all the branches. If you want to fetch a specific branch then use git -fetch origin/branch_name

If you want to fetch a specific branch then use git -fetch origin/branch_name

git fetch will downloads the latest code from remote branch without merging or rebase anything.

Use git reset to overwrite your local files

Note: If you want to use any other branch name then replace master with your branch name Eg:-

The git reset resets your current branch to the latest origin state which you have just fetched. The –hard option overwrites all your local files in your working tree to match files in origin master.

Delete untracked files and directories from your local

If you want to get rid of all your untracked files and folder after git force pull then you can run below git command to perform the task.

git clean delete all your untracked files but first consider running it with -n(dry run) which shows you what are the files that will be deleted without actually deleting it.

git clean dry run command

git pull force
Output of git clean with -n option

git clean remove untracked files and folders

Many times -f option won’t work effectively as it deletes only the file not the directories. If you have both files and directories to be deleted then use -f -d option during git clean.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.