简体   繁体   English

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

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

So I just started using Git and GitHub. 所以我才开始使用Git和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 . 我在克隆存储库时注意到的一件事是,它总是下载到我的主文件夹/users/username Is there a way to change this? 有办法改变吗?

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 ,但是我知道我使用的方式不正确,因为当我尝试执行此操作时,Terminal中返回的内容是

       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... 所以一定是错的...

git clone will copy files to whatever your current directory is, unless you specify otherwise. 除非另有指定,否则git clone会将文件复制到当前目录中。 So if your current working directory is ~ , then 因此,如果当前工作目录为~ ,则

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

will copy files to ~/repo . 会将文件复制到~/repo

You can change this one of two ways: 您可以通过以下两种方式之一进行更改:

cd to 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创建您的/path/to/my/files/repo

Or, git clone can 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创建您的/other/path/to/repo

Type pwd (print working directory) in linux. 在Linux中键入pwd (打印工作目录)。 That will show you the directory that git clone will download the files to. 这将显示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...... 您可以执行cd /home/barackObama/websites/whitehouse; git clone...... cd /home/barackObama/websites/whitehouse; git clone...... to clone into the whitehouse folder. cd /home/barackObama/websites/whitehouse; git clone......克隆到whitehouse文件夹中。

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-dir option 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. --git-dir选项设置到存储库的路径,我正在按命令读取它与(本地)存储库位置相关的内容,而不是某些全局设置。 There might be something that you can do in the config file , but I'm not aware of anything. 您可能在config文件中可以执行某些操作 ,但是我什么都不知道。

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. 在快捷方式Git Bash上按鼠标右键,然后在其中更改开始位置:输入所需的启动目录。 Confirm. 确认。 Done! 做完了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM