How does Git put the code of a project into a new warehouse, but does not display the old commit records in the new warehouse

1. Abnormal error

Scenario: I want to put RuoYi's code into a new warehouse. After putting it in, this warehouse will display all the previous submissions of RuoYi. How can these old submissions not be displayed?


Sometimes, we may need to put the code of a project into a new warehouse, but we don't want to display the old commit records in the new warehouse. This could be because old commits contain some sensitive information, or because we just want to keep the latest code history in the new repository.

Two, the reason

When we need to put the code of a project into a new warehouse, there may be the following situations:

  1. We want to copy the code from a public repository to a private repository to keep the code safe.

  2. We want to copy a project's code from a developer-only repository into a public repository so others can view and use the code.

  3. We want to copy a project's code from an old repository to a new one, and keep only the latest commit history, in order to make the new repository cleaner and easier to manage.

In these cases, we can use steps like the ones mentioned below to accomplish the task. But it should be noted that before performing these operations, we should back up the code of the original warehouse to prevent data loss. At the same time, we also need to ensure that before making any changes, we carefully read and understand the relevant operation guidelines of Git to ensure that we will not accidentally delete or change important data.

3. Solutions

If you want to put ruoyi's code into a new warehouse, and don't want to show the old commit records, you can use the following steps:

  1. Create a new blank repository, for example using GitHub or GitLab to create a new repository.

  2. Clone ruoyi's code locally.

  3. Enter ruoyi's local warehouse directory and use the following command to convert it into a new warehouse:

    git init
    git remote add origin <新仓库的远程地址>
    git fetch --all
    git reset --hard origin/master
    

    These commands will convert ruoyi's local repository to a new repository and associate it with the new remote repository.

  4. Clear old commit records with the following command:

    git checkout --orphan latest_branch
    git add -A
    git commit -am "Initial commit"
    git branch -D master
    git branch -m master
    git push -f origin master
    

    These commands will create a new branch latest_branch and add all files to that branch. It will then create a new commit which will serve as the initial commit for the new repository. Finally, it will delete the old master branch, rename the latest_branch branch to master branch, and force push it to the new remote repository.

This way, you can put your project's code into a new repository without showing old commits.


These codes are Git commands to put the project's code into a new warehouse without showing the old commit records. Below is a detailed explanation of each command:

  1. git init: Initialize a Git repository.

  2. git remote add origin <新仓库的远程地址>: Associate the local repository with the new remote repository.

  3. git fetch --all: Get all branches and commit records from the remote repository.

  4. git reset --hard origin/master: Reset the code of the local warehouse to the latest submission of the master branch of the remote warehouse.

  5. git checkout --orphan latest_branch: Create a new branch latest_branch.

  6. git add -A: Add all files to the staging area.

  7. git commit -am "Initial commit": Create a new commit as the initial commit for a new repository.

  8. git branch -D master: Delete the old master branch.

  9. git branch -m master: Rename the latest_branch branch to the master branch.

  10. git push -f origin master: Force push the master branch to the new remote repository.

The purpose of these commands is to convert the project's local repository into a new repository and to clear the old commit records so that only the initial commits are displayed in the new repository.

Guess you like

Origin blog.csdn.net/qq_46207024/article/details/131242060