注册账号并验证邮箱
设置全局git变量
- git config –global user.name
"${userName}"
- git config –global user.email
${userEmail}
- git config –global color.status auto
- git config –global color.branch auto
查询全局变量
- git config –list
密钥管理
- 获取本机公钥: cat ~/.ssh/id_rsa.pub
- 将本机公钥上传至个人github账户
个人项目
创建远程仓库
- github.com/
${userName}
新建repository,名称为${repositoryName}
初始化本地仓库
- 进入本地代码目录 cd ${Localpath}/
${repositoryName}
- git初始化 git init
安装图形化工具github桌面版
- 在图形工具中直接 commit 所有文件
添加远程仓库链接
- git remote add origin https://github.com/`${userName}`/`${repositoryName}`.git
提交本地代码至远程仓库
- git push -u origin master
更新远程代码至本地
- git fetch origin
合并代码
- git merge origin/master
提交本地代码至个人远程仓库
- git push origin master
他人项目
将他人项目folk至个人github名下
- 网页上直接folk
从个人远程仓库下载代码至本地
- 下载至本地:git clone https://github.com/`${userName}`/`${repositoryName}`.git
- 下载至本地其他分支:git clone -b
${BRANCH}
https://github.com/`${userName}`/`${repositoryName}`.git
添加远程仓库链接upstream
- 进入本地代码目录
- git remote add upstream https://github.com/`${otherUserName}`/`${repositoryName}`.git
从远程仓库更新代码
- git fetch upstream
- 更新远程某分支的代码:git fetch upstream
${remoteBranch}
合并远程代码
- git merge upstream/master
- 把远程的某分支代码合并到本地的某分支:git merge upstream/
${remoteBranch}
[:${localBranch}
]
提交本地代码至个人远程仓库
- git push origin master
从个人远程仓库向目标仓库提交代码
- 发起merge request
仓库、分支命令
查询代码的远程仓库及url
- git remote -v
查询代码的远程分支名称
- git branch -r
查询代码的所有本地分支
- git branch
创建本地分支
- git branch
${branchName}
删除分支
- git branch -d
${branchName}
切换分支
- git checkout
${branchName}
文件处理命令
查询本地代码修改状态
- git status
添加本地已修改代码至本地待提交列表
- git add .
将本地待提交列表中的代码提交至待推送列表
- git commit -m “Initial commit”
查询本地代码版本记录
- git log
撤销本地文件的修改
- git checkout – src/main/user.java
- 仅限未commit的已修改的本地代码
建议使用gui工具提交代码
- github
- sourcetree
版本命令
reset撤销本地commit,恢复已commit文件的修改部分到待commit区
- git reset –soft HEAD^
reset撤销最近3次本地commit,这3次本地修改将不会恢复
- git reset –hard HEAD^3
revert撤销上次的commit
- git revert HEAD
- 原commit提前记录在log中还存在,新增一条commit内容为”revert ‘commit2’:this reverts commit…”
撤销远程push
- git reset –hard {version}
- git push -f origin master
冲突处理
commit前发现冲突
- 在commit前要先fetch、merge,此时如果有冲突,则会有提示
- 使用 git checkout – src/XX.java依次撤销冲突文件的修改(提前备份到sublime中)
- 此时merge时不会有冲突
- 将备份到sublime中的文件恢复
- 此时再处理冲突的部分
- 处理完结再commit push