Git pull ошибка

Problem

Windows users can often have this problem

git pull 

gives the error: error: cannot lock ref unable to update local ref

Cause

Cause a) There are multiple branches, whose
names from the beginning up to any slash (or to the end), differ only in upper and lower case.

Branch name clashing (upper/lower case)
#######################################

# Example 1)
#############################
feature/releasecandidate/fix123
feature/releaseCandidate/improveFeature789
------------------------
               ^
  Identical from beginning up to a slash (here the 2nd one)
  except for the marked letter, where the upper/lower case differs




# Example 2)
#############################
releaseBranch
releasebranch
-------------
       ^
  Identical from beginning to the end
  except for the marked letter

Cause b) Also a problem on linux: One branch is a prefix of another, with a slash boundary:

Prefix with slash-boundary
#######################################

# Example 1) - also a problem on linux
#############################
feature/release2021
feature/release2021/fixIssue07
                   ^
              slash boundary

# Example 2)
#############################
feature/release2022
feature/Release2022/fixIssue99
        ^          ^
  differing case   slash boundary
 (problem on 
   windows)

Solution

Remove the cause (see exact Cause above).

# inspect your branches, to see if you have the upper/lower case problem
git ls-remote --heads YOUR-GIT-URL

For example: create a branch-naming policy, e.g. all in lower-case letters; or letters before the last slash in lower-case. Or some smart hook, that detect a violation. (but note: In cause a) the problem is only on windows, not on linux).

Background

The problem is that windows stores these branches (from example 1 and 2) in the .git folder

# inspect the files/folders under
.git/refs/remotes/origin/

and in Cause a) windows cannot distinguish
the differences in upper/lower case, so git on window goes crazy.

In Cause b) you cannot have a folder (e.g. feature/release2021/) with the same name as a file (feature/release2021).

Workaround

A short-term workaround that often works (until you’ve removed the cause) is:

git pack-refs --all

