git 常用术语
master 默认开发分支
origin 默认远程版本库
Index/Stage 暂存区
Workspace 工作区
Repository 仓库区(或本地仓库)
Remote 远程仓库
git 创建代码库 在当前目录新建一个git代码库 1 2 3 4 5 6 7 $ git init $ git add README.md $ git commit -m "first commit" $ git remote add origin 【远程仓库地址】 $ git push -u origin master $
已有本地仓,链接远程仓库 1 2 $ git remote add origin 【远程仓库地址】 $ git push -u origin master
指定一个目录,将其初始化为git代码库 1 $ git init [project-name]
下载一个项目和它的整个代码历史
下载一个项目和他最近一次的提交(当要克隆的项目过大,出现TimeOut问题时谨慎使用 ) 1 $ git clone --depth 1 [url]
这种方法克隆的项目只包含最近commit的一个分支,体积很小,但会产生另外一个问题,他只会把默认分支clone下来,其他远程分支并不在本地,所以这种情况下,需要用如下方法拉取其他分支:
1 2 3 $ git remote set -branches origin [remote_branch_name] $ git fetch --depth 1 origin [remote_branch_name] $ git checkout [remote_branch_name]
git 配置信息
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
显示当前的git配置信息
编辑git配置文件 1 $ git config -e [--global]
设置提交代码的用户信息 1 2 $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
git 常用命令 增加、删除、修改文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 $ git status $ git diff $ git add [file1] [file2] ... $ git add [dir] $ git add . $ git add -p $ git rm [file1] [file2] ... $ git rm --cached [file] $ git rm -f --cached [file] $ git mv [file-originName] [file-newName]
git 代码提交 1 2 3 4 5 6 7 8 9 10 11 12 13 $ git commit -m [message] $ git commit [file1] [file2] ... -m [message] $ git commit -a $ git commit -v $ git commit --amend -m [message] $ git commit --amend [file1] [file2] ...
git 分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ git branch $ git branch -r $ git branch -a $ git branch [branch-name] $ git branch --track [branch] [remote-beanch] $ git branch -d [branch-name] $ git push origin --delete [branch-name] $ git branch -dr [remote-branch] $ git checkout -b [branch] $ git checkout -b [new-branch] [origin-branch] $ git checkout [branch-name] $ git checkout - $ git branch --set -upstream [branch] [remote-branch] $ git merge [branch] $ git rebase <branch> $ git cherry-pick [commit]
git 标签 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ git tag $ git tag <tagname> $ git tag -d <tagname> $ git push origin :refs/tags/[tagname] $ git show [tag] $ git push [remote] [tag] $ git push [remote] --tag $ git checkout -b [branch] [tag]
git 查看信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 $ git status $ git log $ git log --stat $ git log -S [keyword] $ git log [tag] HEAD --pretty=format:%s $ git log [tag] HEAD --grep feature $ git log --fellow [file] $ git whatchchange [file] $ git log -p [file] $ git log -5 --pretty --online $ git shortlog -sn $ git blame [file] $ git diff $ git diff --cached [file] $ git diff HEAD $ git diff [first-branch]...[second-branch] $ git diff --shortstart "@{0 day ago}" $ git show [commit] $ git show --name-only [commit] $ git show [commit]:[filename] $ git reflog
git远程操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ git fetch [remote] $ git pull [remote] [branch] $ git remote -v $ git remote show [remote] $ git remote add [short-name] [url] $ git push [remote] [branch] $ git push [remote] --force $ git push [remote] --all $ git push <remote>:<branch/tag-name> $ git push --tag
git 撤销 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ git reset --hard HEAD $ git checkout HEAD <file> $ git revert <commit> $ git log --before="1 days" $ 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 stash $ git stash pop
git 其他
参考链接: 点我查看