我可以更改本地驱动器上所有 Git 活动的默认目录吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2434837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:08:57  来源:igfitidea点击:

Can I change the default directory on my local drive for all Git activity?

gitmacosdirectorylocal

提问by jonobr1

So I just started using Git and GitHub. It's still a steep climb, but I'm starting to enjoy it. One thing I've noticed from when I clone a repository is that it always downloads to my home folder /users/username. Is there a way to change this?

所以我刚开始使用 Git 和 GitHub。这仍然是一个陡峭的攀登,但我开始享受它。当我克隆存储库时,我注意到的一件事是它总是下载到我的主文件夹/users/username。有没有办法改变这种情况?

I went searching through the Git manual and thought I might be able to use --git-dir=GIT_DIR, but I know I'm using it incorrectly because when I try this out what returns in Terminal is

我搜索了 Git 手册并认为我可以--git-dir=GIT_DIR使用它,但我知道我使用它不正确,因为当我尝试这个时,终端返回的是

       git --git-dir=New/Directory/Path/
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG

See 'git help COMMAND' for more information on a specific command.

So it must be wrong...

所以一定是错的...

回答by mipadi

git clonewill copy files to whatever your current directory is, unless you specify otherwise. So if your current working directory is ~, then

git clone除非您另行指定,否则会将文件复制到您当前所在的任何目录。所以如果你当前的工作目录是~,那么

$ git clone me@host:/path/to/repo.git

will copy files to ~/repo.

将文件复制到~/repo.

You can change this one of two ways:

您可以通过以下两种方式之一更改此设置:

cdto the desired directory first:

cd首先到所需目录:

$ cd /path/to/my/files
$ git clone me@host:/path/to/repo.git

will create your repo at /path/to/my/files/repo.

将在/path/to/my/files/repo.

Or, git clonecan take an argument that specifies the location:

或者,git clone可以采用指定位置的参数:

$ git clone me@host:/path/to/repo.git /other/path/to/repo

will create your repo at /other/path/to/repo.

将在/other/path/to/repo.

回答by PeterJCLaw

As @mipadi said you can modify the checkout location when you do a clone:

正如@mipadi 所说,您可以在进行克隆时修改结帐位置:

git clone git://some.path/goes/here some/local/path/here

but I get the feeling that you want a more permanent solution.

但我感觉你想要一个更永久的解决方案。

The --git-diroption sets the path to the repository, which I'm reading as a per-command thing that relates to the location of the (local) repo, rather than some global setting. There might be something that you can do in the config file, but I'm not aware of anything.

--git-dir选项设置了存储库的路径,我将其作为与(本地)存储库位置相关的每个命令的内容来阅读,而不是某些全局设置。您可能可以在配置文件中执行某些操作,但我什么都不知道。

回答by d-_-b

Type pwd(print working directory) in linux. That will show you the directory that git clonewill download the files to.

pwd在 linux 中键入(打印工作目录)。这将显示git clone将文件下载到的目录。

By default the command prompt will be in the ~directory, which is the home directory.

默认情况下,命令提示符将位于~主目录中。

You can do cd /home/barackObama/websites/whitehouse; git clone......to clone into the whitehouse folder.

您可以cd /home/barackObama/websites/whitehouse; git clone......克隆到白宫文件夹中。

回答by szpadlicho

I found an easy way to do it here:

我在这里找到了一种简单的方法:

Press the right mouse button on the shortcut Git Bash and there change the start in field: enter your desired startup directory. Confirm. Done!

在快捷方式 Git Bash 上按鼠标右键,然后更改 start in 字段:输入所需的启动目录。确认。完毕!