Git 命令
git 命令简单总结git cheat sheet
1.创建create
- 从远程clone仓库到本地当前目录
clone an existing repository$ git clone ssh://user@domain.com/repo.git
- 在当前目录下新建一个本地仓库
create a new local repository$ git init
2. 本地仓库改变local changes
- 查看你的工作目录下文件状态的改变情况
changed files in your working deiretory$ git status
- 查看工作目录和暂存区的对比
changes to tracked files$ git diff
- 增加所有当前改变到暂存区,以进行下一次commit
add all current changes to the next commit$ git add .
- 增加一些改变文件到暂存区,已进行下一次commit
add some changes in <file> to the next commmit$ git add -p <file>
p表示特指吧</file> - 提交所有改变从暂存区到本地仓库
commit all local changes in tracked files$ git commit -a
- 提交先前暂存区的所有改变到本地仓库
commit previously staged changes$ git commit
- 改变上一次commit的状态信息
change the last commit(don't amend published commits)$ git commit --amend
3. 查看日志信息commit history
- 显示所有的commits,从最新到最后
show all commits, starting with newest$ git log
- 显示具体文件的变化情况
show changes over time for specific filegit log -p <file>
- 查看是谁,什么时候改变具体文件的什么内容
who changes what and when in <file>git blame <file>
</file>
4. 分支和标签 branch & tags
- 列出所有存在的分支( 包括本地和远程)
list all existing branches$ git branch -av
- 切换分支
switch head branch$ git checkout <branch>
- 基于当前本地head所在分支创建一个新的分支
create a new branch based on your current head$ git branch <new-branch>
- 基于远程分支建立一个新的追踪的分支
create a new tracking branch based on a remote branch$ git checkout --track <remote/branch>
- 删除本地分支
delete a local branch$ git branch -d <branch>
- 标示当前的commit作为一个tag
mark the current commit with a tag$ git tag <tag-name>
5. 本地和远程更新、推送、拉取upadate & publish
- 列出所有当前配置的远程仓库
list all currently configured remotes$ git remote -v
- 显示远程仓库的信息
show information about a remote$ git remote show <remote>
- 对本地仓库新增一个远程仓库,其名字为<remote>
add new remote repository, named <remote>$ git remote add <shortname> <url>
</remote></remote> - 从远程仓库下载所有改变,但是不整合到当前head中
download all changes from <remote>,but don't integrate into head$ git fetch <remote>
待进一步分析xxx</remote> - 从远程仓库下载所有改变,并且直接merged/integrate into head
$ git pull <remote> <branch>
- 推送(当前分支)本地改变到远程仓库
publish local changes on a remote$ git push <remote> < branch>
- 删除远程分支
delete a branch on the remote$ git branch -dr <remote/branch>
-d表delete,-r可能表示remote - 推送本地的tags
publish your tags$ git push --tags
有待进一步分析
6. 合并和复位基底merged & rebase
有待进一步分析
- 合并一个分支到当前head所在分支
merged <branch> into your current headgit merge <branch>
</branch> - rebase your current head onto <branch>
don't rebase published commits!$ git rebase <branch>
</branch> - 查看一个rebase信息
abort a rebase$ git rebase --abort
- continue a rebase after resolving conflicts
$ git rebase --continue
- use your configured merge tool to solve conflicts
$ git mergetool
- use your editor to manually solve conflicts and (after resolving) mark file as resolved
$ git add <resolved-file> $ git rm <resolved-file>
7. 回退undo
- 抛弃本地工作目录中所有局部改变
discard all local changes in your working diretory$ git reset --hard HEAD
- 抛弃一个特定文件的局部改变
discard local changes in a specific file$ git checkout HEAD <file>
- 回退一个commit,是通过再来一个commit并做相反的操作
revert a commit (by producing a new commit with contrary changes)$ git revert <commit>
- 重置你的head指针到先前一个commit版本,并且抛弃从那时起的所有改变
reset your head pointer to a previous commit and discard all changes since then$ git reset --hard <commit>
- 保留所有改变到未暂存状态
and preserve all changes as unstaged changes$ git reset <commit>
- and preserve uncommitted local changes
$ git reset --keep <commit>
8. 新增两个
- 本地连接远程仓库
$ git remote add origin git@github.com/xxx.git
- 第一次push
$ git push -u origin master