英文:
i want to work on different branches in git in my local repository which have different branches from origin set as upstream
问题
我正在学习如何使用GitHub。
我想在我的本地存储库中处理不同的Git分支,这些分支与上游设置的不同分支不同。
我是否必须在本地创建不同的文件夹来跟踪这些分支,或者我是否可以只使用一个本地存储库来查看不同分支的代码,如果可以,如何做到?
英文:
I am learning to use GitHub.
I want to work on different branches in git in my local repository which has different branches from origin set as upstream.
Do I have to create different folders for the branches in local or my computer to keep track of them or can I view codes of different branches using just one local repository and how?
答案1
得分: 8
有几种方法:
git checkout
使用 git checkout <branch>
可以将文件夹的内容更改为所需分支中的文件。每次您的“根”文件夹只能包含来自单个分支的内容。
git worktree
# 为不同的分支添加“另一个”目录
git worktree add <second path>/<branch name>
这将在您的计算机上创建另一个文件夹,允许您同时在不同的分支上工作。
创建新的工作树
在工作树文件夹内创建新分支
git worktree -b <branch name> <path>
删除工作树
告诉 git 删除工作目录副本
git worktree delete ...
列出工作树
英文:
There are several ways
git checkout
Using git checkout <branch>
you "change" the content of your folder to reflect the files in the desired branch. Your "root" folder can contain content from a single branch every time
git worktree
# Add "another" directory for a different branch
git worktree add <second path>/<branch name>
This will create another folder on your computer which allows you to work on different branches simultaneously.
### Creating new worktree
# create new branch inside the worktree folder
git worktree -b <branch name> <path>
### Removing worktree
# Tell git to remove the workdir copy
git worktree delete ...
Listing worktree
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论