git常用操作

Git常用命令

创建仓库命令

命令 说明
git init [dir] 初始化仓库,在当前目录新建一个Git代码库,基本上是创建一个具有objectsrefs/headrefs/tags和模板文件的.git目录。
git clone[url] 拷贝一份远程仓库,也就是下载一个项目和它的整个代码历史。

配置命令

命令 说明
git config --list 显示当前的Git配置
git config -e [--global] 编辑Git配置文件。
git config [--global] user.name "[name]" git config [--global] user.email "[email address]" 设置提交代码时的用户信息

增加 / 删除文件

命令 说明
git add [file1] [file2] ... 添加指定文件到暂存区
git add [dir] 添加指定目录到暂存区,包括子目录
git add . 添加当前目录的所有文件到暂存区

代码提交

命令 说明
git commit -m [message] 提交暂存区到仓库区
git commit [file1] [file2] ... -m [message] 提交暂存区的指定文件到仓库区

查看信息和历史

命令 说明
git status 查看暂存区状态/显示有变更的文件
git log 显示当前分支的版本历史
git log --oneline 一行显示当前分支的版本历史
git diff 显示暂存区和工作区的差异
git show [commit] 显示某次提交的元数据和内容变化
git show --name-only [commit] 显示某次提交发生变化的文件
git show [commit]:[filename] 显示某次提交时,某个文件的内容
git reflog 显示当前分支的最近几次提交

撤销

命令 说明
git checkout [file] 恢复暂存区的指定文件到工作区
git checkout [commit] [file] 恢复某个commit的指定文件到暂存区和工作区
git checkout . 恢复暂存区的所有文件到工作区
git reset [file] 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset --hard 重置暂存区与工作区,与上一次commit保持一致
git reset [commit] 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
git reset --hard [commit] 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
git reset --keep [commit] 重置当前HEAD为指定commit,但保持暂存区和工作区不变
git revert [commit] 新建一个commit,用来撤销指定commit
后者的所有变化都将被前者抵消,并且应用到当前分支

分支

命令 说明
git branch 列出所有本地分支
git branch -r 列出所有远程分支
git branch -a 列出所有本地分支和远程分支
git branch [branch-name] 新建一个分支,但依然停留在当前分支
git checkout -b [branch] 新建一个分支,并切换到该分支
git branch [branch] [commit] 新建一个分支,指向指定commit
git checkout [branch-name] 切换到指定分支,并更新工作区
git checkout - 切换到上一个分支
git branch -d [branch-name] 删除分支
git merge [branch] 合并指定分支到当前分支

标签

命令 说明
git tag 列出所有tag
git tag [tag] 新建一个tag在当前commit
git tag [tag] [commit] 新建一个tag在指定commit
git tag -d [tag] 删除本地tag
git show [tag] 查看tag信息
git checkout -b [branch] [tag] 新建一个分支,指向某个tag

远程同步

命令 说明
git fetch [remote] 下载远程仓库的所有变动(远程新增或删除分支都能显示
git pull [remote] [branch] 取回远程仓库的变化,并与本地分支合并
git push [remote] [branch] 上传本地指定分支到远程仓库
git push [remote] --force 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --all 推送所有分支到远程仓库

你可能感兴趣的:(git)