Home > database >  How can I preserve empty MASTER branch in order to be able to checkout and switch back from other br
How can I preserve empty MASTER branch in order to be able to checkout and switch back from other br

Time:03-16

I am writing some python script which for new branches downloads the content from some old SCM system (migration activities). I want to be able to checkout from master branch to that new branch, download the content to the new branch and then switch back to master branch. Please be aware that MERGE to master branch should not be performed at all!!!. I am executing all commands through the exec_cmd method which executes commands on the linux server.

Steps:

  • git clone repo manually on the server

  • execute script to create tmp file so that master is not empty exec_cmd(['touch', 'tmp.txt'])

  • checkout to new branch exec_cmd(['git', 'checkout', '-b', 'newBranch'])

  • download some files and folders using the custom logic...

  • try to return to master branch with git checkout master - FAILS!!!

    exec_cmd:

    ['git', 'checkout', 'master']
    ERROR: command "git checkout master" failed with return code 1
    Command output:error: pathspec 'master' did not match any file(s) known to git

What is the proper way to handle this this using the commands ( without merge and without git login through the script!)? I need to return back to master because from it again I need to create new branches for some other content so I need it to be empty (or almost empty with one tmp file which I thought will provide me a solution)!

Thanks

CodePudding user response:

Your fundamental misunderstanding is here:

execute script to create tmp file so that master is not empty exec_cmd(['touch', 'tmp.txt'])

In git, a branch does not contain files, it contains commits (or, more accurately, it points at commits). A commit does contain files - specifically, a snapshot of all the files in the repository. A commit doesn't have to have any new files, in fact it doesn't have to have any files in it at all, as "the repository at this point was empty" is a piece of information you can record.

The other confusion is that you seem to be trying to do this repeatedly, but once a branch exists, you can get back to it whenever you want. So you can create an empty master branch once, and simply switch to it each time.

To set up, as larsks suggested in the comments, you can run git commit --allow-empty -m 'This is just a placeholder' Or you could create a file and commit that, or anything you like really. Then in your script, git checkout master will return to that commit.

It's important to point out that git checkout will only change the state of files that are listed in one or both of the commits it's switching between. If you just create a file, it will be "untracked" - not part of any commit yet - and switching branch won't touch it.

Finally, to re-iterate: a branch does not contain files, it contains commits. So having created a branch in your script, you will need to commit something on that branch, otherwise as far as git is concerned that branch still just points at the same thing as master.

  • Related