40条最常用Git命令总结
2018-02-14 14:47
218 查看
Git的3层结构
working directory:工作区staging index:暂存区
git directory(Repository):版本库
Git中文件的4种状态
untracked:未被追踪Modified:表示工作区修改了某个文件但是还没有添加到暂存区
Staged:表示把工作区修改的文件添加到了暂存区但是没有提交到版本库
Committed:表示数据被安全地存储在本地库中
Git基本命令
1.Git文件操作
Git基本操作初始化:
git init
Initialized empty Git repository in E:/git_test2018/.git/
查询状态:
git status
On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) StringList.cpp StringList.html nothing added to commit but untracked files present (use "git add" to track)
添加暂存区:
git add StringList.cpp
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: StringList.cpp Untracked files: (use "git add <file>..." to include in what will be committed) StringList.html
提交:
git commit -m 'first commit'
[master (root-commit) b32b353] first commit 1 file changed, 139 insertions(+) create mode 100644 StringList.cpp
配置邮箱和姓名信息:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
查看配置信息:
git config --list
$ git config --list core.symlinks=false core.autocrlf=true core.fscache=true color.diff=auto color.status=auto color.branch=auto color.interactive=true help.format=html http.sslcainfo=F:/Git/Git/mingw64/ssl/certs/ca-bundle.crt diff.astextplain.textconv=astextplain rebase.autosquash=true credential.helper=manager filter.lfs.process=git-lfs filter-process filter.lfs.required=true filter.lfs.clean=git-lfs clean -- %f filter.lfs.smudge=git-lfs smudge -- %f user.name=Tom user.email=Tom@qq.com core.repositoryformatversion=0 core.filemode=false core.bare=false core.logallrefupdates=true core.symlinks=false core.ignorecase=true
查看提交信息:
git log [--oneline]
$ git log commit b32b3537154da3e878f1ff07a4c38303ff46a4af Author: Tom <Tom@qq.com> Date: Tue Feb 13 22:36:57 2018 +0800 first commit
提交所有文件:
git add .
直接将工作区文件提交到版本库中(跳过git add命令):
git commit -am 'remodified'
Git撤销操作
git commit --amend: 撤销上一次提交,并将暂存区的文件重新提交
git checkout -- filename / git checkout -- .: 拉取暂存区的文件并将其替换工作区的文件
git reset HEAD -- filename: 拉取最近一次提交的版本库中的这个文件到暂存区,该操作不影响工作区
Git文件删除
删除工作区及暂存区中的该文件相当于删除文件后执行git add
git rm --cache filename:在不小心将不需要追踪的文件添加到暂存区,想删除暂存的文件但是不想删除工作区的文件很有用
git rm -f filename: 当工作区或者暂存区文件修改了,防止把修改误删除了
git mv oldname newname: 相当于 mv oldname newname; git rm oldname; git add newname
Git分支
Git分支的创建、修改、切换、删除查看分支 :
git branch
新建分支 :
git branch branchname
切换分支 :
git checkout branchname
删除分支(先切换到另一分支) :
git branch -d branchname
重名名分支 :
git branch -m oldbranch newbranch
新建分支并切换到该分支 :
git checkout -b branchname
Git分支的合并
git diff: 比较工作区与暂存区文件的差异
git diff --staged: 比较暂存区与版本库的文件差异
git diff 版本号 版本号: 比较分支内的两个版本的差异
git diff 分支 分支: 比较两个分支的最新提交版本的差异
git merge branchnam: 合并之前需要切换到master分支;快速合并与冲突合并
储存变更
git stash
git stash list
git stash apply stash@{0}
git stash pop stash@{1}
git stash drop stash@{0}
Git远程仓库
github上的仓库git push https://github.com/*/*.git master: 本地推送到Github远程仓库
ssh-keygen: 生成ssh私钥
git pull git@github.com:*/*.git master: 从远程仓库拉取到本地(需在远程Github仓库填写本地公钥信息)
修改远程仓库名称 :
git remote add newname git@github.com:*/*.git
查看远程仓库信息 :
git remote -v
添加远程仓库信息 :
git remote add origin ssh://root@*.*.*.*/*.git
拉取到本地 :
git pull origin master
git reset --hard HEAD:撤销本地修改
Git ssh免密登录
ssh-keygen
ssh-copy-id user@host: 将本机的公钥复制到远程服务器的authorized_keys文件中
如果不是自己的服务器,可以将本地公钥发给服务器管理员,添加在authorized_keys文件后面
git fetch origin master: 获取远程仓库最新代码
Git帮助文档
touch .gitignore: 无需版本控制的文件纳入其中
git help 命令
官方文档地址 : https://git-scm.com/docs