简单的,经常使用的命令也就几个。可是想很是熟练使用,怕是要记住几十个。html
两者都是从远程拉取代码到本地git
假设远程分支上有2个提交,而后基于远程develop,再建立一个分支feature。segmentfault
而后分别在两个分支上作两次提交。缓存
git merge
这时候,你能够用pull
命令把develop
分支上的修改拉下来而且和你的修改合并;结果看起来就像一个新的和并提交
bash
git rebase
$ git checkout feature
$ git rebase develop复制代码
将一个分支的末端指向另外一个提交,能够用来移除当前分支的一些提交。app
文件层面上,将缓存区的文件同步到指定的那个提交。ide
$ git checkout develop
$ git reset HEAD~2
# 将当前的README.md从缓存区中移除出去
$ git reset HEAD README.md复制代码
develop分支末端的两个提交就变成了悬挂提交post
若是提交尚未push,git reset 是撤销commit的简单方法
除了在当前分支上操做,还能够经过其余参数来修改stage或者workspacefetch
# 将当前的改动从stage中移除,但这些改动还保留在workspace中
$ git reset --mixed HEAD
# 彻底舍弃没有提交的改动
$ git reset --hard HEAD复制代码
提交层面上的checkout,能够切换分支,同一分支,能够切换当前HEAD。ui
文件层面上,不会移动HEAD指针,也不会切换到其余分支上,只是更改workspace,而不是stage。
# 将HEAD移到新的分支,而后更新工做目录
$ git checkout develop
# 将HEAD移动到当前commit的前两个commit上,同时更新workspace
$ git checkout HEAD~2
# 将workspace中的README.md同步到最新的提交
$ git checkout HEAD README.md复制代码
撤销一个提交的同时会建立一个新的提交。
# 产生一个新的commit用于撤销倒数第二个commit
$ git checkout develop
$ git revert HEAD~2复制代码
命令 | 做用域 | 经常使用情景 |
---|---|---|
git reset | 提交层面 | 在私有分支上舍弃一些没有提交的更改 |
git reset | 文件层面 | 将文件从缓存区中移除 |
git checkout | 提交层面 | 切换分支或查看旧版本 |
git checkout | 文件层面 | 舍弃工做目录中的更改 |
git revert | 提交层面 | 在公共分支上回滚更改 |
git revert | 文件层面 | (然而并无) |
git stash
会把全部未提交的修改(包括暂存和未暂存的)都保存起来,用于往后恢复当前工做目录
$ git status
On branch develop
Changes to be committed:
new file: README.md
Changes not staged for commit:
modified: index.html
$ git stash
Saved working directory and index state WIP on master: 5002d47 ...
$ git status
On branch master
nothing to commit, working tree clean复制代码
stage是本地的,不会上传到git server
实际应用中,推荐给每一个stash加一个message,使用git stash save
取代 git stash
$ git stash save "test stash"
Saved working directory and index state On autoswitch: test stash
HEAD 如今位于 296e8d4 remove unnecessary postion reset in onResume function
$ git stash list
stash@{0}: On autoswitch: test stash复制代码
git stash list
命令,查看stash列表$ git stash list
stash@{0}: WIP on master: 049d078 stash_0
stash@{1}: WIP on master: c264051 stash_1
stash@{2}: WIP on master: 21d80a5 stash_2复制代码
2. 使用git stash apply
命令能够经过名字指定那个stash,默认指定最近的(stash@{0})
3. 使用git stash pop
将stash中第一个stash删除,并将对应修改应用到当前的工做目录中
4. 使用git stash drop
,后面加上stash名,能够移除相应的stash;或者使用git stash clear
清空全部stash
默认状况下,git stash
会缓存:
但不会缓存:
此时,使用-u
或者--include-untracked
能够stash untracked 文件;使用-a
或者--all
能够stash当前目录下的全部修改(慎用)
# 在当前目录新建一个Git代码库
$ git init
# 新建一个目录,将其初始化为Git代码库
$ git init git_test
# 下载一个项目和它的整个代码历史
$ git clone http://git.code.oa.com/jaelintu/git_test复制代码
# 添加指定文件到暂存区
$ git add file1 file2...
# 添加指定目录到暂存区,包括子目录
$ git add dir
# 添加当前目录的全部文件到暂存区
$ git add .
# 添加每一个变化前,都会要求确认
# 对于同一个文件的多处变化,能够实现分次提交
$ git add -p
# 删除工做区文件,而且将此次删除放入暂存区
$ git rm file1 file2 ...复制代码
# 提交暂存区到仓库区
$ 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 name
# 新建一个分支,并切换到该分支
$ git checkout -b branch
# 新建一个分支,指向指定commit
$ git branch name commit_SHA
# 新建一个分支,与指定的远程分支创建追踪关系
$ git branch --track name orgin/name
# 切换到指定分支,并更新工做区
$ git checkout name
# 切换到上一个分支
$ git checkout -
# 创建追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream name origin/name
# 合并指定分支到当前分支
$ git merge branch-name
# 选择一个commit,合并进当前分支
$ git cherry-pick commit_SHA
# 删除分支
$ git branch -d branch-name
# 删除远程分支
$ git push origin --delete branch-name
$ git branch -dr remote/branch复制代码
# 列出全部tag
$ git tag
# 新建一个tag在当前commit
$ git tag tag-name
# 新建一个tag在指定commit
$ git tag tag-name commit-SHA
# 删除本地tag
$ git tag -d tag-name
# 删除远程tag
$ git push origin :refs/tags/tag-Name
# 查看tag信息
$ git show tag-name
# 提交指定tag
$ git push origin tag-name
# 提交全部tag
$ git push origin --tags
# 新建一个分支,指向某个tag
$ git checkout -b branch-name tag-name复制代码
# 显示有变动的文件
$ git status
# 显示当前分支的版本历史
$ git log
# 显示commit历史,以及每次commit发生变动的文件
$ git log --stat
# 搜索提交历史,根据关键词
$ git log -S [keyword]
# 显示某个commit以后的全部变更
$ git log (tag-name||commit-SHA) HEAD
# 显示某个文件的版本历史,包括文件更名
$ git log --follow file
$ git whatchanged file
# 显示指定文件相关的每一次diff
$ git log -p file
# 显示过去5次提交
$ git log -5 --pretty --oneline
# 显示全部提交过的用户,按提交次数排序
$ git shortlog -sn
# 显示指定文件是什么人在什么时间修改过
$ git blame file
# 显示暂存区和工做区的代码差别
$ git diff
# 显示暂存区和上一个commit的差别
$ git diff --cached file
# 显示工做区与当前分支最新commit之间的差别
$ git diff HEAD
# 显示两次提交之间的差别
$ git diff [first-branch]...[second-branch]
# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"
# 显示某次提交的元数据和内容变化
$ git show commit-SHA
# 显示某次提交发生变化的文件
$ git show --name-only commit-SHA
# 显示某次提交时,某个文件的内容
$ git show commit-SHA:filename
# 显示当前分支的最近几回提交
$ git reflog
# 从本地master拉取代码更新当前分支:branch 通常为master
$ git rebase branch-name复制代码
# 下载远程仓库的全部变更
$ git fetch origin
# 显示全部远程仓库
$ git remote -v
# 显示某个远程仓库的信息
$ git remote show origin
# 增长一个新的远程仓库,并命名
$ git remote add shortname url
# 取回远程仓库的变化,并与本地分支合并
$ git pull origin branch-name
# 上传本地指定分支到远程仓库
$ git push origin branch-name
# 强行推送当前分支到远程仓库,即便有冲突
$ git push origin --force
# 推送全部分支到远程仓库
$ git push origin --all复制代码
# 恢复暂存区的指定文件到工做区
$ git checkout file
# 恢复某个commit的指定文件到暂存区和工做区
$ git checkout commit-SHA file
# 恢复暂存区的全部文件到工做区
$ git checkout .
# 重置暂存区的指定文件,与上一次commit保持一致,但工做区不变
$ git reset file
# 重置暂存区与工做区,与上一次commit保持一致
$ git reset --hard
# 重置当前分支的指针为指定commit,同时重置暂存区,但工做区不变
$ git reset commit-SHA
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工做区,与指定commit一致
$ git reset --hard commit-SHA
# 重置当前HEAD为指定commit,但保持暂存区和工做区不变
$ git reset --keep commit-SHA
# 新建一个commit,用来撤销指定commit
# 后者的全部变化都将被前者抵消,而且应用到当前分支
$ git revert commit-SHA
# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop复制代码
rebase过程当中,也许会出现冲突(conflict)
git add
添加冲突的文件,更新暂存区git rebase --continue
继续剩下的rebasegit rebase --abort
终止rebase行为,而且feature会回到rebase开始以前的状态$ git rebase develop
CONFLICT (content): Rebase conflict in readme.txt
Automatic rebase failed; fix conflicts and then commit the result.
$ git status
On branch feature
You have unmerged paths.
(fix conflicts and run "git rebase --continue")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")复制代码
查看readme.md 内容
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature复制代码
选择保留HEAD
或者feature
的版本
Git tracks changes of files.
Creating a new branch is quick AND simple.复制代码
在提交:
$ git add readme.md
$ git rebase --contine复制代码
转载地址:https://segmentfault.com/a/1190000017114656