Version Control with Repo and Git

本文介绍了Android开发中常用的版本控制工具Git、仓库管理工具Repo及代码审查系统Gerrit的基本使用方法。通过这些工具,开发者可以高效地进行跨网络操作、文件同步、分支创建与切换、文件变更提交等任务。

To work with the Android code, you will need to use both Git and Repo. In most situations, you can use Git instead of Repo, or mix Repo and Git commands to form complex commands. Using Repo for basic across-network operations will make your work much simpler, however.

Git is an open-source version-control system designed to handle very large projects that are distributed over multiple repositories. In the context of Android, we use Git for local operations such as local branching, commits, diffs, and edits. One of the challenges in setting up the Android project was figuring out how to best support the outside community--from the hobbiest community to large OEMs building mass-market consumer devices. We wanted components to be replaceable, and we wanted interesting components to be able to grow a life of their own outside of Android. We first chose a distributed revision control system, then further narrowed it down to Git.

Repo is a repository management tool that we built on top of Git. Repo unifies the many Git repositories when necessary, does the uploads to our revision control system, and automates parts of the Android development workflow. Repo is not meant to replace Git, only to make it easier to work with Git in the context of Android. The repo command is an executable Python script that you can put anywhere in your path. In working with the Android source files, you will use Repo for across-network operations. For example, with a single Repo command you can download files from multiple repositories into your local working directory.

Gerrit is a web-based code review system for projects that use git. Gerrit encourages more centralized use of Git by allowing all authorized users to submit changes, which are automatically merged if they pass code review. In addition, Gerrit makes reviewing easier by displaying changes side by side in-browser and enabling inline comments.

Basic Workflow

The basic pattern of interacting with the repositories is as follows:

  1. Use repo start to start a new topic branch.

  2. Edit the files.

  3. Use git add to stage changes.

  4. Use git commit to commit changes.

  5. Use repo upload to upload changes to the review server.

Task reference

The task list below shows a summary of how to do common Repo and Git tasks. For complete quick-start information and examples, see Getting started.

Synchronizing your client

To synchronize the files for all available projects:

$ repo sync

To synchronize the files for selected projects:

$ repo sync PROJECT0 PROJECT1 PROJECT2 ...

Creating topic branches

Start a topic branch in your local work environment whenever you begin a change, for example when you begin work on a bug or new feature. A topic branch is not a copy of the original files; it is a pointer to a particular commit. This makes creating local branches and switching among them a light-weight operation. By using branches, you can isolate one aspect of your work from the others. For an interesting article about using topic branches, see Separating topic branches

To start a topic branch using Repo:

$ repo start BRANCH_NAME

To verify that your new branch was created:

$ repo status

Using topic branches

To assign the branch to a particular project:

$ repo start BRANCH_NAME PROJECT

To switch to another branch that you have created in your local work environment:

$ git checkout BRANCH_NAME

To see a list of existing branches:

$ git branch

or

$ repo branches

The name of the current branch will be preceded by an asterisk.

Note: A bug might be causing repo sync to reset the local topic branch. If git branch shows * (no branch) after you run repo sync, then run git checkout again.

Staging files

By default, Git notices but does not track the changes you make in a project. In order to tell git to preserve your changes, you must mark them for inclusion in a commit. This is also called "staging".

You can stage your changes by running

git add

which accepts as arguments any files or directories within the project directory. Despite the name, git add does not simply add files to the git repository; it can also be used to stage file modifications and deletions.

Viewing client status

To list the state of your files:

$ repo status

To see uncommitted edits:

$ repo diff

The repo diff command shows every local edit that you have made that would not go into the commit, if you were to commit right now. To see every edit that would go into the commit if you were to commit right now, you need a Git command, git diff. Before running it, be sure you are in the project directory:

$ cd ~/WORKING_DIRECTORY/PROJECT  
$ git diff --cached

Committing changes

A commit is the basic unit of revision control in git, consisting of a snapshot of directory structure and file contents for the entire project. Creating a commit in git is as simple as typing

git commit

You will be prompted for a commit message in your favorite editor; please provide a helpful message for any changes you submit to the AOSP. If you do not add a log message, the commit will be aborted.

Uploading changes to Gerrit

Before uploading, update to the latest revisions:

repo sync

Next run

repo upload

This will list the changes you have committed and prompt you to select which branches to upload to the review server. If there is only one branch, you will see a simple y/n prompt.

Recovering sync conflicts

