如何在 Android 上使用 Git?

我有一个使用 git 进行同步的桌面应用程序。我还有一个 android 应用程序,它和桌面一样,但我不知道如何在它上面做同步部分。我还没有在 android 上找到任何 git 的实现。我找到了一个 jgit,但它没有很好的文档,我甚至无法用它来做一个提交。

我在考虑从我的 android 应用程序到我的 git 服务器的远程命令,但这对我来说真的是一个挑战。 你能告诉我是否还有其他可以在 android 上使用的 git 实现吗?如果你知道怎么解决这个问题,请告诉我。

108187 次浏览

This is how I would do it:

Well you likely want to define an interface for your synchronization needs.

You implement this interface in C for both desktop and android device (with the help of the NDK) by making the implementation use git (native).

Then, for the Android app, you code a JNI layer on top so that it exposes the interface to Java.

The tricky part would be to have the different git commands compile for Android: you'll have to rewrite makefiles.

Another option is using ASE. There seems to be a full-featured pure python implementation of git at https://github.com/jelmer/dulwich.

Since this question was first posted, an Android app has been added to the market that can checkout Git repos, called Agit. It can't commit or push yet, but the clone/fetch/pull functionality is useful. The other issue is that a patched version of ConnectBot needs to be installed before Agit if you want to use SSH keys. If you already have ConnectBot, uninstall it first. I did not do this and ran into problems.

Edit: As of April 2021, the ability to push and pull has been added, however, it does seem to be very buggy as I have only been able to pull changes, but never push them.

If the device is rooted, you can also use debian's debootstrap ( http://wiki.debian.org/Debootstrap ) application to create a debian/armel image, mount it in your android device and chroot to it, you have aptitude here so any package available for armel can be installed on your device (to install git: aptitude install git). Look here for information on how to install debian for your android: http://lanrat.com/install-debian-on-android/

Some other solutions that are now available, besided Agit, since this question was last answered:

  • A git & mercurial client for bitbucket called Bitbeaker

  • A git app from github

  • The Android Java IDE AIDE also has git functionality, so you can download from a git directory and if its an Android app modify and run it aswell.

I would suggest to take a look at Gidder. It's Git server implementation for Android with user and repository management and also support dynamic DNS. You can easily store your code in your Android device and access using a WiFi connection.

Update:

Comment underneath this answer by OP- (Nathan Osman)

unfortunately, I was forced to pull the application after some recent policy changes on the Play Store that required publishers to have their address displayed on the application's store page.


Original Answer

I've just (circa April 2013) published an app on the Play store named GitDroid. The application allows you to do the following:

  • clone remote repositories
  • pull from the remote and merge new revisions
  • view commits and browse files

Overview Tab of GitDroid

The app cannot push to a remote or make local commits yet - this is planned for a future update.

Update: CubeGit has been removed because of changes inside the Android system and missing time / incentives to update. Please pick one of the alternatives.


One more option is was CubeGit.

We have released CubeGit on Google Play a few days ago.

It is an interface to a cross compiled git binary and also supports local commits and push via ssh/http/https.

You can also try Pocket Git: https://play.google.com/store/apps/details?id=com.aor.pocketgit

Pocket Git supports SSH (with passphrases, but private keys seem to have some issues) and HTTP, cloning, stage, unstage, commit, push and pull; create, delete and merge branches; and it also has a graphical log viewer and can show diffs.

Disclaimer: I'm the developer

A lot of already existing apps have been posted in the answers, but following links might be useful for those looking for their own implementation:

  1. libgit2

    This is a pure C implementation that claims to be highly portable. And the project also describes how to compile the sources for android platform. Now what needs to be done is to write a JNI binding for it. Even an executable binary would be sufficient in some cases.

  2. HTTP (dumb) protocol

    As an alternative, if one's purely interested in read operations from a repository, this protocol can be implemented over HTTP. There is also a smart protocol which depends on ssh and does differential transfers.

  3. JGit Core Sources

    Using just the core package from its sources. All the javax.* dependencies and other incompatible/alien classes will needed to be replaced with android alternatives though. But worth the effort if a pure Java implementation can be ported to android.

Try SGit, it can commit & push over SSH with pssphrase-less keys and it is available on F-Droid. It is built on top of JGit. It is not perfect (yet) and JGit implementation is not as good as original Git, but looks usable.

MGit is the successor of SGit, and it works very well for me.

You can install https://github.com/termux/termux-app @ https://play.google.com/store/apps/details?id=com.termux, then you just need to open the terminal and type:

  1. apt update
  2. apt install git

enter image description here

You can install git on android with Termux. Just install from Play Store and run the command:

pkg install git

Reference: https://mvprepublic.com/blog/2020/04/08/how-to-run-git-on-android-devices/