kaaron
kaaron

Reputation: 13

Switch branches w/o changing files in working directory

I have Windows scripts (i.e., PowerShell, VBScript, Batch) that are already "live" and used in production today, but I'd like to put them under git version control.

Each script resides in its own folder, and they're called from that location.

Examples of these calls are as follows:

powershell C:\Scripts\MyScript1\MyScript1.ps1
cscript C:\Scripts\MyScript2\MyScript2.vbs
C:\Scripts\MyScript3\MyScript3.bat

When necessary, I'd like to be able to branch and do code changes without breaking production processes.

Using git, how can I switch branches without changing files in the working directory?

Let's assume I run the following commands for MyScript1

cd C:\Scripts\MyScript1\
git init
git add .
git commit -m "baseline prod script"
git branch NewFeature
git checkout NewFeature

Anything I do from this point forward in the new branch affects the script actually being used in production. Can the branch be redirected to a different folder or something while I'm working on updates?

At the end of the day, I'd like to be able to easily make code updates, using git for version control, but have the branching configured so that my code changes don't impact what's in production until I do the merge.

Upvotes: 1

Views: 515

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21938

git allows for additional worktrees in a repo.

You can keep your files on the branch they're currently on, and add a new worktree in a different directory

git worktree add <path> <committish>

(the <committish> part is usually a branch or a commit hash)

Then you can checkout either branch on either worktree, for example keep your main worktree on the current branch you have to keep as is, and checkout your dev/work branch on the new added worktree, pull, push, do whatever you need, all without impacting the existing filesystem.

It's a good alternative to just recloning the repo (which could have been the naive route, absent the worktrees feature)

Upvotes: 1

Related Questions