Git

Git Bash Change Directory

A folder is referred to as a directory. When working within Bash and utilizing the Git version control system, changing the directory is crucial because you always travel between several folders. There are two ways to modify the directory:

  • Using commands directly in Git Bash
  • Git Bash is launched in the chosen folder.

Before using the change directory command, we must first ensure that git is installed on our Linux or another UNIX-like operating system. To confirm this, we must check the git version; if the git version is displayed on the shell screen, it signifies that git is installed on our operating system. The command we used to check the git version is listed below; it is not necessary to run this command from the root or home directory:

$ git --version

Example 01: Check Current Working Directory

In this tutorial, we’ll look at how to change the current working directory in git bash using bash Linux commands on Kali Linux. The cd command in Git Bash allows you to move within the directories, probably from the current directory to another directory. The command “cd” means “change directory.” The cd command can be widely used in any Linux system’s shells (cmd) for the same purpose. Before using the “cd” instruction, we need to find out the current working directory of our system using the “pwd” instruction as shown.

$ pwd

To begin, make a list of the folders and files in the current directory. Open the Git repository in your local Bash system to get started. We used the “ls” command to see the list of all files and folders in the current directory in the screenshot. If we don’t know the precise name of the directory or folders we want to browse to, we can use the ls command to see all of the available directories and files in the current directory. As shown below, we have a single folder of git named ‘works’ in our current working directory, and many files exist in this folder. Let’s look at a few instances of how to move among git directories using various commands.

$ ls

Example 2: Change Git Directory

We’ll change the current directory from “/home/Linux/” to the “works” folder in the example below. To navigate our desired destination directory, we have been utilizing the keyword “cd” in the command line followed by the directory’s name, i.e., works. This moves us within the “works” directory. Using the “ls” instruction, we learned that it contains a text file and a folder project1 in it. If we want to move within the project1 folder, we have to use the same “cd” instruction as shown. While in the “project1” directory, we have used the “pwd” command to display the path of this current working directory.

$ cd works
$ ls
$ cd project 1
$ pwd

Example 3: Move from Git Folder to Home Directory

The “cd” command can be used to move back toward your home directory again when you are currently at another location, i.e., “works.” If you’re going straight to the home directory/folder, this command will come in handy. The first line reveals that we used the cd (change directory) command without specifying a directory name.

$ cd
$ pwd

Example 4: Move Back to Git Folder

To return to (or forward to) the previous directory/folder, you can also use the “cd -” instruction. If you accidentally browsed to the wrong directory, this command will be very convenient to use. We were previously in the works directory, so instead of typing the command with the whole path, we used the (cd -) command to return to it. Following the execution of this command, the operating system brings us to the works folder, as shown.

$ cd
$ pwd

In a hierarchy, the root directory is the first or topmost directory. If you’re in another directory, type “cd /” to move into the root directory as shown.

$ cd
$ pwd

Use “cd..” to go up one directory level. We’re currently in the project1 directory and want to get to the home directory, as shown in the example below. We’re using one level higher to navigate. When we used the first “cd..” command, we moved to the “works” directory, and when we used the second “cd..” command, we moved to the “home” directory.

$ cd . .
$ cd . .
$ pwd

We can travel numerous levels back using the “/” sign in the “cd” command after double dots, just like we used the simple “cd..” instruction to move back. The levels we want to move back are determined by the amount of dots (.) and forward slashes (/). Make sure to double-check that the number of levels provided in our command is correct.

$ cd . ./ . .
$ pwd

Example 5: Go straight to the Desired Directory

Simply input ‘cd’ at the instruction area along with the exact path to the directory to jump to the desired directory. We should use the path inside the quoted symbols, e.g., “/home/Linux/works/project1”. If no directory is detected, you may receive a directory not found error. See the example below for an illustration.

$ cd /home/Linux/works/project1
$ pwd

By using the cd command and the whole path of the directory, we can navigate directly to our home directory, which is “/home/Linux/” as per the output below.

$ cd /home/Linux/
$ pwd

Conclusion

In this article, we have learned what a directory is and how to switch from one git directory to another via bash commands. We looked at several ideas as well as a real-world example of how to put them into exercise. While operating any other Linux or UNIX-like operating system other than kali Linux, the change directory command is equally useful.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.