概念
- Git: A source code versioning system.
- GitHub,Gitlab: Are services that provides remote access to Git repositories. In addition to hosting your code, the services provide additional features designed to help manage the software development lifecycle. Basic Command
Github使用小技巧
- diff時忽略空格, URL後面添加?w=1
- 查看某個做者的提交歷史,URL後面添加?author=username
- 快速引用, 能夠選中別人的評論文字,按下r, 這些內容會以引用的形式被複制在文本框中
https ssh
區別
1.克隆項目到本地時,https url 最方便,而SSH url克隆則必須在以前先配置和添加SSH key(必須是該項目的擁有者或是管理員,不然沒法添加SSH key)
2.https url 在push時須要驗證用戶名和密碼,SSH 在push時不須要輸入用戶名,若是配置SSH key的時候設置了密碼,則須要輸入密碼,不然直接是不須要輸入密碼的git
生成並添加第一個ssh key
cd ~/.ssh
ssh-keygen -t rsa -C 「mlding@thoughtworks.com"
這時能夠一路回車,不輸入任何字符,將自動生成id_rsa和id_rsa.pub文件。
-t 指定密鑰類型,默認是 rsa ,能夠省略。
-C 設置註釋文字,好比郵箱。
-f 指定密鑰文件存儲文件名。
以上代碼省略了 -f 參數,所以,運行上面那條命令後會讓你輸入一個文件名,用於保存剛纔生成的 SSH key 代碼.固然,你也能夠不輸入文件名,使用默認文件名(推薦),那麼就會生成 id_rsa 和 id_rsa.pub 兩個祕鑰文件。
ssh -T git@github.com(測試SSH key生成好了)
Git 使用
- git reflog 看到全部提交或刪除記錄
- git config —list(看git相關配置)
- git config --global user.name "[name]": Set the name you want atached to your commit transactions
- git config --global user.email "[email address]" : Set the email you want atached to your commit transactions.
- git config --global core.ignorecase false : 設置git對大小寫敏感.
- git config —global alias.ci commit (git ci === git commit)
- git config —global alias.last ‘log -1 HEAD’ (git last可看到最後一次提交)
create repositorygithub
- $ git init [project-name] : Create a new local repository with the specified name.
- $ git clone [url] : Download a project and its entire version history
make changes正則表達式
- git status
- git add [file]
- 使用 git diff 命令能夠查看工做區與暫存區之間的差別。
使用 git diff <gitreversion> 命令能夠查看工做區與指定版本之間的差別。
使用 git diff --cached 命令能夠查看暫存區與當前 HEAD 指針指向版本之間的差別。
使用 git diff --cached <gitreversion> 命令能夠查看暫存區與指定版本之間的差別。
使用 git diff -- <file> 能夠查看特定文件在工做區與暫存區之間的差別。
使用 git diff <gitreversion> -- <file> 能夠查看特定文件在工做區與指定版本之間的差別。
使用 git diff --cached -- <file> 能夠查看特定文件在暫存區與當前 HEAD 指針指向版本之間的差別。
使用 git diff --cached <gitreversion> -- <file> 能夠查看特定文件在暫存區與指定版本之間的差別。shell
- git reset [file] : Unstage the file, but preserve its contents.
- git commit -m "" : Record file snapshots permanently in version history.(message中能夠添加一些emoji的圖片。Example,git commit -m 「init the app @mlding")
- git commit - -amend - -no-edit
- git commit - -amend -m ’new commit message'
- git commit -a這個命令能夠直接提交全部修改,省去了你git add和git diff和git commit的工序 注意:沒法把新增文件或文件夾加入進來,因此,若是你新增了文件或文件夾,那麼就要老老實實的先git add .,再git commit
branch and mergeredux
- git branch: List all local branches in the current repository
- git branch [branch-name] : Create a new branch
- git checkout [branch-name] : Switche to the specified branch and updates the working directory
- git merge [branch] : Combine the specified branch’s history into the current branch
- git branch -d [branch-name] : Delete the specified branch
- git branch |grep 'daily' |xargs git branch -D
這是經過 shell 管道命令來實現的批量刪除分支的功能
git branch 輸出當前分支列表
grep 是對 git branch 的輸出結果進行匹配,匹配值固然就是 daily
xargs 的做用是將參數列表轉換成小塊分段傳遞給其餘命令
所以,這條命令的意思就是:從分支列表中匹配到指定分支,而後一個一個(分紅小塊)傳遞給刪除分支的命令,最後進行刪除。
- git branch --set-upstream-to=origin/: when add branch local,remeber add branch to remote before pull--rebase git branch -d xx //刪除分支,若是還未合master,則會出現不能刪除提示,用-D能夠強制刪除 git branch -v //查看分支的最後修改 分支前帶的爲沒有合併分支到master分支的,無的爲已經合併到master中,能夠將無的分支刪除掉。
- git branch -m old-name new-name(rename your local branch)
- git branch -D pr-mob-910 後想恢復該分支:git reflog(查看要恢復的倉庫版本號)—git branch pr-mob-910 commitId(就把要恢復的分支內容恢復到了pr-mob-910分支中了)
refactor the fileNamewindows
- $ git rm [file] : Delete the file from the working directory and stages the deletion
- $ git rm --cached [file] : Remove the file from version control but preserves the file locally
- $ git mv —force [file-original] [file-renamed] : Change the file name and prepares it for commit
- git rm -r --cached .idea
review history緩存
- git log --oneline: List version history for the current branch
- git diff [first-branch]...[second-branch] : Show content differences between two branches
- git show [commit] : Output metadata and content changes of the specified commit
save local changesapp
- $ git stash : Temporarily store all modified tracked(modified but not committed) files
- stash only certain files: 1. git add files you don’t want to stash; 2. git stash save —keep-index
- $ git stash : List all stashed files
- $ git stash pop: Restore the most recently stashed files
- $ git stash drop: Discard the most recently stashed files
tagssh
- git tag -a 'v8' -m 'fix':Create an annotated tag.
- git show-ref --tag
- git tag: List the available tags.
- git push origin v1.5: 分享標籤到遠端倉庫
- git push origin --tags: 一次推送全部本地新增的標籤上去
- git checkout tag_name : Check out the code of this tag
- git tag -d [tag name] : Delete tag
- git push origin :refs/tags/tag名字
- git tag | grep "publish" |xargs git tag -d(批量刪除本地tag)
cherry-pick:
在本地 master 分支上作了一個commit 如何把它放到 本地 old_cc 分支上ide
- git checkout old_cc
- git cherry-pick commit-id
git pull/git push
- git pull <遠程主機名> <遠程分支名>:<本地分支名>
- git push <遠程主機名> <本地分支名>:<遠程分支名>
- git push origin —delete MOB 255==git push origin :MOB 255(刪除遠程分支)
git rebase(rewrite history)
- p, pick = use commit
- r, reword = use commit, but edit the commit message
- e, edit = use commit, but stop for amending
- s, squash = use commit, but meld into previous commit
- f, fixup = like "squash", but discard this commit's log message
- x, exec = run command (the rest of the line) using shell
Tips
Tip0 批量刪除git 本地分支、遠程分支、tag
批量刪除本地分支
git branch -a | grep -v -E 'master|develop' | xargs git branch -D
批量刪除遠程分支
git branch -r| grep -v -E 'master|develop' | sed 's/origin\///g' | xargs -I {} git push origin :{}
若是有些分支沒法刪除,是由於遠程分支的緩存問題,可使用git remote prune
批量刪除本地tag(刪除前如今本地拉取遠程全部tags)
git tag | xargs -I {} git tag -d {}
批量刪除遠程tag
git tag | xargs -I {} git push origin :refs/tags/{}
用到命令說明
grep -v -E 排除master 和 develop
-v 排除
-E 使用正則表達式
xargs 將前面的值做爲參數傳入 git branch -D 後面
-I {} 使用佔位符 來構造 後面的命令
Tip1. 改變commit歷史記錄
git rebase -i head~2
change commit from pick to edit
git commit —amend —author=「"
git rebase —continue
若是要改多個commit的author email,能夠繼續重複改
git rebase —continue(The rebase would complete.)
- amend change to history commit(not the last)
git rebase -i head~2
change commit from pick to edit
git stash pop(當前的改動)
git commit —amend(添加到edit的那個commit上面了)
git rebase —continue
- rewrite history commit message
git rebase -i head~2
change commit from pick to edit
git commit —amend -m ‘ '
git rebase —continue
git rebase -i head~2
change commit from pick to squash :eq
input new message
Tip2. delete all local branches except master
git branch | grep -v "master" | xargs git branch -D
Tip3. git config core.autocrlf
- Carriage-Return(回車)
- Line-Feed(換行)
- CRLF(windows-style) 表示句尾使用回車、換行rn
- LF(unix-style) 表示句尾只使用換行
- CR(mac-style) 表示句尾只是用回車
Windows上的msyspit默認設置了autocrlf爲true,這樣在提交時自動的把行結束符CRLF轉換成LF,而簽出代碼時把LF轉換成CRLF,這樣保證了在Windows平臺上提交的代碼都是以LF做爲行結束符; Linux平臺上,git默認設置autocrlf爲false,即它不會自動處理CRLF;x is CRLF(windows) or LF(unix-style), -> is file to commit -> repository -> checked out file:
- autocrlf = true (if you have unix-style, LF in one of your files):x -> LF -> CRLF
- autocrlf = input (if you have windows-style, CRLF in one of your files): x -> LF -> LF
- autocrlf = false (never): x -> x -> x
git reset || git checkout || git revert :
易混淆命令對比
git rm
VS rm
- 用
git rm
來刪除文件,同時還會將這個刪除操做記錄下來,git rm 刪除過的文件,執行 git commit -m "abc"
提交時,會自動將刪除該文件的操做提交上去
- 用
rm
來刪除文件,僅僅是刪除了物理文件,沒有將其從 git 的記錄中剔除。用 rm 命令直接刪除的文件,執行 git commit -m "abc"
提交時,則不會將刪除該文件的操做提交上去。不過沒關係,即便你已經經過 rm 將某個文件刪除掉了,也能夠再經過 git rm 命令從新將該文件從 git 的記錄中刪除掉,這樣的話,在執行 git commit -m "abc" 之後,也能將這個刪除操做提交上去。
在被 git 管理的目錄中刪除文件時,能夠選擇以下兩種方式來記錄刪除動做:
- rm + git commit -am "abc"
- git rm + git commit -m "abc"
另外,git add . 僅能記錄添加、改動的動做,刪除的動做需靠 git rm 來完成。
最後,rm 刪除的文件是處於 not staged 狀態的,
也就是一種介於 「未改動」 和 「已提交過」 之間的狀態。
對比圖:
git reset
vs git checkout
vs git revert
vs git reset
- git reset
- git checkout (can manipulate commits and individual files)
- git revert (used to undo changes on a public branch)
- git reset (reserved for undoing changes on a private branch)
git reset
commits:
git reset --hard [commit] : Discard all history and changes back to the specified commit
git reset(此爲默認方式—mixed,不帶任何參數的git reset,就是這種方式,它回退到某個版本,只保留源碼,回退commit和暫存區信息)
git reset —soft(會退到某個版本,只回退commit,不會恢復暫存區信息)
git reset --hard e76855b(撤銷到e76855b指定的那個commit來,在此以後的commit全都刪除了,修改也沒有)
git reset e76855b(撤銷到e76855b指定的那個commit來,在此以後的commit全都刪除了,可是在此以後的修改都會退到暫存區)
files:
git reset HEAD~2 foo.py
The --soft, --mixed, and --hard
flags do not have any effect on the file-level version of git reset, as the staged snapshot is always updated, and the working directory is never updated.
git checkout
git checkout pr-mob-910(switch between branched)
git checkout HEAD~2(since there is no branch reference to the current HEAD, this puts you in a detached HEAD state. This can be dangerous if you start adding new commits because there will be no way to get back to them after you switch to another branch. For this reason, you should always create a new branch before adding commits to a detached HEAD.)
files:
git checkout HEAD~2 foo.py
Questions
Q1.本地一個工程目錄推送到遠端github倉庫:
- 本地該工程目錄下:git init(建立一個新的git版本庫,這個版本庫的配置,存儲等信息都會被保存到.git文件夾中,若是不想該目錄被git管理,rm -rf .git)
- git remote add origin git@github.com:miyading/redux-test.git
Q2. ~ VS ^
- Both ~ and ^ on their own refer to the parent of the commit (~~ and ^^ both refer to the grandparent commit, etc.)
- But they differ in meaning when they are used with numbers:
- ~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent
- ^2 means the second parent where a commit has more than one parent (i.e. because it's a merge)
- These can be combined, so HEAD~2^3 means HEAD's grandparent commit's third parent commit.
Q3. transfer A(local commits) to B
A :git daemon --export-all --base-path=.
B :git pull --rebase git://[B's IP Address]/Q4. git process occupied rm -f ./.git/index.lock Generally such problems occurs when you execute two git commands simultaneously maybe one from command prompt and one from IDE.Q5. Git has a limit of 4096 characters for a filenamegit config --system core.longpaths true