1、Git安装与配置
1.1安装
Debian/Ubuntu
$ apt-get install git
Centos/RedHat
$ yum install git-core
# 源码安装
https://git-scm.com/download
Windows(Git Bash)
# 安装包下载地址
https://gitforwindows.org/
# 官网慢,可以用国内的镜像
https://npm.taobao.org/mirrors/git-for-windows/
Mac安装
http://sourceforge.net/projects/git-osx-installer/
1.2 配置
/etc/gitconfig文件:所有用户 --system
~/.gitconfig:当前用户 --global
.git/config:当前项目 --local (default)
$ git config --list
2、基本概念
3、Git命令
说明:
workspace:工作区
staging area:暂存区/缓存区
local repository:版本库/本地仓库
remote repository:远程仓库
# 创建本地仓库命令,默认在当前目录下创建。其中,local-project-name默认为url中的userProjectname,可自定义。
# 方式1:初始化一个空的本地仓库,后续远程仓库操作前,需添加remote(git remote add)
git init
# 方式2:克隆一个远程仓库为本地仓库,会自动新增一个remote:origin。可以对其进行重置url(git remote set-url --add [newurl])
git clone [url] [local-project-name]
# 远程仓库不存在,添加命令,默认在当前项目下添加。
git remote add [remote-name] [remote-url]
# 远程仓库已经存在,修改其url命令
git remote -v
git remote set-url [name] [newurl]
# 提交或修改
git add .
git commit -m '第n次提交'
# 分支管理
# 创建新分支
git branch [branch-name]
# 创建新分支并立即切换到该分支下进行操作
git checkout -b [branch-name]
# 合并分支(后续继续完善)
git merge [branch-name]
# example
# 查看分支发现有两个分支,A分支和B分支。
# 实现:将B分支合并到A分支。
git branch
git checkout A
git merge B
# 删除本地分支
git branch -d B
# 删除远程分支
git push origin --delete <要删除的远程分支名>
# 误删分支解决
# 查看日志
git reflog
# 精确查找
git reflog | grep
#查看所有tag
git tag
# 查看当前分支tag
git show-ref --tags
# 打tag
git tag -a <tag-name> -m <版本信息> # 示例: git tag -a v0.0.1 -m '第一版稳定版本'
# 删除tag
git tag -d <tag-name>
# 推送tag到远程仓库
git push origin <tag-name>
# 删除远程tag
git push origin :refs/tags/<tag-name>
# 上传下载
# 从远程获取代码并合并到本地的版本
git pull [remote-name] [remote-branch]:[local-branch]
# 将本地的分支版本上传到远程并合并
git push [remoye-name] [local-branch]:[remote-branch]
4、问题
4.1 git clone/push/pull时遇到的问题
gnutls_handshake() failed: An unexpected TLS packet was received.
解决办法:修改https为http。
4.2 git push时遇到的问题
根本原因:两个库里基本文件不一致,远程库里没有README.rd文件。
解决办法:
git pull --rebase origin master
git push origin master
解决办法:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"