筆記內容:
基本的git操作
參考文檔
https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
https://www.atlassian.com/git/tutorials/using-branches/git-merge
git status
,git log
,git show
git status
: 一個很general的項目情況,比方說你在哪個branch上,有沒有commit(或者是否up to date到你clone過來的那個repo)
git log
: 看到每個commit的編碼SHA,一個short description, 還有關于commit的時間和作者。如果想只看編碼前幾位和description,則git log --oneline
git log --stat
查看每個commit里具體都修改了啥, 哪個文件改動了,添加或者刪掉了多少行
git log -p
查看每個commit里具體是哪個文件,第幾行改動了,會提示某個文件的第幾行到第幾行被刪掉了,多少行是加上的,并顯示這些修改過的內容
git log -p XXXXXXX
顯示SHA碼前7位為XXXXXXX的commit,及其之后的commit
git show XXXXXXX
只顯示一個commit的信息把別人的庫(Repository) fork 過來,在本地修改調整之后add, commit, pull request,然后等別人merge你的修改
中文參考
git clone XXXX.git
cd tmap/
在本地一頓操作修改代碼,然后add,commit
git add .
git commit -m "fixed blablablabla" # 可以是很短幾句話
git log # 看一下是不是加上去了
commit XXXXXXXXXXXXXXX
Author: XXXX <XXXXXXXXXXXX>
Date: Fri Jan 17 13:34:43 2020 +0800
fixed blablablabla
push上去
git push origin master
# 按照指示輸入你github的用戶名及密碼
Username for 'https://github.com': XXX
Password for 'https://XXX@github.com':
Counting objects: 8, done. #比方說你改了8個地方
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 869 bytes | 0 bytes/s, done.
Total 8 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), completed with 6 local objects.
To https://github.com/XXX/tmap.git
XXXXXXXX master -> master
然后去你自己github的主頁,看到你已經Fork了的項目:
然后點pull request,再補充一些說明的文字就好了。
- git status
# 比方說這時候你在本地改了代碼,還沒有add,
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: XXXX.py
modified: XXXXXXXXXX.py
modified: XX.py
no changes added to commit (use "git add" and/or "git commit -a")
# 這是在提醒你你改了還沒add不能commit,趕緊去add
- git branch
查看本地分支 - (markdown) 在README.md中加入checkbox和emoji =_=
- Finish my changes
- [x] Finish my chages
- Push my commits to GitHub
-[ ] Push my commits to GitHub
參考https://help.github.com/en/github/managing-your-work-on-github/about-task-lists
參考https://github.com/ikatyang/emoji-cheat-sheet/blob/master/README.md
- 建私倉
- 在本地建local branch, remote,然后merge到master上
merge branch into master
- 先看一下自己有什么branch
$ git branch
- 在新建一個branch之前,先確定本地的master是up-to-date
git pull
- 新建并激活你的本地branch
git checkout -b cs_shuffle
# branch 名字叫cs_shuffle
這時候應該已經在cs_shuffle這個branch里面了
這時候再git branch可以看到cs_shuffle了 - 然后在本地修改代碼,一頓操作
... ...
- add, commit
$ git add –A
$ git commit –m "Some commit message"
- 回到master下
git checkout master
- merge到master
git merge cs_shuffle
- 把branch推到remote: public a local branch
git push origin master
撤回一個commit (undo the last commit)
git reset --soft HEAD~1
這里--soft表示把commit移到stage indexing區域中去了,需要重新commit的話再commit就好了。 HEAD~1表示把head指針往前移動了一格,即移動到前一個commit的位置。
--mixed表示移動到working directory(你需要再add一遍),--hard表示直接刪掉.gitignore
在這里輸入不想納入add或者commit(即不想track)的文件,后面即使git add .
也不會納入它顯示所有的branch(哇太炫了
git log --oneline --graph --decorate --all
合并沖突 merge conflict
當兩個branch, 在同一行都有修改,就會出現merge conflict
在出現沖突的時候,要檢查一下你要保留哪個修改。可以在沖突的文件中搜索“>>>>”,以定位到沖突的位置,修改后再add,commit就好了git revert SHA
把某個commit給撤回,同時新建一個commit說明這個撤回情況