刚毕业的时候用过极短期的SVN,后面就一直在用Git来作代码的版本控制了,前先后后差很少4年的时间,期间作了一些在使用Git过程当中的记录和心得,在这里分享给你们,你们或许能够从中吸取到一些有用的东西。css
不管是github,仍是gitlab,仍是其余的代码托管平台,代码管理都是用git去作的,git能够说是一名程序员的必备技能,对于工做和面试都是很是有帮助的。html
git clone -b 分支名称 远程地址
git config user.name
git config user.email
git branch(查看当前分支)
git branch -a(查看全部分支)
git checkout 分支名(切换到对应分支)
会自动将代码更新为分支代码git branch 分支名
(建立一个分支)git branch -d 分支名
(删除一个分支)git branch -D 分支名
(强制删除一个未合并的分支)git checkout -b 分支名 [基于的分支名或commit值]
(切换分支并直接切换过去)history
history | grep push
git log
git log --summary
git config --global user.name "Frankkai"
git config --global user.email "gaokai20100801@gmail.com"
git config user.name "Frankkai"
git config user.email "gaokai20100801@gmail.com"
git config --global --list
git config --local --list
git config --local user.name
git reset HEAD static/lib/js/constantsUrl.js
git checkout -- static/lib/js/constantsUrl.js
git diff
git diff HEAD
git diff --staged
git add "*.html"
git commit --amend
git add "*.js"
git reset octofamily/octodog.txt
git rm "*.txt"
git merge 分支名
git checkout -- <filename>
git stash
git stash pop
git stash pop stash@{0}
git push origin --delete branchname
git log
//找到commit hash值git reset --hard hash值
git stash list
git stash drop stash@{0}
git remote set-url origin git@foo.bar.com:baz/helloworld.git
git push --set-upstream origin preproduction
git push origin <tag_name>
git push --tags
git tag --list
git rm --cached -r .idea
// --cached仅仅删除index,-r(recursive)递归删除.idea目录下的全部文件git add <file(s)>/.
git reset HEAD <file(s)>/.
git reset HEAD <file(s)>/. && git checkout -- <file(s)>/.
git add -u
git reset --hard
git diff HEAD [commit hash fragment]
git branch -D [branch name]
同步到remote后,合并多个commit 为1个前端
git rebase -i HEAD~2/hash pick && squash :wq!
git remote -v
git rebase --abort
reset soft
gitflow release 发布新版本linux
git flow release start v0.5.0 npm version minor git flow release finish -n git push git checkout master git push
gitflow hotfix 修复一个master上的buggit
git flow hotfix start foo npm version patch // 注意:必定要在修复bug代码以前新增版本号 git add . git commit -m "version change message" git flow hotfix finish -n git push git checkout master git push
对比2个分支的日志程序员
git log develop..master
合并master代码到featuregithub
git checkout feature git merge master
git merge master feature
git rebase冲突时怎么办web
resolve conficts git add . git rebase --continue
squash多个commits成一个怎么敲?
假设merge feature到master。面试
git checkout master git merge --squash feature git commit -m "这是一次squash commit" git push
查看当前分支的父分支npm
git reflog show <childBranch> 32c3956 (HEAD -> currentBranch, origin/fatherBranch, fatherBranch, list) childBranch@{0}: branch: Created from fatherBranch
childBranch 是你新建的分支。
fatherBranch 是它的父分支,也就是来源分支。
撤销远程分支错误提交
...reset git push --force
其实使用本地分支的提交替代远程分支。
误删除领先远程的本地分支如何恢复?
git reflog // 找出最新的commit sha1值,HEAD@{1}比HEAD@{2}新 git branch branchName <sha1>
经过git reflog找到一个commit,而后再cherry-pick也能够。
删除由npm version patch/minor/major误添加的tag
git tag | grep v1.1.38 git tag -d v1.1.38 git push origin :refs/tags/v1.1.38
git fetch与git pull的区别
git fetch 更新origin/*下的全部分支,在发布git flow feature前颇有用,用于更新remote分支。 git pull 主要用来更新多人合做的当前分支,多用于更新local分支。
远程分支删除,本地git fetch不能更新到最新分支
git fetch --prune
--prune
Before fetching, remove any remote-tracking references that no longer exist on the remote.
及时查看本地全部分支的状态
git remote show origin
假如远程已不存在这个分支,git fetch --prune也更新不到状态,该怎么办?
git push origin --delete feature/fix-chat-unread-msg-async
删除单个脱离的远程分支
git remote prune <name>
git merge --abort
git revert Head/[commit hash]
ssh-keygen -t rsa -C "gaokai20100801@qq.com"
/c/Users/frank/.ssh/id_rsa.pub
cd ~/.ssh
ls
cat id_rsa.pub
误删除stash,该怎么办?
git fsck --unreachable | grep commit | cut -d\ -f3 | xargs git log --merges --no-walk --grep=WIP
找到对应的commit hash值
git stash apply 1f55da93d26cd51f15f9e93351dae6b75e25e36f
.idea修改老是会提醒,.gitignore不生效.idea/
git rm -r --cached .idea
git remote -v
查看origin表明的地址。Git的system,global和local参数分别表明什么?
--system能够输出不少git的系统设置,其中最有用的是alias,例如git s表明了git status,系统全部登陆用户有用。 --global输出了git的全局设置,主要包括全局的user.name和user.email,优先级低于单个仓库中设置的user.name和user.email,当前用户全部仓库有用。 --local输出了git的项目设置,主要包括remote.origin.url以及gitflow的不少配置,只对某个仓库有用。
Git工做区和暂存区的区别?
add commit 工做目录---->暂存区---->版本历史
暂存区:git已经得到了对文件的管理权限,暂存区文件有状态:new file,deleted,modified等等。
版本历史:git log查看每一次commit记录。
意外收获:
如果想很是细粒度的控制commit记录,可使用git add 指定文件,分开屡次commit,每一次commit提交一个细粒度功能的变动文件集合,屡次走文件目录 暂存区 版本历史这个流程。
Git如何重命名文件?
git mv README.md readme.md
git rm --cached -r .idea // **--cached仅仅删除index**,-r(recursive)递归删除.idea目录下的全部文件
git rm
删除index上和working tree上的文件,仅仅删除working tree不删除index的状况,不存在。
如何一目了然地区分出工做区和暂存区?
// 版本历史 Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) // 暂存区 Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: readme.md -> README.md new file: helloman // 工做区 Untracked files: (use "git add <file>..." to include in what will be committed) hi
git log --oneline
简洁的commit记录git log -n2 --oneline
最近的2次简洁的commit记录git log --all
全部分支的历史版本信息git log --graph
图形化查看版本演进历史git log --oneline --all -n4 --graph
组合查看日志git help --web log
浏览器查看git log
的用法gitk
无需安装第三方插件,在纯命令行下,无第三方软件状况下可用。HEAD
工做分支refs/heads/fooconfig
repo的配置信息refs
heads,分支;tags,标签或者里程碑refs/heads/master
存放了什么,最新的一个commitrefs/tags/js01
存放了什么,最新的一个tag,包含一个objectobjects
文件夹,2个字符的和松散的pack文件夹,存放的是tree,tree下有blob文件如何判断git文件的类型?git cat-file -t/-p [hash fragment]
// -t 类型,-p 内容
只要任何文件的文件内容相同,在git眼里,它就是惟一的一个blob。
commit tree // 位于objects目录下 blob // 位于objects目录的二级目录下,具体的文件
tree:取出一个commit,存放了一个快照,这个快照,对应了当前项目的全部的文件夹及其文件的快照,是特定时间的整个仓库的一个状态;树里能够有blob,也能够有树,由于树是文件夹;根树是最大的树。
blob: 与文件名是否相同无关,只要内容相同,就是惟一的blob。
一个commit包含了哪些?
git cat-file -p [commit hash fragment]
包含tree,parent,author和commiter。
tree f06f7f36af17cb9098031c66d22a7910c0fa1bac parent 92a55c8a5b1d38d224232ad84b9b728ae77189cb parent eda632a1f2a3ea049c5f5268f6b2f064b71898ce author FrankKai <gaokai20100801@gmail.com> 1548139120 +0800 committer FrankKai <gaokai20100801@gmail.com> 1548139120 +0800 Merge branch 'feature/chatBreakChange' into prerelease # Conflicts: # src/api/chat.js
一个tree包含了哪些?
git cat-file -p [tree hash fragment]
包含tree,blob。
blob 0b96f21c27a3759cecde02fba1e050d86a8e9a54 yarn.lock
一个blob包含了哪些?
git cat-file -p [tree hash fragment]
就是一个具体的文件。
git checkout [commit hash fragment]
,切换到分离头指针状态,不与任何branch或tag关联,git会认为这是不重要的,当成垃圾清理掉。缺点:切换分支后,须要用git branch [branch name] [commit hash fragment]
新建一个分支,不然会丢失原消息。
优势:能够基于某一次commit切出分支,而后新建一个commit,快速会退到想要的版本。
能够指向分支或者commit,但其实分支归根结底仍是指向了commit。
git log 查看HEAD指针指向的分支名:(HEAD->foo, bar, master)
能够快速diff,git diff HEAD [commit hash fragment]
。
父亲的父亲diff:git diff HEAD HEAD~2
,git diff HEAD HEAD^^
。
git commit --amend
如何修改老旧commit的message?
git rebase -i [父 commit hash fragment] reward 添加修改后的commit message
注意:不能在团队的集成分支上,作这样的变动,仅适用于本地。
git比较聪明,它不会彻底将本地的代码扔掉,即便没有人为的生成一次commit记录,也会自动为咱们在stash下生成一次记录,以避免形成重大的代码丢失。
版本号升级 | gitflow对应 |
---|---|
bug -> patch | hotfix |
feature->minor | release |
系统重构->major | release |
可是在scrum的状况下,迭代很是快速,若全部feature都升级minor,会致使minor数字很大,该怎么处理这种状况?
只升级minor时,在commit提交信息中,添加如下信息:
类型 | 提交信息 |
---|---|
bug patch | [bug patch] |
feature patch | [feature patch |
建立一个新的项目并上传到git
git init git ac git remote add origin remote repository URL
git参数--decorate是什么?
清空全部本地git 缓存
git rm -r --cache .
一次完整的rebase流程
git rebase --continue
git reset 删除某个commit以后的代码,原理是Head向后退。有存在被回滚掉的commit分支代码合并过来时,被reset掉的代码仍然会合并上来
缩写 | 全写 |
---|---|
gst | git status |
gaa | git add . |
gcmsg "" | git commit -m "" |
gp | git push |
glog | git log --oneline --decorate --graph |
gl | git pull |
gf | git fetch |
gfa | git fetch --all --prune |
glog --decorate=no
期待和你们交流,共同进步:
- 微信公众号: 大大大前端 / excellent_developers
- Github博客: 趁你还年轻233的我的博客
- SegmentFault专栏:趁你还年轻,作个优秀的前端工程师
努力成为优秀前端工程师!