Remove Git from a Repo

Sometimes we want to remove the reference to existing repository details, such as cloned from github so that we can check the code in a different git repository or a different version control system. This article covers the steps that we can take to remove git references. If we clone a repo or fork a repo from git, the newly created repository will have git references as version control. If we want to remove the git references from the new repository then we need to delete all the git references. Once we remove the git references then it will not have any reference to old repository and we can use this project folder to create a brand new repository in git or any other version control system. This article talks about how to remove
The good thing is that all the git related information is stored in a folder .git . Normally we would have only one .git folder in the root of the repository, but we might have more than one .git folder based on how the repository is configured. Apart from .git folder we might have following git specific files, .gitignore: allows to include/exclude files to be synced to git remote server
.gitkeep: allows us to include an empty directory to be synced to git remote server
. gitattributes: allows us to ensure consistent git settings across the machine.
Essentially we need to delete all these git related folder/files that will make our repository clean and without any git version details. The following sections show how to achieve the same in a Windows environment and Mac environment


The rmdir or rd command will not delete/remove any hidden files or folders within the directory you specify, so we should use the del command to be sure that all files are removed from the .git folder. Open the command prompt Navigate to the project directory, i.e. - cd path_to_your_repository
del /F /S /Q /A .git

rmdir .git


Open a terminal and navigate to the directory of your project, i.e. - cd path_to_your_repository. And run the following commands
rm -rf .git
rm -rf .gitkeep
rm -rf .gitignore
rm -rf .gitattributes
Checking if you have any .git folder left
Go to the directory of your project and run the following command which finds the only the folder with the name .git and prints its path in the console.
find . | grep -i .git
Following section covers some of the most common in day to day life for a developer
git config
To configure common configuration values like user email id, name, console colors etc. For instance to set git to Foo use the following command.
 git config --global "Foo"
Initialize a repo
Create an empty git repo or reinitialize an existing one
git init
Clone a repo
Clone the foo repo into a new directory called foo on local workstation
git clone foo
Create new Branch
Now that we have a repo, we might want to create a branch and work on it
Checkout a different branch
Now that we have created our branch, lets say we want to checkout a different remote branch.
Switch branch
Switch between multiple branch
git switch -c NEW_BRANCHNAME
Staging Changes
Before we can push changes to remote repository we have to create commits. For creating commits we need stage these files by adding them. Using the following commands we can add a specific file "foo.js" or all the files modified since last commit.
git add foo.js
git add .
Unstaging Changes / Restoring Files
Maybe you accidentally staged some files that you don't want to commit.
git restore foo.js
git restore .
To commit the staged files execute the following commands.
 git commit -m " updated foo.js with mew controller"
Undoing Soft Commits
The following command will undo your most recent commit and put those changes back into staging, so you don't lose any work
git reset --soft HEAD~1
Undoing Hard Commits
The next one will completely delete the commit and throw away any changes. Be absolutely sure this is what you want
git reset --hard HEAD~1
Squashing Commits
If we want to combine multiple commits to a single commit so that it is easy to review. Following command will combine 5 different commits to a single commit. With a single commit message.
git rebase -i HEAD~5
Pushing Commits
Push the commits from the branch to remote branch.
git push origin BRANCH_NAME
Undo Last Push
If we want to undo changes that were pushed using last commit, we can use the following command to undo it.
git reset --hard HEAD~1 && git push -f origin master
Download objects and refs from another repository.
git fetch
Suppose we want to get the latest from the remote branch named "REMOTE_BRANCH" to local working branch. Execute the following command.
git fetch origin
git merge origin/REMOTE_BRANCH
Git Pull is the way to get the latest changes from remote branch and apply on local branch. It might cause merge issues if the local branch is not clean.
git pull origin/REMOTE_BRANCH
Git rebasing is the way to
git fetch origin
git rebase origin/REMOTE_BRANCH
Stash the changes in the working directory away, so that later they can be taken back.
git stash
git stash pop
git stash apply
git stash list