git使用记录

Github入门与实践css

图形化工具SourceTree

3.0.8普通用户版account.json跳过登录注册方法已失效,请安装企业版

官网:https://www.sourcetreeapp.com...html

跳过登录办法

进入 sourcetree 目录, 在资源管理器中进入如下地址node

%LocalAppData%\Atlassian\SourceTree\

放入如下文件 accounts.jsongit

[
  {
    "$id": "1",
    "$type": "SourceTree.Api.Host.Identity.Model.IdentityAccount, SourceTree.Api.Host.Identity",
    "Authenticate": true,
    "HostInstance": {
      "$id": "2",
      "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountInstance, SourceTree.Host.AtlassianAccount",
      "Host": {
        "$id": "3",
        "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountHost, SourceTree.Host.AtlassianAccount",
        "Id": "atlassian account"
      },
      "BaseUrl": "https://id.atlassian.com/"
    },
    "Credentials": {
      "$id": "4",
      "$type": "SourceTree.Model.BasicAuthCredentials, SourceTree.Api.Account",
      "Username": "",
      "Email": null
    },
    "IsDefault": false
  }
]

GIT用户权限配置

  • 生成本机 SSH
ssh-keygen -t rsa -C "email@email.com"
// 一路回车结束
  • 生成密钥文件为:~/.ssh/id_rsa.pub ,能够用 cat ~/.ssh/id_rsa.pub 查看
  • 复制粘贴到 SSH设置
  • souretree 配置 GIT 用户权限

Git commit 规范参考文档

Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)。 type:es6

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变更)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变更)
  • test:增长测试
  • chore:构建过程或辅助工具的变更

coding完成后发布

如下以d3-foo 插件发布为例github

Publish to GitHub

If you’re ready to share your code with the world, make your first commit:docker

git add .
git commit

Then create a new repository on GitHub. Add the git remote, and push:npm

git remote add origin git@github.com:{USERNAME}/d3-foo.git
git push -u origin master

Replace {USERNAME} with your GitHub user name and d3-foo with your plugin name, obvs.json

Next, create a git tag that corresponds to the version in your package.json file. (If this is not your first release, you’ll also want to bump the version to 0.0.2, 0.1.0, or 1.0.0 as appropriate.) You can push commits as frequently as you like, but periodically you should bundle these commits together into a release. Tags are simply a mechanism for identifying releases in the repository.segmentfault

git tag -a v0.0.1

(Alternatively, use npm version, which also edits the package.json file for you.)

Push the tag to GitHub:

git push --tags

Publish to NPM:

npm publish

As a side-effect of publishing, NPM will create a d3-foo.zip archive of your release in the build folder. Add this as a custom download to your GitHub releases (for example, see d3-shape) so that people can download your code without needing to use NPM.

Edit your release notes to tell people what’s changed!

Once you’ve published the first time, add your plugin to the wiki. If you want collaborators, let me know, and you can transfer your repository to the D3 organization and setup a team. You’ll retain admin rights and be able to publish new releases as often as you like!

把本地项目上传到github

https://jingyan.baidu.com/art...
图片描述
图片描述

经常使用命令

项目发布过程

仓库初始化

git init

查看仓库状态

git status

添加监控跟新文件

git add -A

提交

git commit -m

推送到远程仓库

git push

图片描述

图片描述

图片描述

图片描述

更改本地并推送到远程仓库

图片描述

在commit以前撤销git add操做

http://stackoverflow.com/ques...

http://stackoverflow.com/ques...

遇到过的问题

github中git push origin master出错:error: failed to push some refs to

图片描述

图片描述

gitignore常见的内容

.DS_Store
.sizecache.json
/bower_components
/dist
/node_modules
npm-debug.log
yarn-error.log

新建develop分支

git checkout -b develop
//这时候在本地git仓库就建了一个develop分支,git status 会发如今develop分支下


//这时候咱们能够继续编码
//当要push上远程的分支时候须要注意的是,远程尚未develop分支 因此咱们能够
git push origin develop


git add .
git commit -m "“
git push --set-upstream origin develop

图片描述
这时候在远程也会发现多了一个develop分支

回滚历史版本

总结一下 Git 不一样状况下如何回滚
在命令行使用git:
在工做区目录下输入 git reflog
能够查询出每一个历史版本的commit,每一个commit都有对应的ID,
确认好要退回的ID后输入:

git reset --hard 确认的ID

即可退回相应的历史版本
图片描述

Git merge VS rebase

http://chenjsh.cn/demo/demoge...
https://www.atlassian.com/git...

图片描述

