撤销工作区中的修改
git checkout -- file
这里表示的是将指定的文件在工作区的修改全部撤销,由于只是撤销工作区中的内容,暂存区中的内容是不受影响的。
- 如果文件没有被添加到暂存区,则撤销修改就会回到和版本库一样的状态。
- 如果文件已经添加到暂存区,撤销修改就会回到添加到暂存区后的状态。
撤销暂存区中的修改
git reset HEAD file
git reset
既可以回退版本,也可以把暂存区的修改回退到工作区。
HEAD
表示最新的版本。
查看每一次提交的变更的文件
git log --stat
git show commitid
git log --oneline --name-only -1
# --name-only 只显示文件名
git log --name-only -1
# --pretty=format:"" 格式化commit message 这里什么都不显示
git log --pretty=format:"" -1
# 最终
git log --pretty=format:"" --name-only -1
git pull 的时候出现refusing to merge unrelated histories
可以在 pull 的时候添加--allow-unrelated-histories
git pull origin master --allow-unrelated-histories
git 拉取远程分支并创建本地分支
#拉取远程分支并在本地创建分支,不会切换到新的分支
git fetch origin remoteBranchName:localBranchName
#拉取远程分支并在本地创建分支,会切换到新的分支
git checkout -b localBranchName origin/remoteBranchName
git 对比两个分支的差别
#比较dev分支比master分支的多提交记录
git log master..dev
git 删除不存在的远程分支的引用
git remote prune origin
Git 流程
participant 工作区
participant 暂存区
participant 本地仓库
participant 远程仓库
工作区->暂存区: git add file
暂存区->本地仓库: git commit
本地仓库->远程仓库: git push
远程仓库->本地仓库: git fetch or git pull
暂存区->工作区: git reset head
冲突的时候
git merge --abort
为 git 命令设置别名
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.pm 'pull origin master'
git 代理的设置方法
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git config --global --unset http.proxy
git config --global --unset https.proxy
npm config delete proxy
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
git 自动换行符
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true
#提交时转换为LF,检出时不转换
git config --global core.autocrlf input
#提交检出均不转换
git config --global core.autocrlf false
git 子模块
git submodule
- 添加一个子模块
git submodule add [email protected] your-directory
- clone 的时候一起克隆子模块
git clone --recursive [email protected]
- 如果没有一起克隆子模块,需要获得子模块执行以下命令:
git submodule init
git submodule update
也可以直接使用git submodule update --init --recursive
git 克隆很慢的解决方案
- 方案 1 使用代理服务器。
- 方案 2 修改 host 文件,这里提一下修改 host 文件的方式
首先访问https://www.ipaddress.com/
在这个网站查询
github.global.ssl.fastly.net
和github.com
对应的公网 ip 地址,然后将它放在 hosts 文件中,这样可以大大提高克隆的速度。