Remove Old and Add New Git in Android Studio

Remove old and add new git in android studio

In order to drop the current git repository and create a new one you need to:

  • Go to the project's directory: cd PROJECT_DIRECTORY

  • Remove all the git specific files: rm -rf $(find . -name ".git*")

  • Initialize a new git repository: git init

This doesn't remove your project's files, only git configs

How do I remove Git from Android Studio project?

AFAIK, you can't remove it using Android Studio (there isn't a button for removing Git from a project). (Upd.: now incorrect, but the rest of this answer is still correct and functional, because Git still works the way this answer relies on).

In addition, .git is a hidden folder, meaning it's, well, hidden. ls needs -a to show it, and most file explorers don't show the folder.

Git is also not dependent on Android Studio in any way. All of the Git history is stored in the .git folder, and is usable with or without Android Studio.


There are (at least) three options.

The first method to removing it is fairly simple. So when you go into your project root, simply append /.git to the path.

So if your project root is D:/files/SomeProject, append the Git folder so the actual path becomes D:/files/SomeProject/.git and delete all the files and folders in there.

Alternatively, you can also use command prompt to delete it (note that this assumes you cd into the root dir first):

Windows:

rd /s /q ".git"

Linux/Mac:

rm -rf .git

And there's of course the option to show hidden folders, but this shows them everywhere. At least for Windows (10), search for folder (alternatively in an applicable language if your computer doesn't use English) and select "Show hidden files and folders". Scroll down until you find Hidden files and folders and select show. Other operating systems have different ways, most of which are likely covered somewhere on the internet (possibly a different Stack Exchange as well).


No matter which way you do it, now you just do git init and you've restarted it. All previous history will be gone, and the active tree will be the one left, so make sure you're on the right branch. When you delete the .git folder, there's no way to recover the history without re-pulling from a remote, and this assumes you have/use one.

Note that if your project is already uploaded to GitHub, you have to use the force flag (-f) for the push. Otherwise it'll just reject the push. Use the -f flag extremely carefully; it will cause problems for anyone else working on the repo (though this is only really a concern if there are others), and it will overwrite the current version of the repo stored on GitHub, or in any other remote you push to, and this is usually unrecoverable.

Replace GitHub repository with a new Android Studio project while preserving old commits

  1. Rename your current project folder (the new one you want to put on GitHub) to something like MyProjectBackup.

  2. In Android Studio, go to File > New > Project from Version Control > Git. Then log in with your GitHub username and password and select your old project's repository name from the list of your GitHub repos. Continue through the import wizard and you should end up with your old project in Android Studio. (Now, for example, your old project is in MyProject and your new project is in MyProjectBackup).

  3. Manually delete everything except for .git and .gitignore (and maybe the readme and license) from your MyProject project folder. (I had tried git rm -r * but I was getting errors.)

  4. From the command line in your MyProject folder run

    git add -u
    git commit -m "deleting old files before adding updated project"

    This will update the tracked files in the working tree for all the manual deletions you just made.

  5. Copy in all your new files from MyProjectBackup. Then run

    git add .
    git commit -m "new updated project"
    git push

Now your new project will be in GitHub and the old project's commit history will still be available.

Helpful reading

  • Git Basics - Recording Changes to the Repository

How to remove some files from commit checklist Android Studio?

First Things first:

You can add a .gitignore file to your repository, via terminal, by the commands below:

  1. In Terminal, navigate to the location of your Git repository.
  2. Enter touch .gitignore to create a .gitignore file.

Second, you should pay attention that, If you already have a file checked in and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

$ git rm --cached FILENAME

for more information on this follow https://help.github.com/en/articles/ignoring-files

How to change git repository using android studio

Go to your project directory and delete hidding .git folder like shown below

you will be disconnected to git.

Sample Image

Git best practice when replacing an Application completely

If you are going to use a public repository (i.e., not setting it to private), I don't see the issue with creating a new repository for the application.

That way you have the old version still there and a clean repository for the new application.

If you really want to replace the current project in the repo, there are possible ways of doing this. I have attached a link to another stack overflow question where they go in more detail about this.

Replace GitHub repository with a new Android Studio project while preserving old commits

Although this one refers to an Android Studio project specifically, the steps should still work for you.

EDIT (After seeing the update to your question):

It sounds like you are mainly modifying the code within the same files. If that is the case, then why not just make it a new commit, overwriting the old content of the files, but preserving the commit history?

How to remove remote origin from a Git repository

Instead of removing and re-adding, you can do this:

git remote set-url origin git://new.url.here

See this question: How to change the URI (URL) for a remote Git repository?

To remove remote use this:

git remote remove origin


Related Topics



Leave a reply



Submit