简体   繁体   中英

git create new branch from a previous commit in repo with submodules

I have a git repo with multiple submodules. On "master" branch of parent repo, lets say the history looks like A -> B -> C -> D -> E -> F (HEAD). My parent repo is setup such that the submodules are pointing to a detached HEAD (like this "HEAD detached at "). I want to go back to a previous commit on "master" branch of parent repo, commit "C" and update one of the submodules to a more recent commit and push this new state of the "master" branch of parent repo to remote.

How do I go about this?

I have the following steps but I am not sure if that these are the right steps.

git reset --hard HEAD~3 (which takes me to commit C)
git submodule update --init
cd path/to/submodule ; git checkout <commit_id> ; cd -
git add path/to/submodule ; git commit
git push origin master

Is this the correct procedure? Would I need to use the --force option in the last step. I looked up for similar question on how its done in a regular git repo without submodules but I am not sure of how to do it with repo with submodules. New to working with submodules.

My parent repo is setup such that the submodules are pointing to a detached HEAD

Well, it's hardly "setup". Detached HEAD is the natural state of submodules. Most of the time most submodules are in detached HEAD state except when you deliberately fix it; and even with manual fixes they will probably switch to detached HEAD rather soon.

Is this the correct procedure?

Yes, looks good.

Would I need to use the --force

Yes because you move the branch pointer in a non-fast-forward way.

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