简体   繁体   中英

How do I use the local git in VS Code, when it is attached to a Docker container?

I am new to VS Code. I have "attached" VS Code to an already running Docker container. My source code resides in the local host machine. The source code folder is mapped to a folder inside the container using "VOLUME /project-home". In VS Code, I am able to open the files within the container and edit/save them.

Now, I am trying to use git which is installed on my local machine within VS Code. But when I click on the "Source Control" icon it says "A valid git installation was not detected" and "Install it or configure it using the 'git.path' setting."

There is some documentation here which I was not able to understand:( https://code.visualstudio.com/docs/remote/containers#_attaching-to-running-containers

I could possibly install git in the container and VS code should be able to recognise it but I want to use the local git. Any idea how to do it?

You don't need git in your Docker container. Make sure your git installed on MacOS is correctly added to PATH.

Here is instruction how to do it: https://stackoverflow.com/a/1835854/6940684 .

Replace /usr/local/git/bin with your git installation path if it's different.

After trying different things, ultimately I installed git inside the docker container. VS Code is now able to recognise it and also the changes made to the code. I did not think of it earlier, but the local git is {obviously} able to see the commits made inside the container. This is because the local folder is mounted as a volume inside the container.

If you're using docker compose, run

docker-compose exec [CONTAINER-NAME] apt-get update
docker-compose exec [CONTAINER-NAME] apt-get install git

After install, check the git version with

docker-compose exec [CONTAINER-NAME] which git

Reload Vs code and you're good to go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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