Git
1、Git工作流程
工作区域
工作区(Workspace) :进行开发改动的地方,是你当前看到的,也是最新的。平常我们开发就是拷贝远程仓库中的一个分支,基于该分支进行开发。在开发过程中就是对工作区的操作。
暂存区(Index/Stage) :保存了下次将要提交的文件列表信息。在.git目录下的index文件,暂存区会记录git add
添加文件的相关信息(文件名、大小、timestamp),可以使用git status
查看暂存区的状态。暂存区标记了当前工作区中,哪些内容是被git管理的。当完成某个需求或功能后需要提交到远程仓库,那么第一步就是通过git add
先提交到暂存区,被git管理。
本地仓库(Repository) :保存了对象被提交过的各个版本。相比工作区和暂存区的内容要旧,git commit
后同步index的目录树到本地仓库。
远程仓库(Remote) :远程仓库的内容可能被分布在多个地点的处于协作关系的本地仓库修改,因此它可能与本地仓库同步,也可能不同步,它的内容是最旧的。如Github、Gitee或局域网的一台电脑。
文件状态
已修改(modified) :表示修改了文件,但还没保存到数据库中。
已暂存(staged) :表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
已提交(commited) :表示数据已经安全的保存在本地数据库中。
分支(branch)
在开发软件时,可能有多人同时为一个软件开发功能或修复BUG,可能存在多个Release版本,并且需要对各个版本进行维护。因此,Git的分支功能应运而生。
分支是为了将修改记录的整体流程分叉保存,分叉后的分支不受其他分支的影响,所以在同一个数据库里可以同时进行多个修改。在完成修改后可合并到主分支。
2、Git常用命令
2.1、Git配置
1、配置用户信息
1 2 git config --global user.name "LinZhu" git config --global user.email XXXX@example.com
2、配置默认文本编辑器
1 git config --global core.editor vim
3、查看所有配置信息及所在文件
1 git config --list --show-origin
2.2、Git分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 git branch <branchName> git branch git branch -vv git branch -d <branchName> git branch -D <branchName> git checkout <branchName> git checkout -b <branchName> git checkout --track <remote>/<branch> git checkout -b <branch> <remote>/<branch>
1 2 3 4 5 6 7 8 9 10 11 git fetch git merge git pull git pull -r
1 2 git push <remote> --delete <branch>
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 git merge git rebase git checkout feature git merge master git checkout feature git rebase master
2.3、修改和提交
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 git add readme.md git add . git commit git commit -a git commit --amend git state git state -s git diff git diff -cached git diff -staged git show git rm readme.md git rm -f readme.md git rm --cached readme.md git rm -r myDir git rm -r * git mv git mv fileName newFileName git mv text.txt myDir git reset [--soft|--mixed|--hard] [HEAD] git reset HEAD^ git reset HEAD~3 git reset HEAD^ fileName git reset --hard origin/master git stash git stash list git stash pop git stash apply stash@{2} git stash drop <stashName> 1、举例来说,有如下Master和Feature分支: a - b - c - d Master \ e - f - g Feature 现在将提交f应用到Master分支, git checkout Master git cherry-pick f 应用之后,分支结构如图: a - b - c - d - f Master \ e - f - g Feature 2、cherry-pick一次转移多个提交 git cherry-pick <HashA> <HashB> 第一步将修改的文件重新加入暂存区(git add .) 运行 git cherry-pick --continue 让cherry-pick继续执行。 其他参数:--abort 放弃合并,回到操作前的样子;--quit 退出。
2.4、提交日志
1 2 3 4 git log git blame <fileName>
3、Repo简介
一个Python实现的Git代码脚本,用于整合多个Git代码库,方便在Android环境中使用Git。