如何移除 git 提交历史中关于某个文件的修改历史?

场景是这样的,在一次提交 PR 的 review 过程当中,我提交的一个文件,在若干 commits 的修改下,最终和最初状态彻底相同,可是 PR 中却保留了对该文件的提交历史,所以 reviewer 但愿我能够将这个文件移除提交历史。

这个主要须要用到 git rebase ,步骤以下:

  • git log filename: 首先经过 git log 来查询要回滚到的 commit id
  • git reset commit-id filename: 对该文件进行 reset 操做(撤销提交历史相关的修改)
  • git checkout filename: 对其进行 checkout 操做(撤销对文件自己的修改)
  • git commit --amend: 修改提交历史信息
  • git rebase --continue/git push: 同步

Git dev分支合并到master分支

Git dev分支合并到master分支完美实战

待代码测试没问题,再将本身分支的内容合并到master 分支,而后提交到远程服务

git checkout master
git merge <branchname>
git push origin master

git tag 快照标签

git支持使用易记的字符串和附加信息为特定的快照打标签。你能够利用标签为开发树
(development tree)加上信息(例如Mergedin new memory management),使其更为清晰,或是标
记出分支上特定的快照。例如,用标签标出release-1分支上的release-1.0和release-1.1。
git支持轻量标签(仅为快照打标签)以及注解标签。
git标签仅在本地范围内有效。 git push 默认不会推送标签。要想把标签发送到origin仓库,
必须加上选项 --tags :

$ git push origin --tags

git tag 命令包括能够用于添加、删除和列出标签的选项。

不使用选项的 git tag 命令能够列出可见标签:

$ git tag
release-1.0
release-1.0beta
release-1.1

你能够经过添加标签名在当前检出中建立标签:

$ git tag ReleaseCandidate-1

在 gittag 命令中加入指定提交的SHA-1,就能够为该提交添加标签:

$ git log --pretty=oneline
bef4a35a67c6228cbeb05913302f96966ff33d01 fix
5bc4812c60222956e5157cc8c9a5f06156a7750b change config
4a8bae2f78c412065c8422821feaa4cc5a94c82b change to es6
36fa4f97a31be4bc410eb0b4e940c753485f1c90 fix: mongo docker
85aa77f7e4c03a3512f75c47f7568aef9f8a757f fix
:...skipping...
bef4a35a67c6228cbeb05913302f96966ff33d01 fix
5bc4812c60222956e5157cc8c9a5f06156a7750b change config
4a8bae2f78c412065c8422821feaa4cc5a94c82b change to es6
36fa4f97a31be4bc410eb0b4e940c753485f1c90 fix: mongo docker
85aa77f7e4c03a3512f75c47f7568aef9f8a757f fix
a3e6f734ad19d33889d2298111cde828147b4c01 node:10 => node:10:...skipping...
bef4a35a67c6228cbeb05913302f96966ff33d01 fix
5bc4812c60222956e5157cc8c9a5f06156a7750b change config
4a8bae2f78c412065c8422821feaa4cc5a94c82b change to es6
36fa4f97a31be4bc410eb0b4e940c753485f1c90 fix: mongo docker
85aa77f7e4c03a3512f75c47f7568aef9f8a757f fix
a3e6f734ad19d33889d2298111cde828147b4c01 node:10 => node:10.15.3-alpine
200e8e81fb285e8ef035cf51236ad5bedaac8393 feats: add dockerf:...skipping...
bef4a35a67c6228cbeb05913302f96966ff33d01 fix
5bc4812c60222956e5157cc8c9a5f06156a7750b change config
4a8bae2f78c412065c8422821feaa4cc5a94c82b change to es6
36fa4f97a31be4bc410eb0b4e940c753485f1c90 fix: mongo docker
85aa77f7e4c03a3512f75c47f7568aef9f8a757f fix
a3e6f734ad19d33889d2298111cde828147b4c01 node:10 => node:10.15.3-alpine
200e8e81fb285e8ef035cf51236ad5bedaac8393 feats: add dockerfile
:...skipping...
bef4a35a67c6228cbeb05913302f96966ff33d01 fix
5bc4812c60222956e5157cc8c9a5f06156a7750b change config
4a8bae2f78c412065c8422821feaa4cc5a94c82b change to es6
36fa4f97a31be4bc410eb0b4e940c753485f1c90 fix: mongo docker
85aa77f7e4c03a3512f75c47f7568aef9f8a757f fix
a3e6f734ad19d33889d2298111cde828147b4c01 node:10 => node:10.15.3-alpine
200e8e81fb285e8ef035cf51236ad5bedaac8393 feats: add dockerfile
2913f6ec026931ab2b189da91f10e0a43d1fee5e 添加readme
dcded2b55f7fd190a1cc8aee2aa5e6b1e50886bd jx_todo_app firstversion
# $ git tag menuComplete ad606b
git tag v1.0.1 bef4a35a67

