目录
一、安装完的基本配置
查看账号
$ git --version # 查看git的版本信息
$ git config --global user.name # 获取当前登录的用户
$ git config --global user.email # 获取当前登录用户的邮箱
设置账号
$ git config --global user.name 'userName' # userName为你的git账号,
$ git config --global user.email 'email' # email为你的git邮箱
# 获取Git配置信息,执行以下命令:
$ git config –list
配置密钥
# 生成公钥私钥,将公钥配置到GitHub服务器,ssh提交就可以免输入用户名密码
# 三次回车即可生成 ssh key
$ ssh-keygen -t rsa
# 查看已生成的公钥
$ cat ~/.ssh/id_rsa.pub
二、新建本地仓库
# 在当前目录新建一个Git代码库
$ git init
# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]
# 下载一个项目和它的整个代码历史
$ git clone [url]
三、分支管理
# 列出所有本地分支
$ git branch
# 列出所有远程分支
$ git branch -r
# 列出所有本地分支和远程分支
$ git branch -a
# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]
# 新建一个分支,并切换到该分支
$ git checkout -b [branch]
# 新建一个分支,指向指定commit
$ git 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 branch -dr [remote/branch]
四、信息查看
# 查看仓库状态,显示有变更的文件
$ git status
# 显示当前分支的版本历史
$ git log
# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat
# 显示暂存区和工作区的差异
$ git diff
# 显示某次提交的元数据和内容变化
$ git show [commit]
五、提交到本地仓库
# 添加指定文件到暂存区
$ git add [file1][file2] ...
# 添加指定目录到暂存区,包括子目录
$ git add [dir]
# 添加当前目录的所有文件到暂存区
$ git add .
# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]
# 提交暂存区到仓库区
$ git commit -m [message]
# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]
# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...
六、标签操作
# 列出所有tag
$ git tag
# 新建一个tag在当前commit
$ git tag [tag]
# 新建一个tag在指定commit
$ git tag [tag] [commit]
# 删除本地tag
$ 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
七、回退操作
# 恢复暂存区的指定文件到工作区
$ git checkout [file]
# 恢复暂存区的所有文件到工作区
$ git checkout .
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]
# 不重置暂存区与工作区,只重置commit记录,与上一次commit保持一致
$ git reset --soft
# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard
# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commitid]
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commitid]
# 暂时将未提交的变化移除,稍后再移入
$ git stash
reset (--mixed):被reset掉的commit和暂存区index中的修改内容都被保存在工作区里,而不是直接丢弃掉。
reset --soft:被reset掉的信息中,commit的修改内容都被保存在index中,而原先index里的修改内容还是原样在index中。
reset --hard:被reset掉的所有信息都全部丢掉了。
八、同步到服务器
# 下载远程仓库的所有变动
$ git fetch [remote]
# 显示所有远程仓库
$ git remote -v
# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]
# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]
# 上传本地指定分支到远程仓库
$ git push [remote] [branch]
# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force
# 推送所有分支到远程仓库
$ git push [remote] --all
九、一般提交流程
git checkout [branch name] # 切换到指定分支
git status # 查看修改文件
git add filename # 添加修改文件到暂存区(本地)
git commit -m "first commit" # 添加文件到本地仓库并提交描述信息
git status # 再次查看修改文件是否本地提交成功
git log # 查看提交记录(此时会显示本地提交)
git pull # 推送前更新到最新版本代码,防止和别人代码产生冲突
git push origin [branch name] # 把本地仓库的文件推送到远程仓库
最后说明一下,这些git命令只是相对比较常用的,有些使用频率较低的没有写在上面。如果有需要,可以专门查找指定功能的命令。