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:
-
Clone the existing Git repository into a new directory:
git clone <existing-repo-url> <new-directory>
Replacewith the URL of the existing repository, and with the name of the new directory where you want to create the new code base. -
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.
-
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. -
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".
-
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
- 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. - 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. - 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. - 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
- 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. - 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. - 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
- Switch to the branch you want to merge into another branch:
git checkout target_branch
- 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. - Review the changes and make any necessary modifications or cleanups.
- Stage all the changes for the final commit:
git add .
- 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. - 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.