init
在本地创建仓库
# 在当前目录新建一个Git代码库
git init
# 新建一个目录,将其初始化为Git代码库
git init [project-name]
clone
从远程仓库下载,生成本地仓库
# 下载一个项目和它的整个代码历史
git clone [url]
#本地备份
git clone d1 d2
add
添加内容到缓冲区
# 添加当前文件夹
# 如果在根目录代表所有内容
git add .
# 添加文件或者目录,多个用空格隔开
git add <file> <file>
commit
提交到本地仓库
#work dir to repository
git commit -a
# see diff info
git commit <file> -v
rm
删除文件
git rm <file>
git rm <file> --cached only remove from index
git rm <file> -force removal
mv
移动或者重命名文件
git mv file-origin file-renamed
checkout
# 从index恢复到workspace
git checkout <file>
# 从local repository恢复到index和workspace
git checkout HEAD <file>
reset
# 恢复暂存区的文件
git reset head <file>
# 重置提交 HEAD~表示上次的提交HEAD
git reset HEAD~
# 重置模式
git reset [mode]
mode:
- --soft
- --mixed
- --hard 重置暂存区和工作区
- --merge
- --keep
branch
分支
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地分支和远程分支
git branch -a
# 新建一个分支,但依然停留在当前分支
git branch [branch-name]
# 把本地分支发布到远程分支
git push origin [branch-name]
# 新建一个分支,并切换到该分支
git checkout -b [branch]
# 新建一个分支,指向指定commitgit branch [branch] [commit]
# 新建一个分支,与指定的远程分支建立追踪关系
git branch --track [branch] [remote-branch]
# 切换到指定分支,并更新工作区
git checkout [branch-name]
# 切换到上一个分支
git checkout -
# 建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
# 合并指定分支到当前分支
git merge [branch]
# 选择一个commit,合并进当前分支
git cherry-pick [commit]
# 删除本地分支
git branch -D [branch-name]
# 删除远程分支
git push origin --delete [branch-name]
git push origin :[branch-name]
git branch -dr [remote/branch]
# 在当前版本,创建远程分支(没有),更新(存在)
git pull origin branch_name
diff
比较
# 比较workspace和index
git diff
# 比较workspace和local repository
git diff HEAD
# 比较index和local repository
git diff --staged
git diff --cached
# 两次提交
git diff <id1> <id2>
# 两个分支
git diff <branch1> <branch2>
# 仅比较统计信息
git diff --stat
log
提交记录
# 查看该文件提交记录
git log <file>
# 详细
git log -p <file>
# 近两次
git log -2
#查看某次提交开始的所有提交
git log <commit-id>
#查看某次提交的某个文件
git log <commit-id> <file>
show
查看各种对象类型
# 查看提交的细节
git show summit_id
remote
查看,添加,删除,修改远端仓库
# 查看远程仓库
git remote
# 查看详细远程仓库
git remote -v
# 添加远程仓库,并没有push
git remote add origin git@github.com:liufang8/javascript.git
# 移除远程仓库
git remote rm [name]
pull
获取并合并其他的仓库
它是下面命令的缩写:git fetch git merge FETCH_HEAD
# 拉取远程仓库
# <远程主机名> 是origin
git pull <远程主机名> <远程分支名>:<本地分支名>
# 如果和当前分支合并,可以省略冒号和后面部分
git pull origin master
# 默认情况本地分支和同名的远程分支建立一种追踪关系,
# 手动建立追踪关系
git branch --set-upstream master origin/next
# 如果当前分支和远程分支存在踪关系,省略远程分支
git pull origin
push
将本地代码推送到远程仓库
git push origin master
stash
暂时隐藏工作区和暂存区的修改,保存本地的修改
切换分支的时候,不用提交本地的修改
# 隐藏本地工作区和暂存区的修改
git stash
# 查看隐藏的操作列表
git stash list
# 恢复上次隐藏的代码
git stash pop
config
配置
git config --global core.autocrlf false
其他
文件夹状态:
- 绿色对号: 已提交
- 红色感叹号: 已经添加未提交
- 红色加号: 为已添加
- 没有符号:该文件或者文件夹被忽略
HEAD: 当前分支(current branch)或者在当前分支最后被提交的状态