开发者

Using 'git remote add origin' creates a global remote instead of one local to current directory/repo

开发者 https://www.devze.com 2023-04-04 10:21 出处:网络
Solution:Finally realized the problem.Stupid User Error.Forgot to run \'git init\' in the new project directory first.Hence, running \'git add .\', \'git remote add origin repo.git\' (or even the typo

Solution: Finally realized the problem. Stupid User Error. Forgot to run 'git init' in the new project directory first. Hence, running 'git add .', 'git remote add origin repo.git' (or even the typo 'git add origin repo.git') in a directory without a git repo, will revert to applying the command globally, hence all the parent and other directories开发者_运维问答 in the git status results.


I've been using a git for a while and never hit this problem, not sure what's going on. In a nutshell, using 'git remote add origin git@github.com...' creates a global remote across my entire filesystem, instead of a local one inside the current directory only. It even overwrites the remotes in other lateral directories.

The code is worth a thousand words, anyone know what's going on here:

prompt:~/bin/Projects$ ls -al
total 56
drwxr-xr-x 15 kurtosis kurtosis 4096 2011-09-09 21:35 .
drwxr-xr-x 26 kurtosis kurtosis 4096 2011-09-09 21:35 ..
drwxr-xr-x  5 kurtosis kurtosis 4096 2011-06-16 13:42 byrongibson.com
drwxr-xr-x  5 kurtosis kurtosis 4096 2011-06-16 13:44 byrongibson.github.com
drwxr-xr-x  3 kurtosis kurtosis 4096 2011-09-09 21:33 know.io
drwxr-xr-x  3 kurtosis kurtosis 4096 2011-06-16 13:45 problem-sets
drwxr-xr-x  2 kurtosis kurtosis 4096 2011-07-18 20:11 vim~
prompt:~/bin/Projects$ git remote -v
prompt:~/bin/Projects$ cd know.io/
prompt:~/bin/Projects/know.io$ ls -al
total 16
drwxr-xr-x  3 kurtosis kurtosis 4096 2011-09-09 21:33 .
drwxr-xr-x 15 kurtosis kurtosis 4096 2011-09-09 21:35 ..
-rw-r--r--  1 kurtosis kurtosis    7 2011-09-09 21:27 README.md
drwxr-xr-x  2 kurtosis kurtosis 4096 2011-09-09 21:27 vim~
prompt:~/bin/Projects/know.io$ git remote -v
prompt:~/bin/Projects/know.io$ git remote add origin git@github.com:byrongibson/know.io.git
prompt:~/bin/Projects/know.io$ git remote -v
origin  git@github.com:byrongibson/know.io.git (fetch)
origin  git@github.com:byrongibson/know.io.git (push)
prompt:~/bin/Projects/know.io$ cd ..
prompt:~/bin/Projects$ git remote -v
origin  git@github.com:byrongibson/know.io.git (fetch)
origin  git@github.com:byrongibson/know.io.git (push)
prompt:~/bin/Projects$ cd ~
prompt:~$ git remote -v
origin  git@github.com:byrongibson/know.io.git (fetch)
origin  git@github.com:byrongibson/know.io.git (push)
prompt:~$ cd bin/Github
prompt:~/bin/Github$ git remote -v
origin  git@github.com:byrongibson/know.io.git (fetch)
origin  git@github.com:byrongibson/know.io.git (push)
prompt:~/bin/Github$ 

Update1: This problem mysteriously went away. No idea what I did or why. If I figure it out, will update with answer.

Update2: I did it again with another repository. This time I think it was because, when I was setting up the repository and adding the remote origin, I made a typo and did this:

> git add origin git@github.com/.../repo.git

E.g., I forgot 'remote'. That set the global origin in ~/.git/config to:

[remote "origin"]
    url = git@github.com/.../repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

Deleting that from config removes the global origin, but unfortunately when I cd back into my new repo and use 'git add .', it not only adds every file and directory in my repo, but apparently everything outside my repo that's not in my global .gitignore as well:

prompt:~/bin/Projects/Haskell/Courses/Stanford$ git remote -v
prompt:>~/bin/Projects/Haskell/Courses/Stanford$ git remote add origin git@github.com:byrongibson/stanford-fall2011-cs240h.git
prompt:>~/bin/Projects/Haskell/Courses/Stanford$ git remote -v
origin  git@github.com:byrongibson/stanford-fall2011-cs240h.git (fetch)
origin  git@github.com:byrongibson/stanford-fall2011-cs240h.git (push)
prompt:>~/bin/Projects/Haskell/Courses/Stanford$ git add .
prompt:>~/bin/Projects/Haskell/Courses/Stanford$ ls -al
total 12
drwxr-xr-x 3 byron byron 4096 2011-09-26 18:19 ./
drwxr-xr-x 3 byron byron 4096 2011-09-26 18:19 ../
drwxr-xr-x 2 byron byron 4096 2011-09-26 18:09 Lectures/
prompt:>~/bin/Projects/Haskell/Courses/Stanford$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   Lectures/basics1-slides.html
#   new file:   Lectures/basics1.html
#   new file:   Lectures/basics1.md
#   new file:   ../../../../btccx/btc.cx/README
#   new file:   ../../../../btccx/btc.cx/README.md
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   ../../../../../.gitconfig
#   modified:   ../../../../../.profile
#   modified:   ../../../../../Downloads/Keys/chromiumppa_key.html
#   modified:   ../../../../../Downloads/Keys/chromiumppa_key.pub
#   modified:   ../../../../../Downloads/Keys/linux_signing_key.pub
#   modified:   ../../../../../Downloads/Keys/tor-key-install.sh
#   deleted:    ../../../../btccx/btc.cx/README
#   deleted:    ../../../../btccx/btc.cx/README.md
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   ../../../../../.FBReader/
#   ../../../../../.ICEauthority
#   ../../../../../.Skype/
#   ../../../../../.SpiderOak/
.
.
.
#   ../../../../../Downloads/xmonad-ubuntu.html
#   ../../../../../Downloads/xmonad-ubuntu_files/
#   ../../../../../Dropbox/
#   ../../../../../Music/
#   ../../../../../Pictures/
#   ../../../../../SpiderOak/
#   ../../../../../Ubuntu One/
#   ../../../../../Videos/
#   ../../../../Archive/
#   ../../../../Clients/
#   ../../../../CryptoCurrency/
#   ../../../../Databases/
#   ../../../../Git/
#   ../../../../Go/
#   ../../../../Google/
#   ../../../../Hosts/
#   ../../../99-scala-problems/
#   ../../LYaH/
#   ../../RWH/


You would normally get the following error if you run git remote -v outside a git repository:

$ git remote -v
fatal: Not a git repository (or any of the parent directories): .git

I would guess that what's happened is that you accidentally ran git init in your home directory at some point, which would explain what you're seeing. If you run:

git rev-parse --show-toplevel

That should show you the top level of the git repository you are actually in.


Solution: Just to clarify above explanation: I forgot to run 'git init' in the new project directory first. Hence, running 'git add .', 'git remote add origin repo.git' (or even the typo 'git add origin repo.git') in a directory without a git repo, will revert to applying the command globally, hence all the parent and other directories in the git status results.

Running 'git init', then 'git remote add origin repo.git' fixes everything above.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号