准备工作
1 安装Git
linux系统:sudo apt-get install git-core
2 git教程文档
输入以下命令:git help tutorial,会打开官方教程文档
file:///D:/software/Git/mingw64/share/doc/git-doc/gittutorial.html
其中D:/software/Git为git软件的安装路径
git官网:
https://git-scm.com/book/zh/v2
2.配置一下你的身份
git config --global user.name “Tony”
git config --global user.email "tony@gmail.com"
git命令
username@hostname:~$ git --help
用法:git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
最常用的 git 命令有:
add 添加文件内容至索引
bisect 通过二分查找定位引入 bug 的变更
branch 列出、创建或删除分支
checkout 检出一个分支或路径到工作区
clone 克隆一个仓库到一个新目录
commit 记录变更到仓库
diff 显示提交之间、提交和工作区之间等的差异
fetch 从另外一个仓库下载对象和引用
grep 输出和模式匹配的行
init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库
log 显示提交日志
merge 合并两个或更多开发历史
mv 移动或重命名一个文件、目录或符号链接
pull 获取并整合另外的仓库或一个本地分支
push 更新远程引用和相关的对象
rebase 本地提交转移至更新后的上游分支中
reset 重置当前 HEAD 到指定状态
rm 从工作区和索引中删除文件
show 显示各种类型的对象
status 显示工作区状态
tag 创建、列出、删除或校验一个 GPG 签名的标签对象
命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些指南。参见
'git help <命令>' 或 'git help <指南>' 来查看给定的子命令帮助或指南。
username@hostname:~$
D:\AndroidStudioProjects\LauncherApplication>git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
D:\AndroidStudioProjects\LauncherApplication>
1.创建代码仓库
先进入要创建仓库的代码目录,再执行以下命令:
git init
2.提交本地代码
先 git add 再 git commit
git add build.gradle //添加单个文件
git add app //添加指定目录下的所有文件
git add . //添加所有文件
git commit -m “First commit.” // -m 后接此次提交的描述信息,
查看修改内容
git status
N1.修改AllAppAdapter.java文件后,执行git status,结果如下
D:\AndroidStudioProjects\LauncherApplication>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: one_scroll/src/main/java/com/example/wedesign/launcherapplication/AllAppAdapter.java
no changes added to commit (use "git add" and/or "git commit -a")
D:\AndroidStudioProjects\LauncherApplication>
2.查看文件变动情况
git diff
git diff one_scroll/src/main/java/com/example/wedesign/launcherapplication/AllAppAdapter.java
3.1 撤销未提交的修改
git checkout app/src/main/java/com/example/providertest/MainActivity.java
3.2 将修改添加到工作目录
D:\AndroidStudioProjects\LauncherApplication>git add one_scroll/src/main/java/com/example/wedesign/launcherapplication/AllAppAdapter.java
D:\AndroidStudioProjects\LauncherApplication>git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: one_scroll/src/main/java/com/example/wedesign/launcherapplication/AllAppAdapter.java
6 撤销git add
文件修改后,需先 git add ,后git commit
执行git add 没有执行 git commit,可通过 git reset HEAD 撤销