Showing posts with label git admin. Show all posts
Showing posts with label git admin. Show all posts

Tuesday, November 26, 2024

How to Copy Git Repository Without History

There are several methods to do this using git clone, git push or using git archive. But I personally prefer the one using git clone.

Objective is to copy repo 1 which is source repo to a new Repo which is NewRemote repo with out commit history.

Precautions before you proceed with this:

  1. Ensure you have write access to the repository.
  2. Backup any important local changes before proceeding.
  3. This will permanently remove the old commit history.
  4. Collaborators will need to re-clone the repository.

Here are step by step git examples for this specific repo

# 1. Clone the source repository
git clone https://github.com/inagasai/SourceRepo.App.git

# 2. Enter the cloned repository directory
cd vGlence.App

# 3. Verify current branches
git branch -a

# 4. Checkout master branch
git checkout master

# 5. Create a new branch without history
git checkout --orphan clean-main

# 6. Add all files to the new branch
git add .

# 7. Commit the files with a new initial commit
git commit -m "Initial commit - reset repository history"

# 8. Delete the old main branch (if it exists)
git branch -D main 2>/dev/null

# 9. Rename current branch to main
git branch -m main

# 10. Remove the original remote
git remote remove origin

# 11. Add the original repository as a new remote
git remote add origin https://github.com/inagasai/NewRemote.App.git

# 12. Force push to overwrite the remote repository
git push -f origin main
  

Detailed Breakdown of the outcome:

  1. This process creates a new branch with no commit history.
  2. It adds all existing files to a new initial commit.
  3. Force pushes to overwrite the remote repository.
  4. Removes all previous commit history.

Hope this helps.

Tuesday, June 27, 2023

Git Cheat Sheet: Essential Commands for Version Control Mastery

Git is a powerful and widely used version control system that enables developers to efficiently manage their codebase and collaborate on projects. However, mastering Git can be a daunting task, especially for beginners. To ease your learning curve, we've prepared a comprehensive Git cheat sheet that includes the most essential commands you'll need to navigate through Git's functionalities. Whether you're a novice or an experienced developer, this cheat sheet will serve as a handy reference to help you streamline your version control workflow.

Git Configuration:

  • git config --global user.name "[name]": Set your username for Git.
  • git config --global user.email "[email address]" : Set your email address for Git.
  • git config --global color.ui auto: Enable colorful output in Git.

Repository Creation and Cloning:

  • git init: Create a new Git repository in the current directory.
  • git clone [repository URL]: Clone an existing repository to your local machine.

Basic Workflow:

  • git add [file]: Add a file to the staging area.
  • git commit -m "[commit message]": Commit your changes with a descriptive message.
  • git status: Check the status of your repository.
  • git log: View the commit history.
  • git diff: Show the differences between your working directory and the last commit.

Branching and Merging:

  • git branch: List all branches in the repository.
  • git branch [branch name]: Create a new branch.
  • git checkout [branch name]: Switch to a different branch.
  • git merge [branch name]: Merge a branch into the current branch.
  • git stash: Temporarily save changes that you don't want to commit yet.

Remote Repositories:

  • git remote add [remote name] [remote URL]: Add a remote repository.
  • git push [remote name] [branch name]: Push your local changes to a remote repository.
  • git pull [remote name] [branch name]: Fetch changes from a remote repository and merge them into your local branch.

Collaboration:

  • git branch -r: List remote branches.
  • git fetch: Download objects and refs from a remote repository.
  • git branch -d [branch name]: Delete a branch.
  • git clone --branch [branch name] [repository URL]: Clone a specific branch of a repository.

Undoing Changes:

  • git reset [commit]: Un stage commits, preserving changes.
  • git revert [commit]: Create a new commit that undoes changes from a previous commit.
  • git checkout -- [file]: Discard changes in a specific file.

This Git cheat sheet provides you with a quick reference to the most commonly used commands for version control. By familiarizing yourself with these commands, you'll be able to navigate Git's functionalities with ease, collaborate effectively, and maintain a clean and organized codebase. Remember, practice makes perfect, so don't hesitate to experiment and explore additional features and options available in Git. Happy coding!

Please consider this cheat sheet as a starting point for your Git journey, and continue to expand your knowledge by exploring additional resources and documentation.

Sunday, June 13, 2021

Git: How to set Git User Name and User email Globally & repository specific

Usually when we install Git, we typically configure your global username and email address after installing Git. However, you can do so now if you missed that step or want to make changes. After you set your global configuration, repository-specific configuration is optional.

Git configuration works the same across Windows, macOS, and Linux.

To set your global username/email configuration:

Open the command line.

-- set user name
$ git config --global user.name "First Name Last Name"
-- set user email
$ git config --global user.email "useremail@gmail.com"

To set repository-specific username/email configuration:

From the command line, change into the repository directory.

--Set your username
$ git config user.name "FIRST_NAME LAST_NAME"

--Set your email address
$ git config user.email "useremail@gmail.com"

--Verify your configuration by displaying your configuration file
$ cat .git/config

Hope this helps!