commit本地版本仓库的的一个快照,可以恢复误删文件,构成时间线
![](https://img.haomeiwen.com/i1062649/e3aee9e98e7e50a0.jpeg)
查看每一次命令
git reflog //用来记录你的每一次命令,可以把HEAD指向任意的commit id
![](https://img.haomeiwen.com/i1062649/6158fcf4c167737f.png)
回退版本,上下来回穿梭,HEAD 指向当前版本,回退版本时就是改变HEAD的指向而已
git reset —hard HEAD^ //(一个^是当前版本上一个版本,当前版本的上10个版本用HEAD-10,会修改工作区的代码,也可以回到将来的版本,上下来回穿梭,一般会提示落后于remote分支)
git pull//拉取远程分支,解决可能的冲突,再次push就行了
工作区回到最后一次git add 或者 git commit 状态
1.工作区的修改还没有提交到暂存区,工作区会回到和版本库版本一样
2.工作区的修改提交到了暂存区,工作区又修改了新的代码,工作去会回到和暂存区一样
git checkout -- readme.md //没有 -- 为切换分支的命令,工作区回到最后一次git add 或者 git commit 状态,会丢失提交后工作区的修改
工作区与暂存区最新版本的区别
git diff HEAD -- readme.md //工作区与暂存区最新版本的区别
工作区的修改已经提交到暂存区了,想要把暂存区的修改撤回
git reset HEAD readme.md //HEAD指向当前版本,操作命令行会提示 unstaged changes after reset.
怎么从版本库删除文件?
使用命令git rm 并且 git commit
git rm readme.md
git commit -m"remove readme.md."
本地仓库与远程关联
git remote add origin GitHub项目地址 //本地仓库与远程关联,origin 时GitHub远程仓库的名称
git push -u origin master //把本地仓库的所有内容都推送到远程仓库上
创建一个分支并切换到刚创建的分支上
git checkout -b dev //创建分支并切换到分支
相当于
git branch dev
git checkout dev
怎样合并dev分支到master?
切换到master分支上,把dev上的修改合并到master上
git merge dev //合并dev分支到当前分支
提示Fast- forward意味着是快进模式,直接把master指向dev的当前提交
合并后删除分支
git branch -d dev //删除分支,your branch is ahead of 'origin/master' by 1 commit.当前分支比远程的master分支超前一个提交
git merge --no--ff -m"merge with no-ff" dev //--no--ff参数,表示禁用FastForward,-m是要把分支合并的信息作为一次commit,不添加合并后会丢失分支信息.
情景:正在dev分支上开发,来了一个bug fix 任务,需要切换到master分支修复,当前在dev的工作未能完成,还不能提交怎么办?
git stash //可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作
git checkout master //切换到master分支
git checkout -b issue-101 //从master分支创建一个临时分支issue-101,修改bug
git add readme.txt
git commit -m "fix bug 101"
git checkout master //修改完成,切换到master分支
git merge --no-ff -m "merged bug fix 101" issue-101 //把issue-101合并到master,bug修复工作完成
git checkout dev //切换到dev分支,继续之前的工作
git status //nothing to commit, working tree clean,因为之前的stash操作,所有工作区是干净的
git stash list //查看工作现场存到哪去了
git stash pop //恢复工作现场,恢复的同时把stash内容也删了,还可以用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;
情景:接到一个秘密的开发任务,开发的过程中,任务取消,必须强制删除分支,不能保留怎么做?
git checkout -b feature-vulcan //从master创建一个分支feature-vulcan
git add vulcan.py //开发完成提交到暂存区
git commit -m "add feature vulcan" //提交到版本库
git checkout master //切回master分支准备合并,接到信息,任务取消,必须强制删除分支
git branch -d feature-vulcan //error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
git branch -D feature-vulcan //强行删除
你的小伙伴要在dev
分支上开发,就必须创建远程origin
的dev
分支到本地,于是他用这个命令创建本地dev
分支
git remote //查看远程分支
git checkout -b dev origin/dev
如果你和你的小伙伴都往远程origin仓库的dev分支推送,可能会推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单git pull
git pull
There is no tracking information for the current branch. //提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> dev
//git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:
git branch --set-upstream-to=origin/dev dev //Branch 'dev' set up to track remote branch 'dev' from 'origin'.建立本地分支和远程分支的关联
git pull //然后解决冲突
* 582d922 (HEAD -> master) add author
* 8875536 add comment
* d1be385 (origin/master) init hello
//Git用(HEAD -> master)和(origin/master)标识出当前分支的HEAD和远程origin的位置分别是582d922 add author和d1be385 init hello,本地分支比远程分支快两个提交。
rebase操作可以把本地未push的分叉提交历史整理成直线
rebase的目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比。
rebase使用场景:
别人往远程分支dev push了一次修改,我提交时要先git pull,合并之后再commit,提交历史分叉了,使用git rebase之后,提交历史变成了一条直线,原理就是git会根据提交时间挪动commit的位置,rebase操作前后,最终的提交内容是一致的,但是,我们本地的commit修改内容已经变化了,它们的修改不再基于
d1be385 init hello
,而是基于f005ed4 (origin/master) set exit=1
,但最后的提交7e61ed4
内容是一致的
rebase之前
$ git log --graph --pretty=oneline --abbrev-commit
* e0ea545 (HEAD -> master) Merge branch 'master' of github.com:michaelliao/learngit
|\
| * f005ed4 (origin/master) set exit=1
* | 582d922 add author
* | 8875536 add comment
|/
* d1be385 init hello
rebase之后
git log --graph --pretty=oneline --abbrev-commit - 7e61ed4 (HEAD -> master) add author - 3611cfe add comment - f005ed4 (origin/master) set exit=1 - d1be385 init hello
git tag 创建标签
1.命令
git tag <tagname>
用于新建一个标签,默认为HEAD
,也可以指定一个commit id;
git tag v1.0 //新建一个标签,默认为HEAD
git log --pretty=oneline --abbrev-commit//查找历史提交的标签
git tag v0.9 f52c633 //对其中某次commit打tag
2.命令
git tag -a <tagname> -m "blablabla..."
可以指定标签信息;
git tag -a v0.1 -m "version 0.1 released" 1094adb
3.命令
git tag
可以查看所有标签
git tag //查看所有的标签
git show v0.9 //可以用git show <tagname>查看标签信息
操作标签
1.删除标签
git tag -d v0.1 //因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。
2.推送某个标签到远程,使用命令git push origin <tagname>
git push origin v1.0 //推送某个标签到远程
git push origin --tags //一次性推送全部尚未推送到远程的本地标签
3.删除远程上的一个标签,怎么做?
1.先从本地删除
git tag -d v0.9
2.然后,从远程删除。删除命令也是push
git push origin :refs/tags/v0.9 //可以删除一个远程标签
删除与远程仓库的关联,添加新的远程关联怎么做?
git remote add origin git@gitee.com:liaoxuefeng/learngit.git //fatal: remote origin already exists.这说明本地库已经关联了一个名叫origin的远程库
git remote -v //也可以先用git remote -v查看远程库信息
origin https://github.com/yqzhouluyao/git_learning.git (fetch)
origin https://github.com/yqzhouluyao/git_learning.git (push)
git remote rm origin //可以删除已有的GitHub远程库
git remote add origin git@gitee.com:liaoxuefeng/learngit.git //再关联码云的远程库
想关联多个远程库怎么做?
git remote rm origin //先删除远程关联
git remote add github git@github.com:michaelliao/learngit.git //先关联GitHub的远程库,远程库的名称叫github,不叫origin了
git remote add gitee git@gitee.com:liaoxuefeng/learngit.git //再关联码云的远程库
git push github master //如果要推送到GitHub
git push gitee master //如果要推送到码云
被gitignore忽略的文件类型怎么添加到暂存区?
git add App.class
The following paths are ignored by one of your .gitignore files:
App.class
Use -f if you really want to add them.
git add -f App.class //可以用-f强制添加到Git
git check-ignore -v App.class //可能是.gitignore写得有问题,需要找出来到底哪个规则写错了,可以用git check-ignore命令检查
.gitignore:3:*.class App.class
参考资料:Git教程
网友评论