If a repo sync shows sync conflicts:

  • View the files that are unmerged (status code = U).
  • Edit the conflict regions as necessary.
  • Change into the relevant project directory, run git add and git commit for the files in question, and then "rebase" the changes. For example:

    $ git add .
    $ git commit 
    $ git rebase --continue
    
  • When the rebase is complete start the entire sync again:

    $ repo sync PROJECT0 PROJECT1 ... PROJECTN
    

Cleaning up your client files

To update your local working directory after changes are merged in Gerrit:

$ repo sync

To safely remove stale topic branches:

$ repo prune

Deleting a client

Because all state information is stored in your client, you only need to delete the directory from your filesystem:

$ rm -rf WORKING_DIRECTORY

Deleting a client will permanently delete any changes you have not yet uploaded for review.

Git and Repo cheatsheet

<think> 我们正在讨论如何将旧仓库的某个分支的子目录合并到新仓库的某个分支的主目录。用户之前的问题是关于git操作,现在他问的是在IntelliJ IDEA中集成git filter-repo的方法。注意,git filter-repo是一个命令行工具,IntelliJ IDEA本身并没有内置对其的直接支持。但是,我们可以通过配置外部工具的方式来在IDEA中使用它。 步骤: 1. 确保已经安装了git filter-repo。可以通过pip安装:`pip install git-filter-repo` 2. 在IntelliJ IDEA中配置外部工具,使得我们可以通过菜单或快捷键运行git filter-repo命令。 3. 配置时,我们需要指定要运行的命令和参数。 具体配置方法: 1. 打开IntelliJ IDEA,进入设置(Settings)。 2. 在设置中,找到“Tools” -> “External Tools”。 3. 点击“+”添加一个新的外部工具。 4. 配置如下: - Name: 任意,例如“git filter-repo” - Program: 填写git-filter-repo的完整路径(可以通过`which git-filter-repo`或`where git-filter-repo`来获取) - Arguments: 根据需求填写,例如:`--path path/to/subdir` (注意,这里我们可以使用IDEA的宏来动态获取路径,比如`$FilePathRelativeToProjectRoot$`,但需要根据实际需求调整,因为git filter-repo需要在仓库根目录运行,而且参数是相对路径) 5. 工作目录:填写`$ProjectFileDir$`(项目根目录) 但是,请注意:git filter-repo操作会重写历史,这是一个危险操作。因此,我们通常建议在命令行中执行,以便更清楚地了解每一步。在IDEA中集成外部工具运行git filter-repo,可能无法提供完整的交互和确认过程。 另外,由于git filter-repo需要在整个仓库上运行,并且参数中的路径是相对于仓库根目录的,所以我们需要确保当前项目目录就是Git仓库的根目录。 然而,用户可能更关心的是如何通过IDEA的图形界面完成整个迁移过程,包括提取子目录。遗憾的是,IDEA并没有提供这样的内置功能。我们可以通过以下步骤在IDEA中完成部分操作: 替代方案(在IDEA中执行部分步骤): 1. 使用IDEA的Git集成来克隆旧仓库(VCS -> Get from Version Control) 2. 在IDEA的终端中运行git filter-repo命令(这样可以利用IDEA的终端,同时使用IDEA的项目路径) 3. 然后使用IDEA的Git工具进行提交和推送等操作。 因此,更推荐的做法是在IDEA的终端(Terminal)中直接运行git filter-repo命令,而不是配置为外部工具。因为外部工具配置对于不同参数每次都要修改,而命令行更灵活。 如果用户坚持要配置外部工具,可以这样配置一个通用命令(不指定具体路径): Program: /path/to/git-filter-repo Arguments: 留空,然后在运行外部工具时手动输入参数(但这样就需要每次输入参数,不太方便) 或者,我们可以配置多个外部工具,每个对应一个常用的子目录。但这样也不灵活。 结论:在IDEA中使用终端直接运行git filter-repo命令是最佳实践。 因此,回答重点: 1. 安装git filter-repo(通过pip) 2. 在IDEA的终端中使用git filter-repo命令来过滤子目录 3. 后续的合并操作可以通过IDEA的Git集成界面进行(比如添加远程仓库、合并等) 详细步骤: ### 在IntelliJ IDEA中使用git filter-repo的方法 虽然IntelliJ IDEA没有原生集成`git filter-repo`,但可以通过以下步骤使用: #### 步骤1:安装git filter-repo 确保系统已安装Python和pip,然后执行: ```bash pip install git-filter-repo ``` #### 步骤2:在IDEA终端中执行命令 1. 打开旧仓库项目(通过VCS菜单克隆或打开) 2. 打开IDEA内置终端(Alt+F12) 3. 运行过滤命令(示例:保留`src/utils`目录): ```bash git filter-repo --path src/utils/ ``` 注意:此操作会重写历史,请确保仓库已备份或确认操作。 #### 步骤3:后续操作 - **推送更改**:由于历史被重写,需要强制推送(谨慎使用): ```bash git push origin --force ``` - **迁移到新仓库**:在新仓库项目中,添加旧仓库为远程,然后合并(参考之前的方法) #### 替代方案:配置外部工具(可选) 1. 打开设置(Ctrl+Alt+S)> Tools > External Tools 2. 点击`+`,填写: - Name: `Filter Repo (keep subdir)` - Program: `git-filter-repo` (或完整路径,如`/usr/local/bin/git-filter-repo`) - Arguments: `--path $Prompt$` (这样运行时会弹出输入框要求输入子目录路径) - Working directory: `$ProjectFileDir$` 3. 使用:右键项目根目录 > External Tools > 选择配置的工具,输入子目录(如`src/utils`) > 注意:外部工具方式无法灵活使用所有`git-filter-repo`参数,复杂操作仍需命令行 ### 验证安装 在IDEA终端运行: ```bash git filter-repo --version ``` ### 注意事项 1. **备份仓库**:`git filter-repo`会永久删除数据,操作前创建分支备份 2. **权限问题**:可能需要用`sudo`安装,但避免在项目中使用sudo 3. **路径差异**:Windows系统需注意路径斜杠(使用`/`或`\`) 通过以上方法,即可在IntelliJ IDEA环境中使用`git filter-repo`进行子目录提取。 §§相关问题§§ 1. 如何在IntelliJ IDEA中配置Python解释器以安装git-filter-repo? 2. 使用git filter-repo后如何解决强制推送导致的团队协作问题? 3. 除了git filter-repo,IntelliJ IDEA有哪些内置的Git历史修改功能? </think> ### IntelliJ IDEA集成git filter-repo的方法 在IntelliJ IDEA中集成git filter-repo需要通过**外部工具配置**实现,以下是详细步骤: #### 1. 前置条件准备 ```bash # 安装git filter-repo(需先安装Python) pip install git-filter-repo ``` > 验证安装:`git filter-repo --version`[^1] #### 2. IDEA配置步骤 1. **打开设置界面** `File > Settings > Tools > External Tools` 2. **添加新工具** ```ini Name: Git Filter-Repo Program: git-filter-repo # 或完整路径如/usr/bin/git-filter-repo Arguments: --path $FilePathRelativeToProjectRoot$ Working directory: $ProjectFileDir$ ``` > 关键参数说明: > - `$FilePathRelativeToProjectRoot$`:自动获取选中文件的相对路径 > - `$ProjectFileDir$`:项目根目录 3. **保存配置** <kbd>Apply</kbd> → <kbd>OK</kbd> #### 3. 使用示例(提取子目录) 1. 在项目视图中右键点击目标目录(如`src/utils`) 2. 选择 `External Tools > Git Filter-Repo` 3. 等待执行完成,IDEA将自动刷新项目 ```mermaid graph LR A[右键目录] --> B[External Tools] B --> C[Git Filter-Repo] C --> D[自动过滤历史] ``` #### 4. 高级参数配置 对于复杂场景,可创建多工具配置: | 场景 | Arguments 示例 | |------|----------------| | 多目录过滤 | `--path dir1/ --path dir2/` | | 保留空提交 | `--preserve-commit-hashes` | | 重写提交信息 | `--mailmap my-mailmap.txt` | #### 5. 注意事项 1. **安全警告** ```diff - 操作会永久删除.git历史,先创建备份分支 + 建议在终端先执行:git branch backup-pre-filter ``` 2. **Windows特殊配置** 需在PATH环境变量添加Python安装路径: ```ini Program: C:\Python39\Scripts\git-filter-repo.exe ``` 3. **冲突解决** 当出现`Commit ... is empty`警告时: - 在终端执行:`git filter-repo --force` - 或添加参数:`--prune-empty never` #### 6. 验证结果 在IDEA中: 1. 打开`Git > Log`标签页 2. 检查历史记录是否仅含目标目录 3. 使用`Show History`右键菜单验证文件历史连续性 > 此方法同样适用于迁移子目录到新仓库,结合`git remote add`使用效果更佳[^1]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值