git 命令查询
配置
git config --list
列出当前配置
git config [--global] user.name 'gsy'
配置 git 的用户名为 gsy,加了 global 的话是全局的
- 不加 global 是配置当前项目,配置文件在
cat .git/config
里面
- 加上 golbal 是配置当前电脑,配置文件在
cat ~/.gitconfig
里面
git config [--global] user.email 'gsy@gsy.com'
配置 git 邮箱
新建代码库
git init [folder name]
在当前目录新建一个foler name
的代码仓库,不填 名字则为将当前目录变为一个git仓库
git clone <url> [folder name]
将url
指定的仓库克隆到folder name
中
- 仓库太大,clone 老失败和断线
mkdir common && cd common && git init
创建文件夹->进入->将其变为一个仓库
git rmeote add origin git@github.com:gsy13213009/shell.git
添加origin仓库,指向远程仓库
git fetch
拉取代码,如果中途失败再次使用git fetch
,直到成功(有断点续传功能)
git checkout master
切出 master 分支
添加、删除文件
git add .
将当前目录下的所有改动的文件添加到 stage 区
git add [file1] [file2]
将 file1 和 file2 添加到 stage 区
git add [dir1] [dir2]
将 dir1 和 dir2 添加到 stage 区,包含子目录
git rm [file1] [file2]
删除 fiel1 和 fiel2,并且将删除操作放到 stage 区
git rm --cached [file1] [file2]
停止追踪文件,将文件放入untrack
里面,如果该文件之前已被commit 过,则会有一个deleted
的操作被放到 stage 区
提交 commit
git commit -m "xxxxxxxx"
将 stage 区的内容提交到本地仓库
git commit --amend -m "aaaaaaa"
如果 stage 区无内容,则是改写上次 commit 的内容为aaaaaaaaa
- 如果 stage 区有内容,则是将 stage 区的内容追加到上一个 commit,并且改写上一个 commit 的内容为
aaaaaaaa
git commit -v --amend
-v 的意思,是使用交互模式编辑上一个 commit 的内容
- 如果 stage 区无内容,则是进入 commit 内容编辑框,编辑 commit 的内容
- 如果 stage 区有内容,则将 stage 区内容追加到上一个 commit 代码里,并且进入编辑模式,修改 commit 的内容
git commit -v --no-edit --amend
和上一个类似,只是不编辑 commit 的内容,直接将 stage 区域的改动追加到上一个 commit 里面
分支
git branch
列出所有本地分支
git branch -r
列出所有远程分支
git branch -a
列出本地和远端的所有分支
git branch xxx
新建一个 xxx 分支,并且依然停留
在当前分支
git push origin --delete xxx
删除 origin 仓库的 xxx 分支
git branch -d xxx
删除本地仓库 xxx 分支
git checkout -b xxx
新建一个 xxx 分支,并且切换
到 xxx 分支
git checkout -
切换到上一个分支
git merge xxx
merge xxx 分支到当前分支
git cherry-pick [commit id]
将某个 commit 合并到当前分支
标签
git tag
列出所有 tag
git tag xxx
为当前 commit 新建一个名为 xxx 的 tag
git tag -d xxx
删除本地 xxx tag
git push origin :refs/tags/xxx
删除 origin 仓库(远端仓库)的 xxx tag
git show xxx
查看 xxx tag 信息
git push origin xxx
将 xxx tag 推送到 origin 仓库
git push origin --tags
将本地所有 tag 推送到 origin 仓库
git checkout -b branch_xxx xxx
基于 xxx tag 切出 branch_xxx 分支
查看信息
git status
查看当前状态,列出所有变更的文件
git log
查看 log 日志
git log --graph --pretty=oneline
单行模式,图形模式下查看 log
git diff
查看工作区和 stage 暂存区的差异
git diff --cached [file1]
查看 stage 区和上一个 commit 之间的差异
git show [commit id]
查看某个 commit id 的改动
git show --name-only [commit id]
查看某个 commit id 改动的文件
git show [commit id]:[filename]
查看某个 commit 下的某个文件的改动
git reflog
查看当前分支的最近 30 天内的提交日志
- 可以做 reset,checkout 操作,方便恢复到某个 commit 的状态
- 对 commit 丢失,代码丢失很管用
远程仓库
git remote -v
显示远程仓库的状态
git remote add xxx [url]
添加一个名字为 xxx 的远程仓库
git pull [remote] [branch]
拉取 remote 仓库的 branch 分支,合并到本地当前分支
- 注意此时的 remote 不一定是 origin,可能是别的 fork 出去的仓库
- 之前直接使用
git pull
拉取当前分支,默认就是 origin 仓库
git push [remote]
将本地仓库 push 到 remote 指定的仓库,不填的话默认是 origin
git push -f
强制推送不清楚这个命令的话,不要乱用
撤销
git checkout [file]
丢弃工作区
的 file 的改动
git checkout [commit id] file
丢弃 file 的改动,使用 commit id 下的 file 内容覆盖当前内容
git checkout .
丢弃当前工作区
的所有文件的改动(stage 区的保留,untrack 文件保留)
git reset [file]
将 file 文件从stage
区移除,放回工作区
,保留改动
git reset --hard
重置当前工作区
和stage
区,丢弃所有改动
git reset [commit id]
重置当前分支的 HEAD 为commit id,同时重置stage
区,工作区不变,代码保留
git reset --hard [commit id]
重置当前分支 HEAD 为 commit id,同时重置stage
区和工作区
,代码丢弃
stash
git stash
将当前工作区,stage 区内容移除,保留到 stash 里面
git stash list
查看 stash 里面有那些内容
git stash apply 0
apply 第 0 个 stash 的内容,stash 还存在
git stash pop
apply 第 0 个 stash 的内容,stash 第 0 个 pop 掉