Towards the end of first year and before the start of my second year, one of the problems I had while developing Simple Weather was I had formatted my laptop, and I got panicked as I no longer had the code I was working on. This depressed me. Luckily searching in my hard disk, I found an old backup, but it didn’t have newer code. So I had realized then that I needed to have backups of my app at regular intervals of time. But doing so would have taken up a lot of system space, and a lot of time to back it up too. I needed another solution
I was also fortunate to have been a member of our FOSS Club at that time (I still am), wherein I had learnt how to use Git. Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. In Noob terms, Git is used when you write a software, you periodically write down (commit, in terms of Git language) what all changes you did when you were writing the code, and push them to a repository (recommended to do periodically)
One thing while seeing newbies push their Android apps source code to GitHub, I realized that they only push the necessary Java files they used to get the app running. There is a disadvantage to this method, if someone else wants to run your app, he cannot do it only on the basis of the Java files, he will need the whole working project – starting from the Java files, all the way upto the Layout resource XMLs, Gradle files and the resources inside your project. So I will be telling how to push your existing Android projects to git properly.
PS. This is only for beginners. For intermediate and advanced guys, there are lots of things like CI and Code coverage which I will not be discussing here.
FIRST THINGS FIRST
- Learn how Git functions from a tutorial (I will prefer this one)
- Install git on your laptop (Google it guys, it is your best friend)
- Create a repository on GitHub or whatever site that supports hosting git repositories
- Name it according to the project (in our case, the app) that you are developing
INITIALIZE GIT IN YOUR LOCAL DIRECTORY
Now there will be a lot of build.gradle files in your project (depending on the number of modules you have, remember app shown here is only one such module, you may have many such more modules)
Your project root folder will be the one which contains a build.gradle, along with gradle.properties, settings.gradle and the gradle executable, depending upon which OS you are using. If you see all these files inside a folder, good that is your project root.
Hopefully you have installed Git on your system by now. If you are on Windows, Right click on your project root folder, and you will see an option to open “Git Bash Here”. Select that. If you are on Linux/Mac, right click on project root folder, select open in terminal.
Now given that you have not initialized git here before, follow the steps below:
- Given you are in the terminal/CMD, type
This will initialize a git repository on your local machine
- Get your project to build fine without errors before proceeding to the next step (It would have a bad impression to not getting your first commit build properly)
1git add .
Executing this command will properly add all the files in your project to your local git repository
1git commit -m "First Commit"
That is the general way to do a first commit, but there are no issues if you want a different Commit message.
- Copy the URL of the repo you created on GitHub. Then do these two steps
12git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME.gitgit push origin master
Congratulations, now you have your first backup of your app stored safely in the cloud (unless there is a physical calamity that causes harm to servers, but they will have backup servers everywhere)
- After you push your code into GitHub, it should probably look like this
HOW TO PROCEED FROM HERE
- Make sure to note of the small changes that you do inside your app. Whenever you change a Java file make sure you note it in your mind what you have done. Whenever you have changed an XML layout, note that in your mind too.
- Do not change too many files at once unless absolutely necessary
- Make sure the changes you do are able to successfully build your app
- Make sure the changes you do are able to run successfully on your Android device/emulator
- After you make sure that the changes you do have worked, commit them like you did above.
Following such general guidelines will help you when you work with not only Android projects, but with other projects as well.
Hope that this post provided a good insight to introduction to working with Git on Android.