# delete the contents of .git/refs/remotes/origin/*  
rm -rf .git/refs/remotes/origin/*

git pull; git pull; git pull   # all good? yes!

Do you get the error message, «Pulling is not possible because you have unmerged files,» when you try to git pull changes? Worry no more.

This tutorial explains the origin of the error message and how to solve it.

Let’s do this!

Understand the basics

Git pull

Let’s start by understanding what git pull entails.

Think of the code tracking environment as a two-part architecture: local and remote environment. In a local environment, we create the project files in a working directory. Next, we stage the changes. We can fall back to untracked files or safely store the files in a repository.

In simple terms, a repository is the version control system’s database. Here, Git refers to the new changes as commits and assigns them unique numbers called commit hashes.

Although the changes are safely stored and can be retrieved, they are local. That means a loss of the computer results in a loss of our files. So, we send the changes to a remote repository through git push.

ALSO READ: Yes, you CAN change commit message in git, here’s HOW!

We can then collaborate with more remote developers, working on the same or separate git branches.  A developer does their part and pushes the changes to the specified branch. To get the other developer’s changes, you do the opposite of git push: git pull.

git pull is the process of updating a local repository with its updated remote version.

Some articles related to this topic:
git pull force Explained
git pull command examples
git pull vs git pull —rebase explained with examples

Git merge

A typical repository has multiple branches: the main branch and its subbranches.

After creating commits in the subbranch, we unite them with the main branch. This process is called git merge. Git merge enables collaboration in a project. Git is intelligent enough to notice the merge timestamps per branch.

Some articles related to this topic:
git merge explained
Git merge vs rebase and the problems they solve

However, here is a catch.

Merge Conflicts

Sometimes Git gets confused when it cannot determine the logical hierarchy of code portions resulting from commits of separate branches. Such a scenario is called a merge conflict.

A merge conflict needs to be resolved. Otherwise, your subsequent pull requests will fail. You can correct the pull error by resolving the merge conflict through a hard reset or manually accepting the new commit’s changes in the script file.

ALSO READ: How to perform git undo add PROPERLY [5 Examples]

Now that you know the origin and the solution to the typical error, «Pulling is not possible because you have unmerged files,» let’s set up a lab environment and do it practically.

Lab environment setup

Log into your GitHub account and create a new repo with a README.md file. I am creating one called lab.

[SOLVED] Pulling is not possible because you have unmerged files

Copy the repo URL.

[SOLVED] Pulling is not possible because you have unmerged files

Return to your local machine’s terminal, clone the repo, and cd into it.

[SOLVED] Pulling is not possible because you have unmerged files

Now let’s generate the error: Pulling is not possible because you have unmerged files.

Generate the error: Pulling is not possible because you have unmerged files

Make a commit in the main branch

Let’s create index.html with a title and a description paragraph.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pulling is not possible because you have unmerged files</title>
</head>
<body>
    <h2>Let's create the error</h2>
    <p>You are about encounter the error: pulling is not possible because you have unmerged files</p>
</body>
</html>

Stage and commit the changes.

git add index.html
git commit -m "Add index.html"

Now we have two commits in the main branch.

git log --oneline

[SOLVED] Pulling is not possible because you have unmerged files

Make a commit in the feature branch

Create a feature branch.

git switch -c feature_branch

And modify the index.html file by adding a paragraph: First, create the feature branch. We use Visual Studio Code to ease the development and manual error correction. Stage the changes and make a commit.

Now the feature branch is ahead of the main branch with a commit.

ALSO READ: git stash explained in detail with examples

[SOLVED] Pulling is not possible because you have unmerged files

Switch to the main branch, and modify the index.html with a paragraph: Generating the error.

Next, stage the changes and create a commit.

[SOLVED] Pulling is not possible because you have unmerged files

What happens when we attempt to merge the feature branch?

Let’s find out.

Create a merge conflict

Now let’s attempt merging the feature branch’s changes.

Input

git merge feature_branch

Output

user@hostname:~/lab$ git merge feature_branch 
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Git tells us it found a conflict in the index.html file and recommends that we correct the error before proceeding.

Okay, let’s inspect the file.

Input

cat index.html

Output

user@hostname:~/lab$ cat index.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pulling is not possible because you have unmerged files</title>
</head>
<body>
    <h2>Let's create the error</h2>
    <p>You are about encounter the error: pulling is not possible because you have unmerged files</p>
<<<<<<< HEAD
    <p>Generating the error</p>
=======
    <p>First, create the feature branch</p>
>>>>>>> feature_branch
</body>

Git is stranded on whether the paragraph from the latest commit (HEAD) from the main branch should come first or the feature branch’s paragraph.

<<<<<<< HEAD
    <p>Generating the error</p>
=======
    <p>First, create the feature branch</p>
>>>>>>> feature_branch

We could edit the file to capture our intention before merging the feature branch. However, let’s ignore the call to resolve the conflict. Nor merge the feature branch.

ALSO READ: SOLVED: git remove file from tracking [Practical Examples]

Instead, let’s return to the GitHub repository and update the README.md file

[SOLVED] Pulling is not possible because you have unmerged files

Getting error

Now attempt to pull the remote changes.

Input

git pull

Output

user@hostname:~/lab$ git pull
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

Git says we have an error: Pulling is not possible because you have unmerged files.

Pulling is not possible because you have unmerged files

Resolving the error: Pulling is not possible because you have unmerged files

Although we could reset one of the commits, we will retain the changes by accepting both paragraphs in the index.html.

from

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pulling is not possible because you have unmerged files</title>
</head>
<body>
    <h2>Let's create the error</h2>
    <p>You are about encounter the error: pulling is not possible because you have unmerged files</p>
<<<<<<< HEAD
    <p>Generating the error</p>
=======
    <p>First, create the feature branch</p>
>>>>>>> feature_branch
</body>
</html>

to

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pulling is not possible because you have unmerged files</title>
</head>
<body>
    <h2>Let's create the error</h2>
    <p>You are about encounter the error: pulling is not possible because you have unmerged files</p>
    <p>Generating the error</p>
    <p>First, create the feature branch</p>
</body>
</html>

Then, stage and commit the changes.

ALSO READ: git undo commit before push [Practical Examples]

Input

git add .
git commit -m "Accept both paragraphs"

Output

user@hostname:~/lab$ git add .
user@hostname:~/lab$ git commit -m "Accept both paragraphs"
[main 308f222] Accept both paragraphs

Now git pulling does not produce the primary error.

Input

git pull

Output

user@hostname:~/lab$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 779 bytes | 194.00 KiB/s, done.
From github.com:Stevealila/lab
   634d087..4685287  main       -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

Although the pull process goes through, we get some warnings. The relieving part is that Git gives us some hints on how to get rid of the warnings.

[SOLVED] Pulling is not possible because you have unmerged files

Summary

The error, «Pulling is not possible because you have unmerged files,» often results from attempting to git pull changes without resolving local merge conflicts. As shown in this tutorial, you can correct the conflicts before proceeding with git pull.

ALSO READ: How to PROPERLY use git remote add? [SOLVED]

There are several causes for a malfunctioning git-pull command. We’ll do our best to mention the most frequent below:

1. Not enough information for Git to work with

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=<remote>/<branch> master

As the error message states: you will have to let Git know what remote branch it should use to track with the current local branch. Doing this will allow you to simply run git pull and Git will know where to bring new data from.

2. Git pull would overwrite uncommitted files in your local repository

Git is doing its best to prevent you from losing data. As such, at times, it will simply prevent a git pull command from merging new data fetched from the server with your local branch.

$ git pull
From REPOSITORY_URL
 * branch            master     -> FETCH_HEAD
   a152b19..171e4a2  master     -> origin/master
Updating a152b19..171e4a2
error: Your local changes to the following files would be overwritten by merge:
  file1.txt
  file2.txt
Please commit your changes or stash them before you merge.
Aborting

The message received from Git is a good starting point. The two files have been modified locally, and the new data fetched from the remote cannot be automatically merged by Git. As such, our intervention is required:

  • You may try to commit the changes first and then run git pull again:
  • $ git commit -am 'Committing two files before git-pull'
    [master d91368b] Committing two files before git-pull
     2 files changed, 2 insertions(++)
    
    $ git pull
    From REPOSITORY_URL
     * branch            master     -> FETCH_HEAD
       a152b19..171e4a2  master     -> origin/master
    Updating a152b19..171e4a2
    Fast-forward
     file1.txt | 1 +
     file2.txt | 1 +
     2 files changed, 2 insertions(++)
  • You may try to stash your local changes, pull from the remote, and then apply the stashed changes:
  • $ git stash
    Saved working directory and index state WIP on master: d91368b Previous commit message
    
    $ git pull
    From REPOSITORY_URL
     * branch            master     -> FETCH_HEAD
       a152b19..171e4a2  master     -> origin/master
    Updating a152b19..171e4a2
    Fast-forward
     file1.txt | 1 +
     file2.txt | 1 +
     2 files changed, 2 insertions(++)
    
    $ git stash apply

For a complete list of all options for git-pull check the official Git docs: https://git-scm.com/docs/git-pull.




Содержание статьи


fatal: No such remote ‘origin’

fatal: No such remote ‘origin’

fatal: No such remote ‘origin’

Скорее всего Вы пытаетесь выполнить

$ git remote set-url origin https://github.com/name/project.git

Попробуйте сперва выполнить

$ git remote add origin https://github.com/name/project.git

2

To https://github.com/YourName/yourproject.git

! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘https://github.com/YourName/yourproject.git’


hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: ‘git pull …’) before pushing again.

hint: See the ‘Note about fast-forwards’ in ‘git push —help’ for details.

Скорее всего Вы пытаетесь выполнить

git push origin master

в новом репозитории.

При этом, когда
Вы создавали удалённый репозиторий на github Вы отметили опцию initialize with readme file.

Таким образом на удалённом репозитории файл

README.md

есть, а на локальном нет. Git не
понимает как такое могло произойти и предполагает, что на удалённый репозиторий кто-то (возможно Вы)
добавил что-то неотслеженное локальным репозиторием.

Первым делом попробуйте

$ git pull origin master

$ git push origin master

Git скачает файл

README.md

с удалённого репозитория и затем можно будет спокойно пушить

Если сделать pull не получилось, например, возникла ошибка

From https://github.com/andreiolegovichru/heiheiru

* branch master -> FETCH_HEAD

fatal: refusing to merge unrelated histories

Попробуйте

$ git pull —allow-unrelated-histories origin master

$ git push origin master

ERROR: Permission to AndreiOlegovich/qa-demo-project.git denied to andreiolegovichru.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Возможная причина — git не видит ваш ключ. Либо ваш ключ уже используется для другого удалённого
хранилища.

Решить можно удалив стандарный ключ id_rsa.pub создать новую пару и добавить на удалённое хранилище.

Если этот способ не подходит — нужно настроить config.

assuming that you have write access to your personal git server, what you are doing is pushing changes to a non-bare repository, and to a branch that are currently checked out as working directory.

The current common good practices is always push to a bare repository. To create it, you could do something like this:

git init --bare SomeRepo.git

if you insist on pushing to a non-bare repository, which is not recommended, make sure that the branch that you are going to push to is not the current checked out branch or the current working directory. just open a command line tools, such as terminal in Linux or git bash in windows, move to your repository directory (the one that has a folder .git in it), execute this command:

git branch -v

it will show all the available branches of that repository, and the branch that currently checked out is marked with ‘*’, for example:

* master        b0bb0b1 some commit message
  development   babbab2 some other commit message
  experimental  bebbeb3 some commit message in experiment

the example above shows that master is the currently checked out branch, thus you couldn’t push to it. But you could push to other branches, such as development or experimental. If you must push to master, you could do it by changing the current directory to other branches, using this command:

git checkout the_branch_name

after this, the currently checked out is the branch that specified in above command, pushing to master is now possible.

Понравилась статья? Поделить с друзьями:
  • Girbau коды ошибок
  • Ginzzu ошибка е1 плита
  • Gilgen ошибка e1000
  • Gilgen автоматические двери коды ошибок e2000
  • Gilgen автоматические двери коды ошибок 105