.gitignore for Android projectsGitLab authenticates git clone/git push over SSH with a key pair. You do this once per computer.
# 1. create a key pair (accept the default file name, a passphrase is optional)
ssh-keygen -t ed25519 -C "your-hdm-account@hdm-stuttgart.de"
# 2. print the public key and copy the whole line
cat ~/.ssh/id_ed25519.pub
# 3. GitLab > avatar (top right) > Preferences > SSH Keys > Add new key: paste it
# 4. test the connection, GitLab answers with "Welcome to GitLab, @<account>!"
ssh -T git@gitlab.mi.hdm-stuttgart.de
id_ed25519 without .pub), only the public key goes to GitLabTo initialize a new project you would call git init. This is not needed in our case: every participant gets a
repository with the folder structure for the assignments, and every team gets a project repository. So please do:
git clone git@gitlab.mi.hdm-stuttgart.de:mad-android/<semester>/<hdm-account>.git
cd <hdm-account>
<semester> is e.g.
ss26, <hdm-account> is your HdM loginmain/master branch and origin points to the GitLab repositoryCreate a new file in the repository, e.g. assignment1/readme.md, then:
git status # which files are untracked or modified?
git add assignment1/readme.md # stage the file (git add . stages everything)
git commit -m "Add readme for assignment 1"
git log --oneline # history, one line per commit
Before committing, check what you have actually changed:
git diff # unstaged changes compared to the last commit
git diff --staged # staged changes that will go into the next commit
.gitignore for Android ProjectsA .gitignore file lists what Git must never track. For an Android Studio project these must stay out of the repository:
# generated build outputs, re-created by Gradle on every build
build/
.gradle/
.kotlin/
*.apk
*.aab
# IDE settings of your machine
.idea/
*.iml
# machine specific paths and secrets (SDK location, OMDB_API_KEY)
local.properties
.gitignore in the project root and in app/ for every new project, the starter projects
of the assignments already contain it.gitignore does not help: git rm -r --cached build/ removes it from the indexWhile working in a team we have a remote repository, in our case hosted on the HdM GitLab. Push your local commits:
git push origin master # or main, depending on your repository
Team members made changes and you want to get them:
git fetch origin # downloads the new commits, changes nothing in your working copy
git pull # = git fetch + git merge origin/<current branch>
git fetch lets you look first (git log --oneline master..origin/master), git pull integrates right awaygit pull, resolve conflicts if any, push againmain/master branch is graded, make sure your final state is pushed thereIn a team, each member works on a different feature in an own branch with main/master as the base:
git switch -c feature1 # create the branch and switch to it
git branch # list branches, * marks the current one
git switch master # switch back
git switch feature1
git switch exists since Git 2.23 and only switches branches. The older git checkout does the same but also restores
files, you will still see it in many tutorials:
git checkout -b feature1 # same as git switch -c feature1
git checkout master # same as git switch master
You did your work on feature1, added files and committed them. Now the feature should go into main/master:
git switch master
git pull # get the latest master first
git merge feature1 # merge the branch into master
git push origin master # the merge is local until you push
git branch -d feature1 # delete the branch when it is no longer needed
$ git merge feature1
CONFLICT (content): Merge conflict in app/src/main/java/.../MainActivity.kt
Automatic merge failed; fix conflicts and then commit the result.
<<<<<<< HEAD
Text(text = "Clicked $counter times")
=======
Text(text = "You clicked $counter times")
>>>>>>> feature1
git status lists them), keep the version you want or combine both, delete the
<<<<<<<, =======, >>>>>>> markers. Android Studio shows this as a three-way diff (Git > Resolve Conflicts)git add <file>, then finish with git commit (the merge message is pre-filled)git merge --abort throws the merge away and brings you back to the state before the mergeThe project is done in teams of 2-4. Nobody pushes directly to main/master, changes go through a Merge Request (MR):
git switch -c feature/search-screen and commit your work on this branchgit push -u origin feature/search-screen, GitLab prints a link to create the Merge Requestgit pull on main/mastermain/master into your branch regularly (git merge master on the feature branch) to keep conflicts small./gradlew test before anyone reviewsIf you have issues using Git in the command line you can of course use a Git client. Popular ones:
status, add, commit, log --oneline, diff are the daily local commands.gitignore keeps build/, .gradle/, .idea/, local.properties and APKs out of the repositorypush publishes commits, fetch downloads, pull = fetch + mergegit switch -c), merge into main/master, resolve conflicts by editing the markers, add,
commitgit fetch and git pull?local.properties never be committed? What else belongs into .gitignore of an Android project?git switch -c feature1 do, and which older command does the same?main/master directly?