Git常用命令

常用命令

初始化用户

1
2
git config --global user.name "Your Name"
git config --global user.email "Your Email"

建立仓库

1
2
mkdir storehouse    //建立仓库,可以手动创建文件夹
git init //初始化仓库

添加文件

1
2
3
4
git add .               //添加所有文件
git add file //添加单个文件
git add 文件夹/ //添加整个文件夹及内容
git add *.文件类型 //添加目录中所有此文件类型的文件

提交到仓库

1
git commit -m "commit message"  //提交文件到仓库,并注释message

查看仓库修改情况

1
git status      //查看仓库修改情况

回退版本

1
2
git reset --hard HEAD~1     //回退到上一个版本
git reset --hard commit_id

撤销修改

1
2
3
git reset --merge HEAD~1    //撤销修改
git checkout -- file
git reset HEAD <file> //已add

删除

1
2
3
4
git rm file                 //删除文件
git rm -r folder //删除文件夹及内容
git commit -m "message"
git checkout --file //假如删错了,可回退

使用远程仓库,例如github

添加远程仓库、上传到github

1
2
3
4
5
6
7
8
cd ~/.ssh/                  //如果没有执行第二行代码,有的话执行第三行代码
ssh-keygen -t rsa -C "user@XX.com"
//win在C:\Users(用户)\用户名(Administrator)\.ssh ,linux在/root/.ssh
cat ~/.ssh/id_rsa.pub
//复制终端上的公钥黏贴到github.Settings.SSH and GPG keys,或者直接找到文件,文本编辑器打开复制
git remote add origin //添加远程仓库
git pull --rebase 本地仓库名 分支名(master) //先合并
git push -u origin master //上传到`github`

删除当前关联的remote仓库

1
2
git remote -v                   //查看当前关联的仓库
git remote rm origin //删除当前关联的remote仓库

远程仓库克隆

1
git clone https://github.com/RealChuan/AppTools.git

分支管理

分支操作

1
2
3
4
5
6
7
8
9
git branch                     //查看当前分支
git branch -a //查看所有分支
git branch -d branch_name //删除分支
git branch -D branch_name //删除分支,并删除对应的commit
git branch -m branch_name //重命名分支
git checkout branch_name //切换分支
git checkout -b branch_name //创建并切换分支
git merge 分支名 //先切换回master,再合并分支
git log --graph //查看分支合并图

子模块管理(submodule)

添加子模块

1
git submodule add https://github.com/ftylitak/qzxing.git 3rd/submodule/qzxing

更新子模块

1
git submodule update --init --recursive

删除子模块

1
2
3
4
git submodule deinit -f 3rd/submodule/qzxing
git rm -f 3rd/submodule/qzxing
git rm -rf 3rd/submodule/qzxing
git rm --cached 3rd/submodule/qzxing

其他操作

提交中文文件

1
git config --global core.quotepath false   //识别中文

多分支提交时:git配置过程中fatal:拒绝合并无关的历史

1
2
3
4
//首先将远程仓库和本地仓库关联起来:
git branch --set-upstream-to=github/master master
//然后使用git pull整合远程仓库和本地仓库
git pull --allow-unrelated-histories

统计仓库代码行数

1
2
其中 -name “*.cpp” 就表示扩展名为.cpp的文件。
find . "(" -name "*.cpp" -or -name "*.h" ")" -print | xargs wc -l

统计用户所写代码行数

1
git log --author="$(git config --get user.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 }'

windows下客户端升级

1
git update-git-for-windows

强制覆盖本地的代码

1.拉取所有更新

1
git fetch --all

2.分支重置

1
git reset --hard origin/<branch_name>

设置代理和取消代理

1.设置代理

1
2
git config --global http.proxy http://proxy.xx.com:8080
git config --global https.proxy http://proxy.xx.com:8080

2.取消代理

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

忽略文件权限变化

1
2
3
4
//全局忽略文件权限变化
git config --global core.filemode false
//仅忽略当前仓库的文件权限变化
git config core.filemode false

大小写敏感

1
2
3
4
//全局大小写敏感
git config --global core.ignorecase false
//当前仓库的大小写敏感
git config core.ignorecase false

合并上一次提交的内容

1
git commit --amend --no-edit

变基

1
2
// 修改向前的10次提交(commit)
git rebase -i HEAD~10