---
title: git使用手册
date: 2018.06.17 21:28:00
updated: 2018-06-10 12:00:00
categories:
- web
tags:
- front end
- git
---
目录
# 创本地库
# 全局配置
# 修改仓库
# 版本回退
# ----》回到过去
# ----》重返未来
# 一些概念
# 管理修改
# 远程仓库
# 分支管理
# 标签管理
# 密钥传送
# 命令别名
# 配置文件
# 忽略文件
# 建服务器
# 附录
# 接口
# 问题
# 参考文献
# 使用场景
正文
创建仓库 (~~创版本库~~创本地库)
# step-x: cnf ini
# step-x: 初化配置
YOUR_DIR_PATH="beautify-repo"
YOUR_CMT_MSG="docs(git): note how to use git"
YOUR_HUB="https://github.com/YMC-GitHub"
YOUR_REPO="git-in-action"
YOUR_REMOTE_NAME="origin"
YOUR_REMOTE_BRANCH="master"
YOU_ARE_FIRST_PUSH="true"
# step-x: use cnf for that git repo ini
# step-x: 初化仓库
mkdir -p "$YOUR_DIR_PATH" ; cd "$YOUR_DIR_PATH" ; git init
# step-x: set sth. here for that git repo
# step-x: 复制粘贴
# step-x: get sth. here for that git repo
# step-x: 查看状态
git status
# step-x: set sth. here for that git repo
# step-x: 添加文件
git add .
# step-x: set sth. here for that git repo
# step-x: 提交上传
git commit -m "$YOUR_CMT_MSG"
[ _"$YOU_ARE_FIRST_PUSH" == _"TRUE" ] && git remote add "$YOUR_REMOTE_NAME" "${YOUR_HUB}/${YOUR_REPO}.git"
# step-x: set sth. here for that git repo
# step-x: 首次提交
[ _"$YOU_ARE_FIRST_PUSH" == _"TRUE" ] && git push -u "$YOUR_REMOTE_NAME" "$YOUR_REMOTE_BRANCH"
# step-x: set sth. here for that git repo
# step-x: 第一次之后,这样提交
[ _"$YOU_ARE_FIRST_PUSH" != _"TRUE" ] && git push "$YOUR_REMOTE_NAME" "$YOUR_REMOTE_BRANCH"
全局配置
#step-x: ini cnf
YOUR_NAME="YMC"
YOUR_EMAIL="hualei0304xxxx@163.com" # the email your register in github,gitee,gitlab ...
#step-x: use cnf
git config --global user.name "$YOUR_NAME"
git config --global user.email "$YOUR_EMAIL"
修改仓库
//我的版本
git --version
//查看目录
ls -ah
//查看状态
git status
//查看修改
git diff
//添加文件
git add xx.xx
//存版本库
git commit -m "update sth."
//查看状态
git status
//传远程库
git push origin master
版本回退
/*----------回到过去-------------*/
//查看版本记录
git log
//当前版本HEAD
//上一版本HEAD^
//上上版本HEAD^^
//一百版本HEAD~100
//回退到某版本
git reset --hard HEAD^
/*----------重返未来-------------*/
//查看命令记录
git reflog
//重返未来版本
git reset --hard commit_id
一些概念
本地库
远程库
工作区
版本库
暂存区
某分支
管理修改
//进行修改
//添暂存区
git add xx.xx
//交版本库
git commit -m "update sth."
//撤销修改(回到最近一次git commit或git add时的状态)
git checkout -- xxx.xx
or
git reset HEAD xxx.xx
git checkout -- xxx.xx
//删除文件
rm test.txt
git status
/*----------确实要删除-------------*/
git rm test.txt
git commit -m "remove test.txt"
/*----------误删时恢复-------------*/
git checkout -- test.txt
远程仓库
//创建
git remote add pb https://github.comYMC-GitHub/ticgit
//修改
//重新命名
git remote rename pb paul
//删除
git remote rm paul
//连接
//连远程库
git remote add origin git@github.com:michaelliao/learngit.git
//git remote add origin https://github.com/YMC-GitHub/canvas-captcha-code.git
//修改地址
git remote set-url origin git@github.com:YMC-GitHub/canvas-captcha-code.git
//上传
git push origin master
//下拉
//下远程库
//默认分支:git clone git@github.com:YMC-GitHub/canvas-captcha-code.git
//新的名字:git clone git@github.com:YMC-GitHub/canvas-captcha-code.git new_locall_name
//指定分支:git clone -b dev git@github.com:YMC-GitHub/canvas-captcha-code.git
//指定标签:git clone -b v1.0.0 git@github.com:YMC-GitHub/canvas-captcha-code.git
//建拉分支:git checkout -b dev origin/dev
//下远程库
git pull origin master:master
//git fetch origin master:dev
分支管理
//创建新分支
git branch dev
//转换到分支
git checkout dev
//看当前分支
git branch
//现进行开发
//回到主分支
git checkout master
//把分支合并
git merge dev
//删除某分支
git branch -d dev
//分支策略-开发
git merge --no-ff -m "merge with no-ff" dev
/*************正在dev分支开发时,急需修复master分支上的bug*****************/
//查看状态
git status
//储藏开发
git stash
//添加一些信心备注git stash save 'message...'
//查看状态
git status
//切换分支
git checkout master
//分支创切
git checkout -b issue-101
//修改内容
//交版本库
git add readme.txt
git commit -m "fix bug 101"
//切换分支
git checkout master
//合并分支
git merge --no-ff -m "merged bug fix 101" issue-101
//删除分支
git branch -d issue-101
//切换分支
git checkout dev
//查看状态
git status
//列出储藏
git stash list
//取出储藏
git stash apply
//删除储藏
git stash drop
//列出储藏
git stash list
/*************Feature分支*****************/
//情景:
每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支。
//分支创切
git checkout -b feature-vulcan
//现在开发
//切换分支
git checkout dev
//删除分支
git branch -d feature-vulcan
//强行删除git branch -D feature-vulcan
/*************多人协作*****************/
//获取远程仓库分支
//查看远程仓库信息
git remote -v
//推本地分支到远程
git push origin master
git push origin dev
//伙伴抓取远程分支
git clone git@github.com:michaelliao/learngit.git
//列出抓的分支列表(只显示master)
git branch
//获取远程开发分支
git checkout -b dev origin/dev
//现在开始开发修改
//之后提交分支
git push origin master
git push origin dev
标签管理
//创建标签
git branch
git checkout master
git tag v1.0.0
//列出标签
git tag
//补建标签
git log --pretty=oneline --abbrev-commit
git tag v0.9 f52c633
git tag
//查看标签
git show v0.9
//交远程库
git push origin v1.0.0
//一次性推送全部尚未推送到远程的本地标签git push origin --tags
//删除标签
本地库删除:git tag -d v0.1
远程库删除:git push origin :refs/tags/v0.9
密钥传送
//是否已有
cd ~/.ssh
ls
//创建密钥
//ssh-keygen
ssh-keygen -t ecdsa -C "web-server-aliyun-hualei03042013@163.com" -f /c/Users/home/.ssh/web-server-aliyun3
-b:指定密钥长度;
-e:读取openssh的私钥或者公钥文件;
-C:添加注释;
-f:指定用来保存密钥的文件名;
-i:读取未加密的ssh-v2兼容的私钥/公钥文件,然后在标准输出设备上显示openssh兼容的私钥/公钥;
-l:显示公钥文件的指纹数据;
-N:提供一个新密语;
-P:提供(旧)密语;
-q:静默模式;
-t:指定要创建的密钥类型。
//查看密钥
cat ~/.ssh/web-server-aliyun3.pub
//复制密钥
cat ~/.ssh/web-server-aliyun3.pub | clip
//传服务器
//粘贴复制
//测试
ssh git@github.com
//传至远程
git push origin --tags
//当多个密钥时可能密钥对应不上,用以下方法解决:
//启客户端
eval $(ssh-agent -s)
//写进配置
ssh-add ~/.ssh/web-server-aliyun3
命令别名
# 设置
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
配置文件
# 查看
## 仓库级别:
cat .git/config
## 用户级别:
cat ~/.gitconfig
忽略文件
## 创建
touch .gitignore
git add .gitignore
## 修改
## 追加内容
## 常用配置
https://github.com/github/gitignore
建服务器
##------------------------------------centos
##安装git
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
yum install git
##创建用户
groupadd git
useradd git -g git
##创建证书
cd /home/git/
mkdir .ssh
chmod 755 .ssh
touch .ssh/authorized_keys
chmod 644 .ssh/authorized_keys
##创建仓库
cd /home
mkdir gitrepo
cd gitrepo
git init --bare sample.git
##更改所属
cd /home
chown git:git gitrepo/
cd gitrepo
chown -R git:git sample.git
## 禁用shell登陆
##克隆仓库
# git clone git@192.168.45.4:/home/gitrepo/sample.git
##-----------------------------------------------------Ubuntu
## 安装git
sudo apt-get install git
## 创建用户
sudo adduser git
## 创建证书
## 创建仓库
cd srv
sudo git init --bare sample.git
## 赋予权限
sudo chown -R git:git sample.git
## 禁用shell登录
/etc/passwd
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
## 克隆仓库
git clone git@server:/srv/sample.git
## 管理仓库
### 人少:/home/git/.ssh/authorized_keys
### 人多:[Gitosis](https://github.com/res0nat0r/gitosis)
附录
### 本地库与远程库
git commit操作的是本地库,git push操作的是远程库。
接口
//将本地分支的更新,推送到远程主机
git push <远程主机名> <本地分支名>:<远程分支名>
//新建分支test
git push origin test
//删除分支
git push origin :test
查看文档
git help log
介绍自己
git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
导入仓库
tar xzf project.tar.gz
cd project
git init
编写工程
交暂存区
git add .
交版本库
git commit -m "update sth."
查看历史
git log
问题
问题:
fatal: Unable to create 'F:/webMyLib/image-valication-code-by-canvas/.git/index.lock': File exists.
解决:rm -f ./.Git/index.lock
来源:https://www.cnblogs.com/DonaHero/p/6846626.html
问题:
每次push都需要输入passphrase for key
问题:
git使用sshkey免密码登陆
解决:
https://blog.csdn.net/accountwcx/article/details/46822257
https://www.cnblogs.com/ayseeing/p/3572582.html
https://www.cnblogs.com/weixing/p/5600469.html
问题:
多个密钥
解决:
https://blog.csdn.net/yanzhenjie1003/article/details/69487932?locationNum=4&fps=1
问题:
Git bash Error: Could not fork child process: There are no available terminals (-1)
解决:tasklist, taskkill /F /IM ssh-agent.exe
https://stackoverflow.com/questions/45799650/git-bash-error-could-not-fork-child-process-there-are-no-available-terminals
问题:
warning: LF will be replaced by CRLF in .gitignore.
解决:git config core.autocrlf false
https://blog.csdn.net/sunshine_drizzle/article/details/51075644
问题:
删除中间的某次commit记录
解决:
git branch -b patch
git log
q
git diff
手动修改不要的
git rebase --onto patch~2 patch~1 patch
git add .
git rebase --continue
git checkout master
git merge patch
git diff
手动修改合并无法进行的地方
git commit
https://www.cnblogs.com/zqunor/p/8620335.html
问题:
Auto Merge Failed; Fix Conflicts and Then Commit the Result.
解决:
git status
...
https://blog.csdn.net/trochiluses/article/details/10100719
参考文献
https://git-scm.com/docs/gittutorial
https://help.github.com/articles/checking-for-existing-ssh-keys/
https://gitee.com/all-about-git
使用场景
# 多个密钥
# https://www.awaimai.com/2200.html
# 打包文件
# https://www.awaimai.com/1485.html
# 自动部署
# https://www.awaimai.com/2203.html
# 贡献代码
# https://www.awaimai.com/1598.html