1、创建一个git仓库,生成.git文件 git init
2、克隆指定分支 git clone -b 分支名 仓库地址
3、查看当前分支 git branch
4、查看所有分支 git branch -a
5、切换分支 git checkout 分支名
以某个分支为基础创建新分支 git checkout -b 新分支名 基准分支名
6、拉代码
git pull 从远程仓库获取最新提交,并将其合并到当前分支(相当于 git fetch + git merge)
git fetch 从远程仓库获取最新提交,但不会自动合并
7、添加至缓冲区 git add
git add -A 提交所有变化
git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
8、提交代码 git commit -m "注释"
9、推送代码 git push
推送本地新建的分支 git push --set-upstream origin 分支名
缩写 git push -u origin 分支名
10、版本回退 git reset
git reset --hard HEAD^ 回退上一个版本 (HEAD代表当前版本,有一个^代表上一个版本,以此类推)
git reset --hard d7b5:回退到指定版本(其中d7b5是想回退的指定版本号的前几位)
11、删除文件 git rm 文件名称
12、忽略文件.gitignore
新加.gitignore只能忽略那些原来没有被提交过的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
解决办法:删除本地缓存,改成未track状态
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
13、查看日志 git log
14、取消合并未提交更改 git merge --abort
取消合并已提交更改 git reset --hard HEAD@{1}
15、比较文件修改前后差异 git diff 文件名称
stash
# 保存当前未commit的代码
git stash
# 保存当前未commit的代码并添加备注
git stash save "备注的内容"
# 列出stash的所有记录
git stash list
#stash@{0}: WIP on ...
#stash@{1}: WIP on ...
#stash@{2}: On ...
# 删除stash的所有记录
git stash clear
# 应用最近一次的stash
git stash apply
# 应用第二条stash
git stash apply stash@{1}
# 应用最近一次的stash,随后删除该记录
git stash pop
# 删除最近的一次stash
git stash drop
reset --soft
回退已提交的commit,并将commit的修改内容放回暂存区。
#让 commit 记录强制回溯到某一个节点
git reset --hard
#回溯节点外,还会保留节点的修改内容
git reset --soft
# 恢复最近一次 commit
git reset --soft HEAD^
#已 push 的commit再次 push 时,由于远程分支和本地分支有差异,需要强制推送 git push -f 来覆盖被 reset 的 commit
git push -f
tag
git tag <tagname> 创建标签
git tag -a <tagname> -m "message" 创建带备注的标签
git push origin <tagname> 推送具体标签
git push origin -tags 推送本地所有标签
git tag -d <tagname> 删除本地标签
git push origin -delete <tagname> 删除远程标签