- 配置
- git config --list 显示当前的Git配置
- git config -e --global 编辑Git配置⽂件
- git config --global user.name "lhy" 设置提交代码时的⽤户信息
- git config --global user.email "1562732132@qq.com" 设置提交代码时的⽤户信息
- config --global user.editor "lhy" 设置提交代码时的⽤户信息
- 新建仓库
- git init 在当前⽬录新建⼀个Git代码库
- git init orange-shanxi 新建⼀个⽬录,将其初始化为Git代码库
- git clone projectUrl 下载⼀个项⽬和它的整个代码历史
- 添加和删除文件
- git add gal-rpt-bal/gal-rpt-bal-list.vue gal-rpt-bal/gal-rpt-bal-list.js 添加指定文件到暂存区
- git add gal-rpt-bal 添加指定⽬录到暂存区
- git add . or git add * 添加当前⽬录的所有⽂件到暂存区
- git add *.js 添加所有后缀为js的⽂件到暂存区
- git add -p 对于同一文件的多出变化可以实现分次提交
- git rm gal-rpt-bal/gal-rpt-bal-list.vue gal-rpt-bal/gal-rpt-bal-list.js ... 删除工作区文件,并将这次删除放入暂存区
- git rm --cached gal-rpt-bal/gal-rpt-bal-list.vue 停止追踪指定文件,但该文件会保留在暂存区
- git mv [file-original] [file-renamed] 修改文件名并将其放入暂存区
- 提交代码
- git commit -m "message" 提交暂存区到仓库区
- git commit gal-rpt-bal/gal-rpt-bal-list.vue gal-rpt-bal/gal-rpt-bal-list.js -m message 提交暂存区的指定⽂件到仓库区
- git commit -a 提交⼯作区⾃上次commit之后的变化,直接到仓库区
- git commit -v 提交时显示所有diff信息
- git commit --amend -m message 使⽤⼀次新的commit,替代上⼀次提交,如果代码没有任何新变化,则⽤来改写上⼀次commit的提交信息
- git commit --amend gal-rpt-bal/gal-rpt-bal-list.vue gal-rpt-bal/gal-rpt-bal-list.js 重做上⼀次commit,并包括指定⽂件的新变化
- 撤销
- git checkout --filepathname 放弃工作区某个文件的更改
- git checkout . 放弃工作区所有文件的更改
- git reset filepathname 撤销暂存区某个文件的add
- git reset or git reset HEAD . 撤销暂存区所有文件的add
- git reset [commit] or git reset --mixed [commit] 撤销仓库中的某个commit,重置暂存区,但工作区不变
- git reset --soft [commit] 撤销仓库中的某个commit,暂存区不变
- git reset --head [commit] 撤销仓库中的某个commit,重置暂存区与工作区(文件修改会丢失)
- git revert [commit] 新建一个commit,用来撤销指定commit,后者的所有变化都将被前者抵消,并且应用到当前分支
- 分支
- git branch 列出所有本地分支
- git branch -r 列出所有远程分支
- git branch -a 列出所有本地分支和所有远程分支
- git branch [branch-name] 新建一个分支,但依旧停留在当前分支
- git checkout -B [branch-name] 新建一个分支,并切换到该分支
- git checkout -q [branch-name] or git checkout -B [branch] origin/[branch] 以remote分支为基础,新建一个分支,并切换到该分支
-
git branch [branch] [commitId] 新建一个分支,指向指定commitId
-
git branch --track [branch] [remote-branch] 新建一个分支与指定的远程分支简历追踪关系
-
git branch --set-upstream [branch] [remote-branch] 在现有分支和远程分支之间建立追踪关系
-
git checkout [branch-name] 切换到指定分支,并更新工作区
-
git checkout - 切换到上一个分支
-
git merge [branch] 合并指定分支到当前分支
-
git rev-parse --short HEAD 获取分支commit Id
- git cherry-pick [commit] 选择一个commit,合并进当前分支
- git branch -D [branch-name] 删除分支
- git push origin --delete [branch-name] or git push origin : [branch-name] 删除远程分支
- 标签
- git tag 列出所有tag
- git tag [tag] 新建一个tag在当前commit
- git tag [tag] [commit] 新建一个tag在指定commit
- git tag -d [tag] 删除本地tag
- git push origin :refs/tags/[tagName] 删除远程tag
- git show [tag] 查看tag信息
- git push [remote] [tag] 提交指定tag
- git push [remote] --tags 提交所有tag
- git checkout -b [branch] [tag] 新建一个分支,指向某个tag
- 远程同步
- git fetch [remote] 下载远程仓库所有变动
- git fetch --all 刷新远程仓库
- git fetch --prune 同步远程仓库所有分支
- git remote -v 显示所有远程仓库
- git remote show [remote] 显示某个远程仓库的信息
- git remote -update --prune 同步远程仓库所有分支,并删除本地多余分支
- git remote add [shortname] [url] 增加一个新的远程仓库,并命名
- git remote rm [shortname] 移除一个远程仓库
- git pull [remote] [branch] 取回远程仓库的变化,并与本地分支合并
- git push [remote] [branch] 上传本地指定分支到远程仓库
- git push [remote] --force 强行推送当前分支到远程仓库,即使有冲突
- git push [remote] --all 推送所有分支到远程仓库
- 查看信息
- git status 查看有变更的文件
- git log 显示当前分支的版本历史
- git log --oneline 显示commit历史,以及每次commit发⽣变更的⽂件
- git log -5 --pretty --oneline 显示过去5次提交
- git log --stat --oneline参数可以将每条⽇志的输出为⼀⾏,如果⽇志⽐较多的话,⽤这个参数能够使结果看起来⽐较醒⽬
- git log -S [keyword] 根据关键词搜索提交历史
- git log --follow [file] or git whatchanged [file] 显示某个⽂件的版本历史,包括⽂件改名
- git log -p [file] 显示指定⽂件相关的每⼀次diff
- git shortlog -sn 显示所有提交过的⽤户,按提交次数排序
- git blame [file] 显示指定⽂件是什么⼈在什么时间修改过
- git diff 显示暂存区和⼯作区的差异
- git diff --cached [file] 显示暂存区和上⼀个commit的差异
- git diff HEAD 显示⼯作区与当前分⽀最新commit之间的差异
- git show [commit] 显示某次提交的元数据和内容变化
- git show --name-only [commit] 显示某次提交发⽣变化的⽂件
- git show [commit]:[filename] 显示某次提交时,某个⽂件的内容
- git reflog 显示当前分⽀的最近⼏次提交
- 仓库完整迁移
- git clone --bare [old-git-address] 从原地址克隆一份裸版本库
当前⽬录下会产⽣⼀个 xxx.git 的⽂件夹,这个步骤,就是克隆git每⼀次的提交信息,和本地的代码没有关系,只要线上的代码是最新的,这个git版本就是完整的。 - cd xxx.git and git push --mirror [new-git-address] 推送给裸版本库到新的地址