How to make cargo build a certain git branch

When I checkout a rust project, it will build the branch main if I use cargo build. If I switch git to another branch, cargo build still builds the main branch. I have read the documentation, and searched the internet, but as far as I can see, there is little documentation and no mention of how to make cargo use another branch.

The way I suspected it to work, is cargo to use the active branch.

My question is if I can make cargo use another git branch, and if so, how.

I did find documentation about how to use a git branch for dependencies. But this is about the current project.

How exactly are you checking out the other branch ?

git clone <remote>
git checkout <branch>
cargo build

That should be enough. Cargo mostly doesn't use information from your VCS, it used to, for the maintainer name, not anymore.

I normally got clone repositories, which then contain all branches, and have the main branch checked out.
If I change branch (git checkout ), cargo build still builds from main.
The only way to build from my dev branch, is to remove the git directory and git clone … -b dev, which then only checks out the branch dev, which then obviously is used by cargo build.

If you want to troubleshoot this issue you should give the exact steps you take. The issue is almost surely in one of those steps.

Cargo only use the source files currently in the src directory, the Cargo.toml, and the artefacts in the target directory, to decide what to build.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.