git常用命令:git clone、git push、git add、git commit、git checkout、git pull
解释:
- workspace:工作区
- staging area:缓存区
- local repository:本地仓库
- remote repository:远程仓库
命令 | 说明 |
---|---|
git init | 初始化本地仓库 |
git clone | 从remote下载文件到local |
git add | 添加文件到staging area |
git commit | 将staging area中的内容提交到local repository |
git diff | 比较staging area和workspace内容的差异 |
git status | 查看当前仓库的状态,显示有变更的文件 |
git reset | 回退版本 |
git rm | 删除workspace文件 |
git mv | 移动或重命名workspace文件 |
git log | 查看历史提交记录 |
git blame | 以列表的形式查看指定文件的历史修改记录 |
git remote | 远程仓库操作 |
git fetch | 从远程仓库获取代码 |
git pull | 下载远程代码并合并 |
git push | 上传远程代码并合并 |
首次上传代码
- 在gitlab上创建项目
- 在git bash上配置用户名和邮箱
git config --global user.email ccc@example.com
git config --global user.name ccc
git config --global http.sslVerify false
- git add .
- git commit -m 提交注释
- git pull origin master --allow-unrelated-histories
- git push -u origin master
分支
使用checkout切换分支时,查找顺序:本地分支 --> 远程分支 --> 提示分支不存在
# 查看本地分支
git branch
# 查看远程分支
git branch -r
# 查看所有分支
git branch -a
# 推送本地分支
git push origin test(分支名)
创建并切换分支
# 创建本地分支
git branch my_branch
# 切换本地分支
git checkout my_branch
# 创建并切换本地分支
git checkout -b my_branch
# 重置分支(删除已存在的分支并重新创建,分支不存在也不报错)
git checkout -B my_branch
#本地分支重命名
git branch -m 旧分支命 新分支名
# 当前分支重命名
git branch -m 新分支名
删除分支
# 删除本地分支
git branch -d my_branch
# 删除本地分支(远程分支不存在的情况)
git branch -D my_branch
# 删除远程分支
git push origin --delete my_branch
撤销更改:让工作区的文件回到最近一次git add或git commit的状态。
git checkout -- . (撤销所有文件更改)
git checkout -- file1 file2 (撤销工作区的某些文件更改)
撤销缓存区的更改
gie reset my_branch -- file1 file2
统计代码总量
find . -name "*.txt" -or -name "*.h" -or -name "*.py" -or -name "*.robot" |xargs grep -v "^$"|wc -l
查看个人代码量
git log --author="zhangsan" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
统计每个人的修改行数
git log --format='%aN'|sort -u | while read name;
do echo -en "$name\t";
git log --author="$name" --pretty=tformat: --numstat |awk '{add += $1; subs += $2; loc += $1 - $2} END {printf "added lines: %s, removed lines: %s, total lines: %s\n", add,subs, loc}' -;
done
统计提交数
git log --oneline | wc -l