On Git

Start a new code base without history from the existing one

If you want to create a new code base without any history from an existing Git repository, you can follow these steps:

  1. Clone the existing Git repository into a new directory:
    git clone <existing-repo-url> <new-directory>
    Replace with the URL of the existing repository, and with the name of the new directory where you want to create the new code base.

  2. Remove the Git directory from the new directory:

    cd <new-directory>
    rm -rf .git
    

    This will remove the .git directory, which contains the history and metadata for the Git repository.

  3. Initialize a new Git repository in the new directory:
    git init
    This will create a new Git repository in the current directory without any history or metadata from the previous repository.

  4. Add your new files and commit them:

    git add .
    git commit -m "Initial commit"
    

    This will add all new files in the directory to the new Git repository and create an initial commit with the message "Initial commit".

  5. Add a remote repository and push:
    If you want to push your new code base to a remote repository, you can add a new remote and push your changes:

    git remote add origin <new-repo-url>
    git push -u origin master
    

    Replace with the URL of the new remote repository.

Create, merge and push tags to the remote branch

  1. Create a new tag:
    To create a new tag, use the git tag command followed by the tag name. For example, to create a tag named "v1.0.0", run the following command:
    git tag v1.0.0
    This will create a new lightweight tag at the current commit.
  2. Push the tag to the remote branch:
    To push the tag to the remote branch, use the git push command with the --tags option. For example:
    git push --tags
    This will push all tags to the remote branch.
  3. Merge a tag to a branch:
    To merge a tag to a branch, use the git merge command followed by the tag name. For example, to merge the "v1.0.0" tag to the "master" branch, run the following command:
    git merge v1.0.0 master
    This will merge the changes from the tag to the "master" branch.
  4. Push the changes to the remote branch:
    To push the changes to the remote branch, use the git push command with the branch name. For example, to push the "master" branch to the remote repository, run the following command:
    git push origin master
    This will push the changes to the "master" branch to the remote repository.

Remove tags both from local and remote branch

  1. Delete the tag locally:
    To delete a tag locally, use the git tag command with the -d option followed by the tag name. For example, to delete the "v1.0.0" tag locally, run the following command:
    git tag -d v1.0.0
    This will delete the "v1.0.0" tag from your local repository.
  2. Delete the tag from the remote branch:
    To delete a tag from the remote branch, use the git push command with the --delete option followed by the tag name. For example, to delete the "v1.0.0" tag from the remote branch, run the following command:
    git push --delete origin v1.0.0
    This will delete the "v1.0.0" tag from the remote branch.
  3. Remove the tag reference from your local repository:
    To remove the tag reference from your local repository, use the git fetch command with the --prune-tags option. For example:
    git fetch --prune-tags
    This will remove the tag reference from your local repository.

Merge a branch as one commit into another branch

  1. Switch to the branch you want to merge into another branch:
    git checkout target_branch
  2. Merge the source branch into the target branch using the --squash option:
    git merge --squash source_branch
    This command merges the changes from the source branch into the target branch but does not create a merge commit. Instead, it prepares all the changes as staged but not committed.
  3. Review the changes and make any necessary modifications or cleanups.
  4. Stage all the changes for the final commit:
    git add .
  5. Commit the changes with a descriptive message:
    git commit -m "Merge source_branch into target_branch"
    This commit will contain all the changes from the source branch as a single commit on the target branch.
  6. Push the merged commit to the remote repository:
    git push origin target_branch
    By following these steps, you can merge the changes from a source branch into a target branch as a single commit, providing a cleaner commit history.
posted @ 2023-05-26 12:29  Ying‘s  阅读(7)  评论(0编辑  收藏  举报