Git in the IDE

Android Studio 2: The New UI

Course 2 ยท Chapter 10 ยท Git in the New UI

Git saves the history of your project so you can undo, compare and share it. Android Studio has Git built in, and the New UI changes what you see depending on whether the project has a repository yet. This chapter shows both states, the two tool windows you will use most, and a few commit habits.

What was checked, and how: read this first
Checked on the install (your screenshots, Rabbit 2): the toolbar widget and menu name before and after a repository exists, the Commit window and the Git window. Checked in the docs (one web search, summaries only): enabling version control from the VCS menu, marked (docs). From other JetBrains IDEs, not Android Studio: committing and pushing steps, marked (JetBrains docs, not checked here). Share Project on GitHub is not verified.

Before a Repository: VCS

In a project with no repository the menu bar says VCS, and the top toolbar shows a Version Control widget beside the project name (checked on the install). (docs) From the VCS menu choose Enable Version Control Integration, pick Git as the system for the project root and click OK. Version-control settings are also under File > Settings > Version Control.

After a Repository: Git

ItemBefore a repositoryAfter a repository
Menu barVCSGit (replaces VCS)
Toolbar widgetVersion ControlThe current branch name (master in the project checked)

The Commit Window (Alt+0)

On the left bar. It lists the changed files with checkboxes, has an Amend last commit option, a message box, Commit and Commit and Push... buttons, and a gear for options (all checked on the install). Tick the files that belong together, write the message and commit. (JetBrains docs, not checked here) New files start under Unversioned Files and are added by ticking them.

Habit: in the screenshot the commit message was typed as "initial commit" with the quote marks. The IDE does not need them; they become part of the message. Write a short plain sentence, say what changed and why, and keep one idea per commit.

The Git Window (Alt+9)

At the bottom. It has Log and Console tabs, a branch tree (HEAD, Local, Remote), a search for text or a hash, and filters for Branch, User, Date and Paths (checked on the install). In that project the log showed origin & master, meaning the project already pointed at a remote. The Log tab is where you read history; the Console shows the Git commands the IDE ran.

Remotes and Pushing

A remote is a copy on a server such as GitHub, usually named origin. Two routes are possible: make the repository in the IDE first and add a remote later, or create the repository on GitHub first and clone or connect it. (JetBrains docs, not checked here) Other JetBrains IDEs push with Git > Push or Ctrl+Shift+K, which lists the commits before sending them; Commit and Push... does both in one step. Some JetBrains IDEs can also create a GitHub repository from the VCS or Git menu; I could not confirm that for Android Studio, so check the menu yourself. If you prefer the command line, the Terminal window (Alt+F12) runs the same commands:

# Same actions in the Terminal tool window git status git add . git commit -m "Add catalogue list screen" git push
Keep secrets out
A generated project's .gitignore normally excludes local.properties and build folders (general knowledge). Check it before the first push, and never commit passwords or API keys.
Compared with the Classic UI
Android Studio: The IDE Itself 8 describes a Git widget in the status bar and the VCS menu throughout. On Rabbit 2 the branch widget is in the top toolbar and the menu is Git once a repository exists. The Git concepts in that chapter still apply.

Try It Yourself

Exercise 1

In a scratch project with no repository, note the menu name and toolbar widget. Use VCS > Enable Version Control Integration with Git, then note what changed in the menu bar and the widget.

๐Ÿ“„ View solution
Exercise 2

Open the Commit window (Alt+0), tick your files, commit with a message that has no quote marks, then open the Git window (Alt+9) and find your commit in the Log tab.

๐Ÿ“„ View solution
Exercise 3

Search the Git menu for any command that creates or shares a repository on GitHub and report its exact name, or say there is none. Then create a branch and switch back to master using the toolbar widget.

๐Ÿ“„ View solution
Not Yet Verified
Checked on the install: VCS menu and Version Control widget before a repository; Git menu and branch widget after; the Commit window's contents and Alt+0; the Git window's tabs, tree, search, filters and Alt+9; the quoted commit message.
Checked in the docs (one web search): Android Studio's Enable Version Control Integration and the Settings path.
From other JetBrains IDEs only: unversioned-file handling, Git > Push and Ctrl+Shift+K.
Not verified: Share Project on GitHub or any GitHub menu item; the toolbar widget's branch menu (Exercise 3); the .gitignore contents; the exercises (not run by me). A screenshot of the Git menu open (after a repository exists) and the branch widget's drop-down would verify this chapter.
Sources: Version control basics (developer.android.com), Tutorial: Getting started with Git in GoLand (jetbrains.com).
Coming up
Chapter 11 covers the Agent window and Studio Labs. Chapter 12 is the capstone.