一、新建代码库
复制代码
在当前目录新建一个Git代码库
$ git init
新建一个目录,将其初始化为Git代码库
$ git init [project-name](project-name这个就是你新建的目录)
下载一个项目和它的整个代码历史
$ git clone [url]
复制代码
二、配置
复制代码
显示当前的Git配置
$ git config --list
编辑Git配置文件
$ git config -e [--global]
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
复制代码
三、增加/删除文件
复制代码
添加指定文件到暂存区
$ git add [file1] [file2] ...
添加指定目录到暂存区,包括子目录
$ git add [dir]
添加当前目录的所有文件到暂存区
$ git add .
$ git rm [file1] [file2] ...
停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]
改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]
四、代码提交
复制代码
提交暂存区到仓库区
$ git commit -m [message]
提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]
提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a
提交时显示所有diff信息
$ git commit -v
使用一次新的commit,替代上一次提交
如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]
重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...
五、分支
$ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
六、标签
$ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
七、查看信息
$ git status
$ git log
$ git log --stat
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file]
$ git whatchanged [file]
$ git log -p [file]
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
八、远程同步
$ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [localbranchname]
$ git push [remote] [localbranchname]
(第一次推送需要加上-u的参数,比如:
git push -u origin master)
$ git push [remote] --force
$ git push [remote] --all
九、撤销
$ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
十、其他
$ git archive
转载请注明原文地址:https://blackberry.8miu.com/read-27472.html