开发工具之GIT
Posted xiaohua7988
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发工具之GIT相关的知识,希望对你有一定的参考价值。
GIT WORKFLOW
this readme created on 2019.07.28
by Suarez7988
这是一遍介绍git版本控制流程的中文说明,必须通篇阅读一下
https://github.com/oldratlee/translations/tree/master/git-workflows-and-tutorials
此教程参考公司gitlab仓库写作,从注册、拉取远程仓库、到提交与冲突解决
使用的是“功能分支工作流”模式,详见子章节
https://github.com/oldratlee/translations/blob/master/git-workflows-and-tutorials/workflow-feature-branch.md
1. edit gitconfig like the example as follows:
the git config file is: ~/.gitconfig
[user]
name = suarez
email = suarez7988@XXX.cn
[alias]
log = log --color
st = status
co = checkout
br = branch
up = rebase
ci = commit
dw = diff --color-words
[color]
ui = true
diff = auto
[core]
editor = vim
2. config ssh key
login https://gitlab.xxx.com/
add your ssh key in https://gitlab.xxx..com/profile/keys
you can add many keys in different development environment by yourself
3. copy data repository to local
git clone git@GIT_URL:THIS_PROJECT YOUR_DIRECTORY
if you get "Permission denied (publickey)" problem, See 8.g.
4. create or switch to your own branch
if the project is just created, your should goto 8.i.
cd YOUR_DIRECTORY
git checkout -b YOUR_BRANCH_NAME master
if your branch exists, you can also:
git checkout YOUR_BRANCH_NAME
5. do the work on your own branch
git checkout YOUR_BRANCH_NAME
git status
git add <changed files>
git commit -m "changes reasons"
6. publish your code review(CR)
git checkout master # 将本地代码切换到本地的master分支
git pull origin master master # 将origin/master分支的改动更新到本地的master分支
git checkout YOUR_BRANCH_NAME # 将本地代码切换到本地的YOUR_BRANCH_NAME分支
git pull origin master YOUR_BRANCH_NAME # 将origin/master的改动更新到本地的YOUR_BRANCH_NAME分支,这步可能会报冲突Conflicts字样,如果冲突,请先解决冲突,然后再继续下面流程,解决冲突的办法见下面8.e
git push -u origin YOUR_BRANCH_NAME # 将本地YOUR_BRANCH_NAME的改动推送到远端origin/YOUR_BRANCH_NAME
git diff master YOUR_BRANCH_NAME > patch[_main change reason].txt # 比较本地YOUR_BRANCH_NAME分支和master分支的不同
if your level is developer, you should create Merge Request on the webpage
- go to the project webpage
- if it shows a ‘create Merge Request‘ button click it
(or choose ‘Merge Requests‘ click button ‘New Merge Requests‘ select project and YOUR_BRANCH_NAME for ‘Source branch‘
select the same project and master for ‘Target branch‘ click ‘Compare branches and continue‘) - write main change reason for ‘Title‘
- select reviewer for ‘Assignee‘
- click ‘Submit merge request‘
the viewer will receive a mail for this merge request.
after he/she review the code, if he/she ‘Close merge request‘, you should goto step 5 and correct your code; if he/she ‘Accept merge request‘, NOTE your branch will merge to master directly.
if your level is master or higher, you can also do the following directly:
git checkout master
git pull
git pull origin YOUR_BRANCH_NAME
git push origin master
7. tag for code publish(only for SA)
git tag -f yyyymmddhh_tag
git push --tags
8. addtional operations
8.a. see all the branch including remote and local
git branch -a
8.b. delete useless branch
delete local branch
git branch -d YOUR_BRANCH_NAME
delete remote branch
git push origin :YOUR_BRANCH_NAME
8.c. you know
git blame <file>
8.d. only add all tracked file
git add -u
8.e. resolve a conflict
use "git status" to see where the problem is
冲突是常见的,不代表你的提交是错误的,当你和另外一个同事同时修改了同一个文件,并且他先于你把这份文件提交到origin/master分支上时,你在git pull origin master YOUR_BRANCH_NAME时才可能冲突,冲突发生时terminal里会显示如下字样:
Auto-merging xxx.py
CONFLICT (content): Merge conflict in xxx.py
Automatic merge failed; fix conflicts and then commit the result.
if there is still problem exsits, do the following operation:
冲突发生后,切忌慌张,切忌逃避,切忌埋怨同事,解决办法也很简单
主要方式就是先编辑冲突的文件,然后git commit提交。
冲突的文件中,<<<<<<与=====中的代码为服务器改动的代码,=====与>>>>>>中的代码为本地改动的代码,
手动编辑冲突的部分,理解同事的修改逻辑,需要你将同事的改动精髓和你的改动相融合,
如果你是第一次处理冲突,请自行上百度搜索git冲突解决方案,来一次愉快的学习。
严禁没解决冲突就直接发merge request!
8.f. merge newest code to local branch
if you just want your local branch merge newest code
git pull origin master
but if you want to update your local master and local branch at the same time, goto 6
8.g. solve "Permission denied (publickey)" problem
first, check your key is add to the profile of your gitlab
then do following command to see if your key located correctly
ssh -vT git@GIT_URL
in most cases, change the key file name to ~/.ssh/id_rsa will solve the problem
but if you have more than one key, you can do the following suggestion
edit the file ~/.ssh/config
Host "GIT_URL"
HostName "GIT_URL"
User "git"
IdentityFile "~/.ssh/YOUR_KEY_FILE"
if the result of "ssh GIT_URL" is:
PTY allocation request failed on channel 0
Welcome to GitLab, hongjun.liu!
Connection to gitlab.xxx.com closed.
thank GOD, you finally succeed
then you can clone the remote repository use following command:
git clone git@GIT_URL:THIS_PROJECT YOUR_DIRECTORY
for more information you can goto https://help.github.com/articles/error-permission-denied-publickey
8.h. revert local commit
if you get "Your branch is ahead of ‘origin/master‘ by n commit", but you do not want to push these changes
git reset --hard HEAD~n
8.i. start a new project
git clone git@GIT_URL:hongjun.liu/PROJECT_NAME.git
cd PROJECT_NAME
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
NOTE that, the very first commit should push at the first time, this step create a remote branch called ‘master‘.
以上是关于开发工具之GIT的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程