# clone in your remote repo into a new and separate branch. Here is a git example of the flow: # branch A is where your current code is One way to prevent the error is to create a brand new branch, pull your required code in, and then manually merge your local branch into your main flow. However, sometimes you just want to keep the commits. The easiest way to prevent the fatal: refusing to merge unrelated histories error is to avoid pulling remote repositories into branches that already have commits on them. How to prevent ‘fatal: refusing to merge unrelated histories’ git stash apply: keeps the changes in the stash and applies the changes to the current code.git pop: ‘pops’ the changes from the stash and applies them to the current code.Here is a quick summary of differences between git stash apply and git pop: This will move the changes stashed and reapplies them to your current working copy.Īlternatively, you can use git stash apply to add the changes to your current working copy of code. Once you’ve successfully pulled into your branch, you can unstash your files, commit them as a separate commit and resolve any file conflicts that you may have. This will give you a clean working tree to pull your remote repository into. To stash your unsaved files, use the following git command: git stash. To unstage all the files in your last commit, use the following git command: git reset HEAD~. This will ensure that any conflicts that you may encounter in the code are addressed before merging and prevent application errors from occurring. The alternative (and longer) way of fixing the fatal: refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone. Option 2: unstage, stash, clone, unstash, and then commit gitignoreĪutomatic merge failed fix conflicts and then commit the result. Here is an example of what a common conflict may look like when trying to merge branches: Auto-merging package.jsonĬONFLICT (add/add): Merge conflict in package.jsonĬONFLICT (add/add): Merge conflict in package-lock.jsonĬONFLICT (add/add): Merge conflict in README.mdĬONFLICT (add/add): Merge conflict in. However, in reality, at least one thing pops up, and you will need to use the normal Git resolution flow to resolve them. This git flag works seamlessly when there are no file conflicts. The idea behind -allow-unrelated-histories is that git lets you merge unrelated branches. You can also replace the master branch with whatever branch you want the pull request to merge into. You can substitute origin with the remote repository you are pulling from. Here the git command will look something like this: git pull origin master -allow-unrelated-histories. One way to solve the issue is to use the -allow-unrelated-histories git flag. Option 1: Use ‘–allow-unrelated-histories’ There are two ways of solving the fatal: refusing to merge unrelated histories error. Your branch is up to date with 'origin/main'.How to resolve ‘fatal: refusing to merge unrelated histories’ You can see your state of repository and staging area with the git status command.įor example, here below is the result of git status on my repository: On branch main For example, if the branch you have checked out tracks origin/master, git pull is equivalent to git pull origin main Git pull only works if the branch you have checked out is tracking an upstream branch. ().Īfter a fresh clone doing git clone you will have a local branch “main”, a remote “origin” and your main branch has “origin/main” as upstream. Git pull origin main fetches commits from the main branch of the origin remote (into the local origin/main branch), and then it merges origin/main into the branch you currently have checked out. For other version control platforms, it is usually git pull origin master.Ī pull is a fetch and a merge: git pull = git fetch + git merge You can use git pull origin main if you are using Github for version control. Only doing git pull sometimes does not give you the latest commits.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |