Git 使用 配置身份 更改全局用户名和邮箱:
git config –global user.name “你的名字”
git config –global user.email “你的邮箱”
更改此库的用户名和邮箱
git config user.name “你的名字”
git config user.email “你的邮箱”
查询用户名和邮箱
git config user.name
git config user.email
1 2 3 jianghouren@localhost ~ % git config user.name jianghouren@localhost ~ % git config user.email
创建仓库 仓库(repository)是用于保存版本管理所需信息的地方,所有本地提交的代码都会被提交到代码仓库中,如果有需要还可以推送到远程仓库中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 jianghouren@localhost ~ % cd EspressoApplication jianghouren@localhost EspressoApplication % git init Initialized empty Git repository in /Users/jianghouren/EspressoApplication/.git/ jianghouren@localhost EspressoApplication % ls -al total 72 drwxr-xr-x 15 jianghouren staff 480 7 1 20 :12 . drwxr-xr-x+ 47 jianghouren staff 1504 7 1 20 :10 .. drwxr-xr-x 9 jianghouren staff 288 7 1 20 :12 .git -rw-r--r-- 1 jianghouren staff 208 7 1 20 :10 .gitignore drwxr-xr-x 5 jianghouren staff 160 7 1 20 :10 .gradle drwxr-xr-x 12 jianghouren staff 384 7 1 20 :10 .idea -rw-r--r-- 1 jianghouren staff 952 7 1 20 :10 EspressoApplication.iml drwxr-xr-x 9 jianghouren staff 288 7 1 20 :10 app -rw-r--r-- 1 jianghouren staff 558 7 1 20 :10 build.gradle drwxr-xr-x 3 jianghouren staff 96 7 1 20 :10 gradle -rw-r--r-- 1 jianghouren staff 1073 7 1 20 :10 gradle.properties -rwxr--r-- 1 jianghouren staff 5296 7 1 20 :10 gradlew -rw-r--r-- 1 jianghouren staff 2260 7 1 20 :10 gradlew.bat -rw-r--r-- 1 jianghouren staff 441 7 1 20 :10 local.properties -rw-r--r-- 1 jianghouren staff 54 7 1 20 :10 settings.gradle jianghouren@localhost EspressoApplication %
忽略文件 对项目的两个 .gitignore 文件进行配置(根目录下一个,app 模块下一个)。并且,其中指定的文件或目录是可以使用 “ * ” 通配符的。比如想忽略测试文件:
1 2 3 4 /build /src/test /src/androidTest
提交本地代码 add 用于把想要提交的代码添加进来,commit 则是真正执行提交操作。
1 2 3 4 5 6 7 8 jianghouren@localhost EspressoApplication % git add build.gradle jianghouren@localhost EspressoApplication % git add app jianghouren@localhost EspressoApplication % git add . jianghouren@localhost EspressoApplication % git commit -m "First commit."
查看修改内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 jianghouren@localhost EspressoApplication % git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .idea/vcs.xml 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: app/.gitignore jianghouren@localhost EspressoApplication % git diff diff --git a/app/.gitignore b/app/.gitignore index 796b 96d..346 a6ac 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1 ,3 @@ /build +/src/test +/src/androidTest jianghouren@localhost EspressoApplication % git diff app/.gitignore jianghouren@localhost EspressoApplication %
撤销未提交的修改 只适用于那些还没有执行过 add 命令的文件
1 2 3 4 5 6 7 8 9 10 11 12 jianghouren@localhost EspressoApplication % git checkout app/.gitignore Updated 1 path from the index jianghouren@localhost EspressoApplication % git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .idea/vcs.xml jianghouren@localhost EspressoApplication %
对于已添加的文件,应该先对其取消添加,然后才可以撤回提交。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 jianghouren@localhost EspressoApplication % git add . jianghouren@localhost EspressoApplication % git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .idea/vcs.xml modified: app/.gitignore modified: app/src/main/java/com/example/espressoapplication/MainActivity.java jianghouren@localhost EspressoApplication % git checkout app/.gitignore Updated 0 paths from the index jianghouren@localhost EspressoApplication % jianghouren@localhost EspressoApplication % git reset HEAD app/.gitignore Unstaged changes after reset: M app/.gitignore jianghouren@localhost EspressoApplication % git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .idea/vcs.xml modified: app/src/main/java/com/example/espressoapplication/MainActivity.java 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: app/.gitignore jianghouren@localhost EspressoApplication % git checkout app/.gitignore Updated 1 path from the index jianghouren@localhost EspressoApplication %
查看提交记录 使用 git log 命令来查看。还可以通过 git log “记录的 id”,或者 git log -1,来查看最近几次的提交。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 jianghouren@localhost EspressoApplication % git log commit 6b 16d74628f10bb80736039ae881567e1415b574 (HEAD -> master) Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 21 :19 :01 2020 +0800 Add log. commit 67b 6e205d7183d51406d1bf75fb248829f8a2692 Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 20 :35 :02 2020 +0800 First commit. jianghouren@localhost EspressoApplication % git log 6b 16d74628f10bb80736039ae881567e1415b574 commit 6b 16d74628f10bb80736039ae881567e1415b574 (HEAD -> master) Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 21 :19 :01 2020 +0800 Add log. commit 67b 6e205d7183d51406d1bf75fb248829f8a2692 Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 20 :35 :02 2020 +0800 First commit. jianghouren@localhost EspressoApplication % git log -1 commit 6b 16d74628f10bb80736039ae881567e1415b574 (HEAD -> master) Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 21 :19 :01 2020 +0800 Add log. jianghouren@localhost EspressoApplication % git log -2 commit 6b 16d74628f10bb80736039ae881567e1415b574 (HEAD -> master) Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 21 :19 :01 2020 +0800 Add log. commit 67b 6e205d7183d51406d1bf75fb248829f8a2692 Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 20 :35 :02 2020 +0800 First commit. jianghouren@localhost EspressoApplication % git log 67b 6e205d7183d51406d1bf75fb248829f8a2692 commit 67b 6e205d7183d51406d1bf75fb248829f8a2692 Author: feiyeyuanye <199302212 @qq.com> Date: Wed Jul 1 20 :35 :02 2020 +0800 First commit. jianghouren@localhost EspressoApplication %
分支的用法 这里因为缺乏多人开发的经验,暂存疑惑:
多人开发:个人创建分支 –> 合并到主干线上 ?
独立开发:主干线上开发 –> 发布版本时创建分支 ?
比如项目在发布 1.0 版本时建立一个分支,然后在主干线上继续开发 1.1 版本功能。当在 1.0 版本上发觉任何 bug 时,就在分支上进行修改,然后发布新的 1.0 版本,并记得将修改后的代码合并到主干线上。这样的话,不仅可以轻松解决 1.0 版本存在的 bug,而且保证了主干线上的代码也已经修复了这些 bug,当 1.1 版本发布时,就不会有同样的 bug 存在了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 jianghouren@localhost EspressoApplication % git branch * master jianghouren@localhost EspressoApplication % git branch version1.0 jianghouren@localhost EspressoApplication % git branch * master version1.0 jianghouren@localhost EspressoApplication % git checkout version1.0 Switched to branch 'version1.0' jianghouren@localhost EspressoApplication % git branch master * version1.0 jianghouren@localhost EspressoApplication % git checkout master Switched to branch 'master' jianghouren@localhost EspressoApplication % git merge version1.0 Already up to date. jianghouren@localhost EspressoApplication % git branch -D version1.0 Deleted branch version1.0 (was 6b 16d74). jianghouren@localhost EspressoApplication % git branch * master jianghouren@localhost EspressoApplication %
与远程版本库协作 要养成经常从版本库中获取最新代码的习惯,避免经常出现冲突。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 jianghouren@localhost ~ % git clone git@github.com:feiyeyuanye/Preliminary-use-of-Kotlin.git Cloning into 'Preliminary-use-of-Kotlin' ... remote: Enumerating objects: 211 , done. remote: Counting objects: 100 % (211 /211 ), done. remote: Compressing objects: 100 % (136 /136 ), done. remote: Total 211 (delta 61 ), reused 188 (delta 38 ), pack-reused 0 Receiving objects: 100 % (211 /211 ), 156.74 KiB | 8.00 KiB/s, done. Resolving deltas: 100 % (61 /61 ), done. jianghouren@localhost ~ % cd Preliminary-use-of-Kotlin jianghouren@localhost Preliminary-use-of-Kotlin % git push origin master Everything up-to-date jianghouren@localhost Preliminary-use-of-Kotlin %
将远程版本库上的修改同步到本地。Git 提供了两种命令来完成:fetch 和 pull。
1 2 3 4 5 6 7 8 9 10 11 jianghouren@localhost ~ % cd Preliminary-use-of-Kotlin jianghouren@localhost Preliminary-use-of-Kotlin % git fetch origin master From github.com:feiyeyuanye/Preliminary-use-of-Kotlin * branch master -> FETCH_HEAD jianghouren@localhost Preliminary-use-of-Kotlin % git diff origin/master jianghouren@localhost Preliminary-use-of-Kotlin % git merge origin/master Already up to date. jianghouren@localhost Preliminary-use-of-Kotlin %
1 2 3 4 5 6 7 jianghouren@localhost Preliminary-use-of-Kotlin % git pull origin master From github.com:feiyeyuanye/Preliminary-use-of-Kotlin * branch master -> FETCH_HEAD Already up to date. jianghouren@localhost Preliminary-use-of-Kotlin %
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 jianghouren@jianghourendeMacBook-Pro androidsolo % git add . jianghouren@jianghourendeMacBook-Pro androidsolo % git jianghouren@jianghourendeMacBook-Pro androidsolo % git add . jianghouren@jianghourendeMacBook-Pro androidsolo % git commit -m "update" jianghouren@jianghourendeMacBook-Pro androidsolo % git status jianghouren@jianghourendeMacBook-Pro androidsolo % git pull jianghouren@jianghourendeMacBook-Pro androidsolo % git push origin master jianghouren@jianghourendeMacBook-Pro androidsolo % git status jianghouren@jianghourendeMacBook-Pro androidsolo % git branch
将代码托管到 GitHub 上
GitHub 上创建版本库,并复制版本库的 Git 地址。
为版本库命名
添加描述信息
勾选 “Initialize this repository with a README”
添加一个 Android 项目类型的 .gitignore 文件
使用 Apache License 2.0 来作为项目的开源协议
本地新建项目文件夹,cd 切换到此文件夹目录下,并 clone 远程版本库到本地。
将此目录下的文件全部复制粘贴到上一层目录中,这样就能将整个工程目录添加到版本控制中去了。注意,.git 是一个隐藏目录,复制时千万不要漏掉。另外,上一层目录中也有一个 .gitignore 文件,直接将其覆盖即可。复制完后可将该目录删除。
接下来将项目中现有的文件提交到 GitHub 上面。
git add。先将所有文件添加到版本控制中。
git commit -m “First commit.”。在本地执行提交操作。
ls -al (可查看目录结构)
git push origin master。将提交的内容同步到远程版本库。
最后一步时,GitHub 可能会要求输入用户名和密码来进行身份效验,这里输入注册时填入的用户名和密码就可以了。
在 GitHub 上刷新一下项目版本库的主页,就可以看到提交的文件了。
修改 GitHub 仓库提交信息(邮箱) 修改了 GitHub 的邮箱后,导致提交信息记录(绿色的小格子)消失。
https://www.dazhuanlan.com/2020/03/01/5e5aa51db0750/
https://docs.github.com/en/github/using-git/changing-author-info
https://stackoverflow.com/questions/750172/how-to-change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-gi?rq=1
https://stackoverflow.com/questions/39631983/github-invalid-author-email-commits-no-contribution-activity
将项目 clone 下来后,执行脚本修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/sh(这是注释,执行命令时要删除。) git filter-branch --env-filter ' OLD_EMAIL="your-old-email@example.com" CORRECT_NAME="" CORRECT_EMAIL="" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags
关闭 less 界面
按 q 关闭 less 界面。
按 Ctrl+Z 关闭 vim 界面?下次遇到了再试。
Sourcetree 使用
添加本地代码到 GitHub:
备注
参考资料 :
第一行代码(第3版)
廖雪峰学git
GitHub 文件