4

I have a existing git repository with structure as follows:

Mobile_Application
-android-app
-ios-app
-paying-services

I want to check out as new project in my Android Studio version 3.0.1, only what contains in android-app folder. I tried to checkout whole project and I pulled the content of the all folders but could not build the android project at all. Is it possible to checkout as new project only the android-app folder?

Cœur
  • 32,421
  • 21
  • 173
  • 232
DimDim
  • 291
  • 1
  • 17

3 Answers3

2

OK, I know what you want and I will try to give you the most complete answer possible, you will perhaps learn something about git along the way:

Let us assume your repository has a remote, i.e. you cloned your repository from an existing remote repository via git clone <remote-url>. If not, make sure it is already "pushed" to some remote.

  1. Run the following command to configure the current repository to allow sparse-checkout:

    git config core.sparsecheckout true
    
  2. Then, inside git bash run the following command to specify the path(s) that you want to keep in your working tree (fancy terminology for "checkout"):

    echo android-app/ >> .git/info/sparse-checkout
    

    It is VERY IMPORTANT that you run this command inside the git bash prompt, DO NOT use PowerShell/CMD to do this, otherwise the generated text file might be encoded in Unicode with a BOM marker, and git will complain with a typical “error: Sparse checkout leaves no entry on the working directory”

    (You've been advised!)

  3. Then run the following command to update the index and working tree:

    git read-tree -mu HEAD
    

And voilà, your working tree should only contain the android-app/ folder from the root of your repository!

Fernando Espinosa
  • 3,772
  • 1
  • 24
  • 35
1

This is not formal way or proper but you can try this way open the terminal in android. What ever in this before commit it.

git branch 
*  master

git add .
git commit -m " your suitable comment"

To secure your current code.

git checkout -b new-branch
git branch
  master
* new-branch

remove the folders on new-branch which you mentioned don't need

-ios-app
-paying-services

now again commit it.

git add .
git commit -m "init"

Now if you have previous commits sha list and if you don't want to show all of them you can do like this.

git log --oneline -1
sha1
git reset --hard sha1

Now you are on the new-branch only what contains in android-app

Mobile_Application
-android-app

Your task is done! what you wanted but if you want the current branch. You can delete the previous branch.

git branch -d master
git branch
* new-master

if you want the current new-branch as master

git branch -m master
git branch
* master
badarshahzad
  • 1,097
  • 14
  • 23
1

Are these sub-proejcts connected somehow internally? Do they share something - tests, libraries, CI setup or any other stuff that you need to reuse in the different sub-projects? Do they depend on each other?

If the answers of these questions are "NO", then the thing that you are doing is wrong.

I am not absolutelly sure why do you have this single repo with sub-projects inside, but this is what I shall suggest you:

Option 1: Create three separates git repositories about each of the sub projects and just checkout the Android one. Then you gonna solve all of your issues. This is more professional and it keeps the things separated and simple. Plus that you don't need any weird git hub commands to checkout your Android sub-project. Later when all these sub-projects grow up and a lot of developers start working on them at the same time, having them like a one big repository, could create issues, restrictions, limitations and mess. Separate git repo for each project does not cost anything!

Option 2: Use some git tool to checkout the monolit repository, but not Android Studio directly (simple git terminal or SourceTree or any other git UI application (editor)). Once you checkout the repository, then open Android Studio, select to оpen existing project and navigate to the Android project directory (android-app). This will give you option to load in Android Studio only the Android part. Of course this has big disadvantages, because you are bypassing Android Studio git tools in this case, but you can still use your external git UI application (the one that you use to checkout the project at the beginning) to push, pull etc.

Option 3: Use something called "sparse checkout" in git. It could be achieved by this way:

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "android-app/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master

Take a look here to see the full proper explanation for git sparse checkout.

More info or different approaches you can get from here

If your client wants the single repo with all the projects inside, try to convince him, that this doesn't bring any benefits, but it just makes additional complexity and it is wrong.

I hope this will help you to solve the issue, but I personally recommend option 1. This is the right way and it doesn't cost anything, unless you have any arguments against that, but I doubt :).

Good luck

Stoycho Andreev
  • 5,468
  • 1
  • 23
  • 27