Git基本命令

查看修改记录log

git log
git log - -oneline 每个commit内容显示在一行

比较unstaged状态下的不同,即在add之前

git diff

比较staged状态下的不同,即在add之后commit之前

git diff - -cached

修改已经commit的版本

git commit - -amend - -no-edit 该次提交的内容,直接合并到上一个commit

reset回到add之前

git add two.txt
git status -s #”-s”:status的缩写模式
M two.txt
git reset two.txt
Unstaged changes after reset:
M two.txt

回到之前某个commit,每一个commit都有自己的id号

$ git log - -oneline
49c6cdb Merge branch ‘A’
bdc16e3 add hello.txt
$ git reset - -hard 110a0a4
HEAD is now at 110a0a4 Merge branch ‘C’

分支

git checkout -b A 直接创建和切换到A分支
git branch -a 列出本地和远程分支
A
* master
remotes/origin/A
remotes/origin/master
git branch 列出本地分支
* A *代表当前所在分支
B
C
master

merge分支冲突

当不同分支同时修改同一个文件时,在进行合并时就会发生冲突。如master分支和A分支同时修改hello.txt进行提交后,将A分支合并,此时发生冲突,根据提示打开hello.txt,其中冲突已经被标志出,手动解决冲突然后add且commit冲突就解决了。如下面所示:
git merge A
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

git commit -am “solved conflict” 添加同时提交

git log - -oneline - -graph 方便查看分支
* eddf4f2 solved conflict
|\
| * 5782deb modify hello.txt
* | 1ecc93c modify hello.txt

暂存修改,恢复暂存

假如在某一分支正在修改某文件,这时突然必须得去其他分支修改文件,就可以先将该分支暂存,后边再恢复,如下所示:
git status -s
M one.txt
git stash 暂存修改
Saved working directory and index state WIP on A: 5782deb modify hello.txt
HEAD is now at 5782deb modify hello.txt
git status
On branch A
nothing to commit, working tree clean
git stash list 查看在stash中的缓存
stash@{0}: WIP on A: 5782deb modify hello.txt
git stash pop 恢复暂存
On branch A
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout – …” to discard changes in working directory)

    modified:   one.txt

no changes added to commit (use “git add” and/or “git commit -a”)
Dropped refs/stash@{0} (d313cf12fa37a84ed835c6d714274c83334822c7)
git status -s 同起始状态一致
M one.txt
git pull origin master
如果远程和本地没有同时修改,即只有本地改就不会有冲突,pull时就会默认使用本地的

删除分支

git push origin - -delete 分支名
删除远端分支
git branch –d 分支名
删除已合并的本地分支
git branch –D 分支名
删除本地未合并的分支

删除版本库文件

git rm
使用git rm 后commit
git rm -r 文件夹/
此处-r表示递归所有子目录

关于git工作区、暂存区、版本库可参考:
http://www.runoob.com/git/git-workspace-index-repo.html
git其他一些常用命令,可参考:
http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
关于git在linux、mac安装;rebase分支冲突 可参考:
https://morvanzhou.github.io/tutorials/others/git/4-3-rebase/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值