简体   繁体   English

如何使用 git bash 更改文件夹?

[英]How to change folder with git bash?

My default git folder is C:\\Users\\username\\.git .我的默认git文件夹是C:\\Users\\username\\.git

What command should I use to go into C:/project ?我应该使用什么命令进入C:/project

The command is:命令是:

cd  /c/project/

Tip:小费:
Use the pwd command to see which path you are currently in, handy when you did a right-click "Git Bash here..."使用pwd命令查看您当前所在的路径,当您右键单击“Git Bash here...”时很方便

Go to the directory manually and do right click → Select 'Git bash' option.手动转到目录并右键单击→选择“Git bash”选项。

Git bash terminal automatically opens with the intended directory. Git bash 终端会自动打开目标目录。 For example, go to your project folder.例如,转到您的项目文件夹。 While in the folder, right click and select the option and 'Git bash'.在文件夹中,右键单击并选择选项和“Git bash”。 It will open automatically with /c/project .它将使用/c/project自动打开。

Here are the steps I followed:以下是我遵循的步骤:

  1. In bash, check in which directory you are by using the command:在 bash 中,使用以下命令检查您所在的目录:

     $ pwd
  2. copy the URL of the directory you want to go like after using the first command ( PWD ) I got:在使用我得到的第一个命令( PWD )后,复制你想要去的目录的 URL:

     $ /c/Users/yourUsername
  3. Now I want to change this to the directory of c drive and folder MyPictures .现在我想将其更改为c驱动器和文件夹MyPictures的目录。 To do that, I will go the directory of MyPictures , copy the URL, and paste it in the Git bash.为此,我将转到MyPictures目录,复制 URL,并将其粘贴到 Git bash 中。 However, before that:然而,在此之前:

    bash 中的语法更改

    C:\\MyPicture becomes $ cd /C/MyPicture (backslashes are replaced with slashes) C:\\MyPicture变为$ cd /C/MyPicture (反斜杠替换为斜杠)

  4. if the folder name is having some spaces like (my program) then you need to enclose it in double quotes like:如果文件夹名称有一些空格,例如(我的程序),那么您需要将其括在双引号中,例如:

     $ cd "C:\\Program Files"
  5. Remember to change directory you just need to copy the requiredUrl and paste that in bash with double-quotes like:请记住更改目录,您只需要复制requiredUrl并将其粘贴到 bash 中,并使用双引号,例如:

     cd "required URL"

Note: URL required with slashes.注意:需要带斜杠的 URL。

How to change folders in Git Bash如何在 Git Bash 中更改文件夹

As Bob mentioned , you can change directories with cd :正如Bob 所提到的,您可以使用cd更改目录:

cd /c/project

If you have a Windows path with backslashes, enclose the path in double quotes:如果您有带反斜杠的 Windows 路径,请将路径用双引号括起来:

cd "C:\project"

Tips提示

  • You can check the current folder with pwd .您可以使用pwd检查当前文件夹。

  • If the path contains spaces, you will need to use quotation marks.如果路径包含空格,则需要使用引号。 ( cd "C:/Program Files" ) ( cd "C:/Program Files" )

  • On Windows, you change the default starting directory for Git Bash.在 Windows 上,您可以更改 Git Bash 的默认起始目录。

    • Right click git-bash.exe , select Properties , open Shortcuts , and change Start in: to your most commonly used folder.右键单击git-bash.exe ,选择Properties ,打开Shortcuts ,并将Start in:更改为您最常用的文件夹。 ( screenshot ) 截图
  • The cd command can be memorized as " change directory ". cd命令可以记忆为“更改目录”。

See also也可以看看

pwd : to check where you are (If necessary) pwd :检查你在哪里(如有必要)

cd : change directory cd : 改变目录

In your case if I understand you, you need:在你的情况下,如果我理解你,你需要:

cd c/project

Your Question is :你的问题是:

My default git folder is C:\\Users\\username.git我的默认 git 文件夹是 C:\\Users\\username.git

But I want to go into c:/project但我想进入 c:/project

What command do I need to get into that?我需要什么命令才能进入它?

Since you have asked primarily about gitbash which is Linux based (Terminal), there are differences in commands when compared with Command Prompt of Windows.由于您主要询问基于Linux(终端)的gitbash,因此与Windows的命令提示符相比,命令存在差异。 We'll discuss gitbash (Terminal) commands only.我们将只讨论 gitbash(终端)命令。

1.First of all we must understand that command line(In Windows) and Terminal(In Mac) always points to some folder on storage Drives . 1.首先我们必须明白命令行(在Windows中)和终端(在Mac中)总是指向存储驱动器上的某个文件夹。

To check towards what directory it is pointing to at any given time.在任何给定时间检查它指向的目录。 You need to type the command: pwd "an acronym for 'Print Working Directory' ".您需要输入命令: pwd “'打印工作目录'的首字母缩写词”。

Linux 中的 pwd 命令

  1. There is a command ls which gives us information about the folders and files in a particular directory.有一个命令ls为我们提供有关特定目录中的文件夹和文件的信息。 This is quite a handy command and often used to know about the file structure.这是一个非常方便的命令,通常用于了解文件结构。 In my answer I will make use of this also.在我的回答中,我也将利用这一点。

