Home > database >  Shifting from one git repo to another via command line
Shifting from one git repo to another via command line

Time:09-22

Say I have two repositories, which I regularly interact with. For example, repo1 and repo2. Let's say I've just committed and pushed a change to repo1, but now I've got work to do in repo2.

Behind the scenes, how does git realize that I'm no longer interacting with repo1 but now with repo2? (Is it as simple as navigating to the directory where repo2's local branch is located?) Do I need to use some command line language to tell git that I'm shifting focus to repo2?

CodePudding user response:

By default, Git performs autodiscovery for the repository based on the directory it's invoked in. When performing autodiscovery, it looks in the current directory to see if there's a .git folder or gitlink, and if there isn't, repeats the process for the parent directory, and so on until it hits the root of the file system or a mount point. If it finds a repository, it sets the directory containing the appropriate .git entry as the root of the working tree, and the .git folder as the git directory.

You can choose to have autodiscovery work based on a different directory by using the -C flag, which must precede the command name (e.g., git -C DIR status), and you can also specify directories explicitly using command-line options or environment variables. These can be useful when you want to manipulate a submodule or other repository but don't want to change directories.

However, most of these are unusual situations, and in most cases, Git determines the repository based on the current directory. So shifting focus to repo2 is as simple as changing into some directory in repo2. On Unix, it's as simple as something like cd ~/checkouts/repo2.

  • Related