KansaiRobot
KansaiRobot

Reputation: 9912

git changing branches from a remote repo

I cloned a repository (from gitlab) and I think I commit a mistake.

First I cloned the repo, I made some changes and committed them in a local branch, then I realized I wanted to see another branch of the repo "another_branch" so I did

git checkout another_branch

but then I realized that some files were not there. So I did

git pull origin another_branch

and I got the files but I got the message

uto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

So now my repo is a mess with middle merging and uncommited changes, etc.

What did I do wrong and how can I correct this.

Upvotes: 1

Views: 43

Answers (1)

CodeWizard
CodeWizard

Reputation: 141956

After your first clone, you have changed some files. When you tried to pull (checkout) the second branch git detect your changes and in your case, it caused a conflict.

How to fix it?

  • Few solutions here

Get rid of your local changes

  • In case you don't need to keep your changes and you wish to discard them
# Discard local changes
git reset origin/<branch name> --hard

# Now check to see that you don't have any "new" unchanged files
git status

Keep local changes

  • You are in a middle of a merge so again few options:

    • Abort the merge, commit or drop your changes
    # Abort the merge
    git merge --abort
    
    # Now you are back to square 1.
    # If you wish to keep your changes - commit them
    # if you don't need them same as above 
    git reset origin/<branch> --hard
    

For a more detailed answer read those posts:

Upvotes: 2

Related Questions