选项 -a 能够为标签加入注解:

$ git tag -a tagWithExplanation

git会打开编辑器,建立注解

你能够在命令行中使用 -m 选项定义信息:

$ git tag -a tagWithShortMessage -m "A short description"

若是使用 git show 命令,会显示以下信息:

$ git show tagWithShortMessage
tag tagWithShortmessage
Tagger: Clif Flynt <clif@cflynt.com>
Date:
Fri Dec 23 09:58:19 2016 -0500 6
A short description
...

选项 -d 能够删除标签:

$ git tag
tag1
tag2
tag3
$ git tag -d tag2
$ git tag
tag2
tag3F

can you delete mutiple branches in one command with git

git branch -D `git branch`| grep -E 'fix-*'

git stash

git stash(git储藏)可用于如下情形:

  • 发现有一个类是多余的,想删掉它又担忧之后须要查看它的代码,想保存它但又不想增长一个脏的提交。这时就能够考虑git stash。
  • 使用git的时候,咱们每每使用分支(branch)解决任务切换问题,例如,咱们每每会建一个本身的分支去修改和调试代码, 若是别人或者本身发现原有的分支上有个不得不修改的bug,咱们每每会把完成一半的代码commit提交到本地仓库,而后切换分支去修改bug,改好以后再切换回来。这样的话每每log上会有大量没必要要的记录。其实若是咱们不想提交完成一半或者不完善的代码,可是却不得不去修改一个紧急Bug,那么使用git stash就能够将你当前未提交到本地(和服务器)的代码推入到Git的栈中,这时候你的工做区间和上一次提交的内容是彻底同样的,因此你能够放心的修Bug,等到修完Bug,提交到服务器上后,再使用git stash apply将之前一半的工做应用回来。
  • 常常有这样的事情发生,当你正在进行项目中某一部分的工做,里面的东西处于一个比较杂乱的状态,而你想转到其余分支上进行一些工做。问题是,你不想提交进行了一半的工做,不然之后你没法回到这个工做点。解决这个问题的办法就是git stash命令。储藏(stash)能够获取你工做目录的中间状态——也就是你修改过的被追踪的文件和暂存的变动——并将它保存到一个未完结变动的堆栈中,随时能够从新应用。

git stash用法

stash当前修改

git stash会把全部未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工做目录。
好比下面的中间状态,经过git stash命令推送一个新的储藏,当前的工做目录就干净了。

$ git status
On branch master
Changes to be committed:

new file:   style.css

Changes not staged for commit:

modified:   index.html

$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch master
nothing to commit, working tree clean

须要说明一点,stash是本地的,不会经过git push命令上传到git server上。
实际应用中推荐给每一个stash加一个message,用于记录版本,使用git stash save取代git stash命令。示例以下:

$ git stash save "test-cmd-stash"
Saved working directory and index state On autoswitch: test-cmd-stash
HEAD 如今位于 296e8d4 remove unnecessary postion reset in onResume function
$ git stash list
stash@{0}: On autoswitch: test-cmd-stash

从新应用缓存的stash

能够经过git stash pop命令恢复以前缓存的工做目录,输出以下:

$ git status
On branch master
nothing to commit, working tree clean
$ git stash pop
On branch master
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

Dropped refs/stash@{0} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

这个指令将缓存堆栈中的第一个stash删除,并将对应修改应用到当前的工做目录下。
你也可使用git stash apply命令,将缓存堆栈中的stash屡次应用到工做目录中,但并不删除stash拷贝。命令输出以下:

$ git stash apply
On branch master
Changes to be committed:

    new file:   style.css

Changes not staged for commit:

    modified:   index.html

查看现有stash

可使用git stash list命令,一个典型的输出以下:

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log

在使用git stash apply命令时能够经过名字指定使用哪一个stash,默认使用最近的stash(即stash@{0})

移除stash

可使用git stash drop命令,后面能够跟着stash名字。下面是一个示例:

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

或者使用git stash clear命令,删除全部缓存的stash。

参考

git 教程
Githug 通关攻略
沉浸式学 Git
git建立本地分支以及推送本地分之至远程分支
https://segmentfault.com/a/11...
git commit 规范指南
git 技巧
git-stash用法小结

相关文章
相关标签/搜索