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用法小結

相關文章
相關標籤/搜索