Add a new project to a branch of your Github repository. 将一个新项目添加到Git仓库的分支
2023-11-15 05:30:44  Git  >> Github

When you have a new Project B that can be the supplement to Project A, which has been existed in your Github repository. The best way is to add the B to the branch of A . Since they are two seperated project in your local PC, you would need a way to

  • Add B to the new branch of A
  • Keep A unchanged in the original master

Here are the general steps:

  1. Initialize the Local Repository: If your project is not yet under Git version control, you need to initialize a new Git repository. Open a terminal in your project directory and run the following command:
    1
    git init
  2. Add Files to the Repository: Use the following command to add your project files to the Git repository:
    1
    git add .
    This will add all new, modified, and deleted files to the staging area.
  3. Commit Changes: Run the following command to commit your changes to the local repository:
    1
    git commit -m "Initial commit"
    This will save your changes to the local repository.
  4. Connect to the Remote Repository: If your project needs to sync with a remote repository, you need to connect it to a remote repository. Run the following command:
    1
    git remote add origin <RemoteRepositoryURL>
    <RemoteRepositoryURL> is the URL of your remote repository.
  5. Create and Switch to a New Branch: Run the following command to create a new branch and switch to it:
    1
    git checkout -b <NewBranchName>
    Alternatively, if you are using a newer version of Git (2.23 and above), you can use the following command:
    1
    git switch -c <NewBranchName>
  6. Push to the Remote Repository: Finally, push the new branch to the remote repository:
    1
    git push -u origin <NewBranchName>
    This will push your new branch to the remote repository and set it as the default upstream branch.

This way, your new project is added to a new branch in the Git repository.