Android Tutorial Using a Git Repository (Version Control) for Beginners

git logo.png


What is Version Control?

Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.
(read more...)

What is Git?
By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.
(read more...)

What is a Repository?
In software development, a repository is a central file storage location. It is used by version control systems to store multiple versions of files. While a repository can be configured on a local machine for a single user, it is often stored on a server, which can be accessed by multiple users.
(read more...)

How do I get started?
First, you'll need to install Git for Windows.
Once installed, you can already start using it locally, but if you want to safely secure your files online, it's time now to choose an online repository.
There are many available options such as GitHub, GitLab or Bitbucket.
For this tutorial, we'll be rolling with GitLab, since it allows you to have free private repositories.

How do I use Git on Windows?
Right-click you project's folder. Click "Git Bash here".
context menu.png

You are now using a Bash shell (Linux), on Windows. Pretty cool, eh?
Alright, back to the point!

To turn your project folder into a local git repository, simply type:
B4X:
git init
Now let's configure it. This is a one-time step. Check the video below at 12:30.
B4X:
git config --global user.name 'Your Name'
git config --global user.email '[email protected]'
Time to make our first backup, or in Git lingo terms, our first "commit".
B4X:
git status
git add *
git status
git commit -m 'Yay!! My first commit!!! Woohooo!'
git status
gitk

'Note: The commands "git status" and "gitk" are not really necessary, as they're for visualization purposes only.

Quick video tutorial (skip to 7:22):

What is .gitignore and how to use it?
The file ".gitignore" (yes, it starts with a dot!) is a file you can create to ignore any files you decide not to be necessary in a backup.
This generally applies to the generated binaries.

Usage:
- Create an empty file named ".gitignore" in your project folder. You can use Notepad.

File content:
B4X:
#Git will ignore the following files/folders:
bin/
gen/
src/
*.apk
*.dex
*b4a.meta

How does GitLab work?

Once you register your new account, you'll be presented with a quick-start guide.

If necessary, however, you may prefer to follow the instructions presented in this video:

How do I push my commit into the online repository?
First you'll need to add the remote repository, for example:
B4X:
git remote add origin [email protected]:username/projectname.git
Then, it's as easy as:
B4X:
git push origin master

Whaaaaaaat? This is too complicated!!!!!
Not really. Git can be a very complex system,
but only if you decide to really dive into it.
Stick with the basic commands and you'll be just fine. :)

TL;DR:
- Install Git for Windows and get an online repository.
- Git Bash on your project folder
- Create '.gitignore' if necessary
- Configure Git Bash to work with your online repo.
B4X:
git init
git config --global user.name 'Your Name'
git config --global user.email '[email protected]'
git remote add origin [email protected]:username/projectname.git
- Daily use:
B4X:
git add *
git commit -m 'Your message here.'
git push origin master
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
Thanks!

Related to it, really worth looking at SourceTree, a graphic (and free) Git client. I used it for a large distributed team project, and currently for some cloned github repos for my own projects. It allows to do everything from a more intuitive environmet. I think I am only using a 20% of it and it is more than enough for my daily needs (until the day in which I really break something and will need to learn the advanced command-line features).
 

moster67

Expert
Licensed User
Longtime User
Thank you.
I used Git for Windows some time ago but then I started copying important files to external backups instead although I much prefer gits. I will re-start using git again.

Anyway, a good idea is also to configure the "gitignore" file if you want to avoid copying static data which are rarely changed.
I added the following lines:

#B4a
bin/
gen/
src/
*.apk
*.dex
*b4a.meta

Another great thing with gits is that you can see changes made in code between different versions and you can also recover code-files in case you accidentally saved something you did not mean to do.
 

wonder

Expert
Licensed User
Longtime User
Anyway, a good idea is also to configure the "gitignore" file if you want to avoid copying static data which are rarely changed.
Thanks!! Added!

Another great thing with gits is that you can see changes made in code between different versions and you can also recover code-files in case you accidentally saved something you did not mean to do.
Yes, that's the power of Git! :)
 
Last edited:

wimpie3

Well-Known Member
Licensed User
Longtime User
I've used GIT already multiple times in previous projects but I've always stopped after a certain time and went back to versioned files on my NAS.

If there is someone who deserves to be fired, it should be the programmer who invented GIT. Hard-to-use, command line based... in short: everything good software shouldn't be!

I'm sure it is really useful when you have to work with multiple persons on the same project, but I have my doubts for single-user projects.
 
Last edited:

MaFu

Well-Known Member
Licensed User
Longtime User
I'm using Mercurial, It's more or less the same as Git (but imho the better choice on windows). And also command line based. And this is one of the best features of git and mercurial. For the normal tasks i use TortoiseHg (or TortoiseGit for Git) and never have to use the command line. But for special needs i can create batch files and automate tasks which i normally never can do with a windows only tool.
And its not only useful for developer groups, a single user also have great benefits from a version control system.

If a GUI is installed (like Tortoise) all tasks are very easy and mostly done with one or two mouse clicks.
 

wonder

Expert
Licensed User
Longtime User
If there is someone who deserves to be fired, it should be the programmer who invented GIT.
Git was created by Linus Torvalds, you know, the guy who wrote Linux, one of the fathers of modern computing... so yeah, he should totally fire himself for designing such a tool.

Hard-to-use, command line based... in short: everything good software shouldn't be!
Ugh!! Typing commands into a console window, what a horrible thing to do!! Almost as horrible as having to type several lines of code just to create some stupid Android app. Who the hell does that??? Good software is all about tap-tap-click copy-paste, right? :cool:
 
Last edited:

jimmyF

Active Member
Licensed User
Longtime User
Git was created by Linus Torvalds, you know, the guy who wrote Linux, one of the fathers of modern computing... so yeah, he should totally fire himself for designing such a tool.

There were two major products to come out of Berkeley: LSD and Unix. We don't believe this to be a coincidence.
Just sayin...
;)
 

Carlos marin

Active Member
Licensed User
Longtime User
Thanks!

Related to it, really worth looking at SourceTree, a graphic (and free) Git client. I used it for a large distributed team project, and currently for some cloned github repos for my own projects. It allows to do everything from a more intuitive environmet. I think I am only using a 20% of it and it is more than enough for my daily needs (until the day in which I really break something and will need to learn the advanced command-line features).

jordi ese sourcetree se puede trabajar con b4a tambien ???
 
Top