一、一般配置
# 查看当前git版本信息
git --version
# 获取当前用户的用户名
git config --global user.name
# 获取当前用户登录的邮箱
git config --global user.email
# 用linux中的vim编辑全局配置
git config -e
二、登录git
# 设置git账户用户名
git config --global user.name "your-user-name"
# 设置git账户用户邮箱
git config --global user.email "your-user-email"
三、新建代码库
# 在当前目录下创建一个git仓库
git init
# 新建一个目录,将其初始化为一个git仓库
git init [project-name]
# 下载一个项目和它保存的所有日志
git clone [url]
# 下载项目中的某个分支及其所有日志
git clone -b [branch-name] [url]*
四、增加/删除文件
# 将文件添加到暂存区
git add [file-name]
# 将当前目录的所有文件添加到暂存区
git add .
# 将多个文件添加到暂存区
git add [file1] [file2] [file3] ...
# 将全部文件添加到暂存区
git add -a
git add \*
# 删除工作区文件,并将本次删除放入暂存区
git rm [file1] [file2] [file3] ...
# 修改文件名,并将此次操作放入暂存区
git mv [file-original] [file-renamed]
五、代码提交
# 将暂存区的内容添加到版本库(仓库区)
git commit -m "备注信息"
# 提交暂存区指定的文件到版本库(仓库区)
git commit [file1] [file2] [file3] ... m "备注信息"
六、分支管理
分支的好处:
- 同时并行推进多个功能开发,提高开发效率
- 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。
# 列出本地所有分支
git branch
# 列出所有远程分支
git branch -v
# 列出所有本地分支和远程分支
git branch -a
# 新建一个分支,但依然使用当前分支,不做切换动作
git branch [branch-name]
# 新建一个分支,并且切换当前分支为该分支
git checkout -b [branch]
# 切换分支,并更新工作区
git checkout [branch-name]
# 切换至上一个分支
git branch -
# 删除分支
git branch -d [branch-name]
# 删除远程分支
git push origin --delete [branch-name]
git branch -mr [remote/branch]
七、查看信息
# 显示有变更的文件
git status
# 显示当前分支的版本历史
git log
# 显示commit历史,以及每次commit发生变更的文件
git log --stat
# 根据关键字搜索提交历史
git log -S [keyword]
# 显示某个文件的版本历史
git log --follow [file]
git whatchanged [file]
# 显示所有提交过的用户和提交次数
git shortlog -sn
# 显示暂存区和工作区的代码差异
git diff
# 显示工作区与当前分支最新commit之间的差异
git diff HEAD
# 显示某次提交的元数据和内容变化
git show [commit]
# 显示最近几次提交
git reflog
八、远程同步
# 下载远程仓库的所有变动
git fetch [remote]
# 显示所有远程仓库
git remote -v
# 新增一个远程仓库并命名
git remote add [shortname] [url]
git remote add origin https.....git
# 取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]
# 上传本地指定分支到远程仓库
git push [remote] [branch]
# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force
# 推送所有分支到远程仓库
git push [remote] --all
九、撤销
# 恢复暂存区的指定文件到工作区
git checkout [file]
# 恢复某个commit的指定文件到暂存区和工作区
git checkout [commit] [file]
# 恢复暂存区的所有文件到工作区
git checkout .
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
# 重置暂存区与工作区,与上一次commit保持一致
git reset --hard
# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
git reset [commit]
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
git reset --hard [commit]
# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
git reset --keep [commit]
# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
git revert [commit]
# 暂时将未提交的变化移除,稍后再移入
git stash
git stash pop
十、git 清理本地账户信息
git config --system --unset credential.helper
git config --global credential.helper wincred
# 清除掉缓存在git中的用户名和密码
git credential-manager uninstall
十一、永久在本地保存用户名密码
# 先输入下面这条命令,执行完成后,执行一次git push,输入用户名及密码,即可记录成功
$ git config credential.helper store
$ git push
上传一套:
git init初始化
更新项目 git pull origin master
后面接一个点就是add该目录下全部文件 git add .
添加注释 git commit -m "提交注释"
提交上git git push origin master