Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 24, 2022 07:42 pm GMT

How to force overwrite local changes with 'git pull'

Have you ever been working on a project in git and ran into an error telling you that you can't use git pull because you have local changes?

error: Untracked working tree file 'App.vue' would be overwritten by merge

This is usually some changes have been committed to the repo you are pulling from - but you have a similar file locally. For example, if a file gets accidentally added to a repo called README.md, and you already have README.md on your local version.

Sometimes though, you want to force overwrite your files with the ones found in the repo. In this scenario, your local changes will be replaced by the ones found on the remote repository.

Forcing git pull

To force a git pull, you want to do three things:

  • first sync up and fetch all remote repository changes.
  • backup your current branch - since when we force the pull, all changes will be overwritten.
  • force the git pull.

The important thing to do here is a backup, where you commit all your local changes to a backup branch. You can also copy your files somewhere else if you're worried about overwriting them. If you do not commit/backup your local changes to another branch, they will be overwritten so please be careful. :)

To force a git pull, we run the following commands to create a backup branch, and then force the git pull on the master branch:

git fetch --all# Creates a new branchgit branch my-backup-branch# Switch to the new branch.. we'll use it to backup our local changesgit switch my-backup-branch# Add all files to a commitgit add .# Commit the new branch, so that it is savedgit commit -m "Backup of branch"# Switch back to our main branch, `master`git switch master# Force git pull using `git reset --hard`git reset --hard origin/master

First, git fetch --all syncs up our remote to our local. Then, git branch my-backup-branch creates a new branch, which we switch to for the backup. After that, I've added in a commit, so that we commit any changes on that backup branch, my-backup-branch, so the contents remain saved. If you don't commit your changes to the backup branch, you will lose them.

Then we switch back to our main, master branch, assuming your main branch is called master. If it's called something else, you will have to use that command. You can see all other branches available to switch to by running git branch --list.

Finally, we use git reset --hard origin/master to force git pull. This will force overwrite any local changes you made.

And you're done. Now your local changes will be backed up on the branch my-backup-branch, and all remote changes will be forced into your master branch.

Forcing Git Pull - the key command

The key command to force a git pull from a remote repository is git reset --hard origin/master. The other commands are to ensure you don't lose any data, by making a backup!

Can't find origin/master

If you can't find origin/master, you may now have that branch on your origin. Instead, try running git branch -r to see any remote branches, so you can pick the one you want to git reset from.


Original Link: https://dev.to/smpnjn/how-to-force-overwrite-local-changes-with-git-pull-p62

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To