在此处输入图片说明

  1. To traverse along the folder tree we make use of yet another very important command know as cd which stands for change directory.为了沿着文件夹树遍历,我们使用了另一个非常重要的命令cd ,它代表更改目录。 And your question has the answer within this cd command only.你的问题只有在这个cd命令中才有答案。

Here are some of the ways to traverse along the folder tree:以下是沿文件夹树遍历的一些方法:

3a) cd command let's us traverse to child directory. 3a) cd命令让我们遍历到子目录。 Kindly check the snapshot.请检查快照。 转到子目录

3b) Now to traverse back into the parent directory, we make use of cd .. command: Please check the Image below: 3b) 现在要遍历回父目录,我们使用cd ..命令: 请检查下图: 在此处输入图片说明

By Using the above two steps we can easily solve your Query:通过使用以上两个步骤,我们可以轻松解决您的查询:

A) Currently you are in : C:\\Users\\username.git A) 目前你在:C:\\Users\\username.git

So, doing cd .. will point the Terminal towards Users folder.因此,执行cd ..会将终端指向用户文件夹。

在此处输入图片说明

B) Again Typing cd .. will make Terminal to point towards C Drive. B) 再次键入 cd .. 将使终端指向 C 驱动器。

在此处输入图片说明

C) Now doing ls at this point will let you know about all the folders and files in C drive. C) 现在执行ls会让你知道 C 盘中的所有文件夹和文件。

Check if there is a project folder, Then simply for the last time type the command:检查是否有项目文件夹,然后只需最后一次键入命令:

cd project光盘项目

And Walla you are have traveled so far to reach to your destination.和瓦拉你已经走了这么远才能到达你的目的地。 Congratulations.恭喜。

在此处输入图片说明

Note: If the project folder is not created with C drive, simply write the command mkdir project and it will be created.注意:如果项目文件夹不是用C盘创建的,直接写命令mkdir project就可以创建了。 Then follow the above steps to play around.然后按照上面的步骤来玩。

4) There is one more straight forward quick solution to your problem in particular: 4)特别是有一种更直接的快速解决方案可以解决您的问题:

Wherever the terminal is pointing.终端指向的任何地方。 Simply write the command:简单写下命令:

4a) cd / It will point to default root folder. 4a) cd /它将指向默认的根文件夹。

Then type the command : cd /c/ to point towards c directory.然后键入命令: cd /c/指向 c 目录。 Then simply go to child directory, which in your case is project directory by typing:然后只需通过键入以下内容转到子目录,在您的情况下是项目目录:

cd project光盘项目

在此处输入图片说明

And you are good to go: ENJOY :)你很高兴去:享受:)

From my perspective, the fastest way to achieve what you're looking for is to change " Start in " value.从我的角度来看,实现您正在寻找的最快方法是更改​​“开始”值。

To do that, right-click on git-bash.exe , go to Properties and change Start In value to the folder you want.为此,请右键单击git-bash.exe ,转到“属性”并将“开始于”值更改为您想要的文件夹。

Right clicking a specific folder can help ease your pain than just by typing the whole directory.右键单击特定文件夹可以帮助减轻您的痛苦,而不仅仅是键入整个目录。 Right click + clicking s or Right click and then click "GIT bash here"右键单击 + 单击 s 或右键单击,然后单击“GIT bash here”

Hope this seems helpful希望这看起来有帮助

Simply type cd then copy and paste the file path.只需键入cd然后复制并粘贴文件路径。

Example of changing directory:更改目录示例:

更改目录的示例

I wanted to add that if you are using a shared drive, enclose the path in double quotes and keep the backslashes.我想补充一点,如果您使用的是共享驱动器,请将路径用双引号括起来并保留反斜杠。 This is what worked for me:这对我有用:

$cd /path/to/"\\\share\users\username\My Documents\mydirectory\"

最快的方式$ cd "project"

To move from c drive to other drive(D or E)use this command----->要从 c 驱动器移动到其他驱动器(D 或 E),请使用此命令----->

cd "your path" Now hit enter. cd "你的路径" 现在按回车键。 Check your path using $pwd.使用 $pwd 检查您的路径。 Your path has been change from one directory to other.您的路径已从一个目录更改为另一个目录。

Ex---> cd "D:\\WEB_DEV\\HTML_CSS_projects\\TouristVisitors_LandingPage".例如---> cd "D:\\WEB_DEV\\HTML_CSS_projects\\TouristVisitors_LandingPage"。 note - this is my path, your path should be different.注意 - 这是我的路径,你的路径应该不同。

cd c:/用户/用户名/文件夹/子文件夹

只需右键单击所需的文件夹并选择 git-bash Here 选项,它会将您定向到该文件夹​​并开始工作,希望它能正常工作。

if you are on windows then you can do a right click from the folder where you want to use git bash and select "GIT BASH HERE".如果您在 Windows 上,则可以从要使用 git bash 的文件夹中右键单击并选择“GIT BASH HERE”。 在此处输入图片说明

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

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