1.Git common command
1 2 3 4 5
| // set username git config --global user.name [username]
// set email git config --global user.email [email]
|
1 2 3 4 5
| // show username git config --global user.name
// show email git config --global user.email
|
1 2 3 4 5 6 7 8 9 10 11
| // init local repository git init
// show local repository status git status
// add file git add [file]
// commit to local repository git commit -m "message" [file]
|
1 2 3 4 5
| // show log git reflog
// show log in detail git log
|
2.Code rollback
(1) The file hasn’t git add
, discard the modify.
(2) The file has already git add
, but not git commit
, cancel the git add
.
1
| git restore --staged [file]
|
(3) The file has already git commit
, restore to the before version.
Git shift code version, move HEAD
pointer.
1
| git reset --hard [commit version]
|
3.Git branch
在推进多个开发任务时,我们可以为每个任务创建单独的分支,各个分支之间不会相互影响。
使用分支的优点:
1 2 3 4 5 6 7 8 9 10 11
| // 查看分支 git branch -v
// 创建分支 git branch [分支名]
// 切换分支 git checkout [分支名]
// 把指定的分支合并到当前分支上 git merge [分支名]
|
master、 hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD 决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向 master,那么我们现在就在 master 分支上。
HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。
所以切换分支的本质就是移动 HEAD 指针。
4.Git 团队协作机制
1.团队内协作
1 2 3 4 5
| [本地库] <-- pull -- [远程库]
[本地库] -- push --> [远程库]
[本地库] <-- clone -- [远程库]
|
2.跨团队协作
A拥有远程库1,B想协助开发远程库1
1 2 3 4 5 6 7 8 9 10 11 12 13
| B: [远程库1] -- fork --> [远程库2]
B: [本地库2] <-- clone -- [远程库2]
B: [本地库2] -- push --> [远程库2]
B: Pull Request --> A
A: audit(approve or deny)
A: merge -> [远程库1]
A: [本地库1] <-- pull -- [远程库1]
|
5.远程仓库操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // 查看当前所有远程仓库地址别名 git remote -v
// 为远程仓库地址命名 git remote add [remote_repo_name] [remote_repo_url]
// 将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并 git pull [remote_repo_name] [remote_branch]
// 推送本地分支上的内容到远程仓库 git push [remote_repo_name] [local_branch]
// 将远程仓库的内容克隆到本地 git clone [remote_repo_url]
|
6.拉去远程仓库的分支到本地
例如远程仓库有 hot-fix 分支,本地没有,本地想拉取 hot-fix 分支,并合并到本地的 dev 分支
1 2 3 4 5 6 7 8 9 10 11 12
| // 查看所有分支 git branch -a // 本地新建 hot-fix 分支,并与远程仓库的 hot-fix 分支关联 git checkout -b hot-fix origin/hot-fix // 切换到 hot-fix 分支 git chekcout hot-fix // 更新 hot-fix 分支 git pull origin hot-fix // 切换到 dev 分支 git checkout dev // 将 hot-fix 分支的内容合并到 dev 分支 git merge hot-fix
|