Table of Contents

收集一些git教程和常用命令

git常用命令速查



git init             # 開始用 Git 來管理



git add .            # 把所有檔案加入 Git



git commit -a        # 做成一個版本



git commit -a -m "your message here"  # commit 時直接寫訊息, 不用到下個螢幕上寫



git add mine.txt     # 把 mine.txt 加入管理



git tag v0.02        # 本版別名取為 v0.02



git tag v0.03 40e3e  # 把版本 40e3e 取名為 v0.03;







git status           # 查詢從上個版本到現在哪些檔案被修改



git diff             # 查詢從上個版本到現在哪幾行被修改



git diff v0.01 v0.03 # 查詢兩個版本間的差異



git diff v0.01:story.txt v0.03:story.txt    # 查詢兩個版本間某個檔案的差異







git log     # 查詢有史以來哪幾個檔案被修改



git log -p  # 查詢有史以來哪幾行被修改



git log --stat --summary  # 查詢每個版本間變動的檔案跟行數







git show v1.01            # 查詢 v1.01 版裡的修改內容



git show v1.01:story.txt  # 叫出在 v1.01 版時的 story.txt 檔案



git show v1.01:story.txt > test.txt   # 把 v1.01 版的 story.txt 抓出來,放入一個叫 test.txt 的檔案



git show HEAD             # 看此版本修改的資料



git show HEAD^            # 看此版本前一版的修改的資料



git show HEAD^^           # 看此版本前前一版的修改的資料



git show HEAD~4           # 看此版本前前前前一版的修改的資料 >_<







git grep "貓" v0.01   # 查詢 0.01 版裡頭有沒有貓



git grep "熊"         # 查詢現在的版本裡有沒有熊







git branch 加英文版    # 建立新的分枝叫 "加英文版"



git branch            # 查看現有的分枝



git branch beta v0.5  # 依照 "v0.5" 版裡的內容來建立一個叫 "beta" 的分枝



git checkout 加英文版  # 換到 "加英文版" 的分枝上



git checkout master   # 換到主幹上



git branch -d 加英文版 # 刪除 "加英文版" 分枝







gitk    # 版本瀏覽軟體



git gui # 瀏覽從上一版本到現在變動的地方; 沒辦法顯示中文







git gc                # 維護 Git 的資料庫



git fsck --full       # 同上, 三不五時下次指令








git reset --soft HEAD^ #commit完后,发现有些文件改错了,或者没有add进去,用这个命令可以重新commit,添加comment.



git push -f origin master#当用过reset后,重新commit,往上push的时候,可能会遇到



                            #! [rejected]        master -> master (non-fast-forward),这个时候,如果确定没有冲突的话,可以加-f(force)强制push上去.







git branch test tag#以某个tag生成branch,再用git checkout test,切换到那个branch.






Github教程(转载)

创建和合并分支



#git branch 显示当前分支是master



#git branch new-feature  创建分支



# git checkout new-feature 切换到新分支



# vi page_cache.inc.php



# git add page_cache.inc.php



Commit 到本地GIT



# git commit -a -m "added initial version of page cache"



合并到远程服务器



# git push origin new-feature







如果new-feature分支成熟了,�
��得有必要合并进master



#git checkout master



#git merge new-feature



#git branch



#git push 



则master中也合并了new-feature 的代码


通过tag创建Branch,并提交

git tag v1.0.0 2c1b73235f77e6204e0b584cfae692486ccf3e92
git branch leili-v1.0.0 v1.0.0
git checkout leili-v1.0.0
git push origin leili-v1.0.0

删除服务器branch

git push origin  :server-branch-name

git checkout remote branch

Check the remote branch list

git branch -rv

Then create a local branch from the remote branch

git checkout -b test origin/test

Commit to a remote branch:

git push origin proguard

Reference:http://stackoverflow.com/questions/1783405/git-checkout-remote-branch

git add local to the remote

git init
git remote add origin https://github.com/pjq/SoundRecordingExample2.git
git status
git pull
git pull origin master
git status
git add .
git commit -m "init commit"
git push origin master

change url

git remote set-url origin https://github.com/jikabao/merchantapp_android.git

create patch

git show 3514997d38509ab5492e32afc3e4e872daf7932e >DashboardGLPLTitle.patch
patch -p1  <DashboardGLPLTitle.patch

Or you can create patch between two commitid

git diff old-commitid new-commitid >diff.patch
git diff 557393295b0811278de4875d5fc5558988371bca 0abea5c74c10b81672817aa93ae21cc4852b7702 >diff.patch

If you want to revert your patch -R

patch -R -p1 <../diff.patch

git auto-complete

http://blog.jameschevalier.us/how-to-add-auto-completion-to-git/

wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion
sudo vim /etc/bashrc
source /Users/pengjianqing/.git-completion

git alias

http://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.visual '!gitk'

git tag

   git tag release/1.2.9
   git tag -l
   git push origin tag release/1.2.9
  

git checkout tag

First make sure that the tag exists locally by doing

git fetch --tags

Then check out the tag by running

git checkout tags/<tag_name>

Then create new branch base on the current commitment

git branch newtagbranch

git fetch & git merge

Refer to

git checkout feature/sp_xd
git fetch origin feature/sp_xd:tmp
git diff tmp
git merge tmp

git checkout feature/sp_xd
git fetch --all
git fetch origin feature/sp_xd
git diff feature/sp_xd origin/feature/sp_xd
git merge origin/feature/sp_xd

git branch -D tmp

git checkout feature/sp_xd
git fetch origin feature/sp_xd
git checkout -b newBrach origin/feature/sp_xd

#Delete the local branch that already deleted from the remote
git remote prune origin

#Remote server
git remote -v

#Remote branch
git branch -r
#All branch
git branch -a

#Show the remote repo information
git remote show origin
git remote prune origin

git subtree

Refer to http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/

git remote add -f datalayer   https://Jianqing.Peng@stash.englishtown.com/scm/mobile/datalayer-android.git 
git subtree add  --prefix=datalayer datalayer integration_lean --squash
git fetch all  --prune   
git fetch datalayer integration_lean
git subtree pull --prefix=datalayer/ datalayer integration_lean --squash
git subtree push --prefix=datalayer/ datalayer integration_lean

git checkout specified file from another branch

git checkout branch_name -- path/to/file

git revert

github support revert the pull request, so after you have revert the pull request, and still want to merge the feature branch, then you need to find the revert commit id, and revert it again

git revert ce8731f5cbc55ab50723ee2a8ec615b462b3b0e6 -m 1

So the full log will be like

commit 817947e37c5f835384fd282ab2004a6d9a848975 (HEAD -> pjq/testing, origin/pjq/testing)
Author: Jianqing Peng <Jianqing.Peng@sap.com>
Date:   Mon Feb 5 19:09:40 2018 +0800

    Revert "Merge pull request #2087 from sfofficial/revert-2086-pjq/testing_new_feature"
    
    This reverts commit ce8731f5cbc55ab50723ee2a8ec615b462b3b0e6, reversing
    changes made to 67a513f686ec9c203b7d38a3df1abbe7079320d4.

commit ce8731f5cbc55ab50723ee2a8ec615b462b3b0e6
Merge: 67a513f686 a157cdef4e
Author: Jianqing Peng <jianqing.peng@sap.com>
Date:   Mon Feb 5 19:07:33 2018 +0800

    Merge pull request #2087 from sfofficial/revert-2086-pjq/testing_new_feature
    
    Revert "add new feature 1"

commit a157cdef4e391981b2053b52f560a70464b2dd7f
Author: Jianqing Peng <jianqing.peng@sap.com>
Date:   Mon Feb 5 19:06:29 2018 +0800

    Revert "add new feature 1"

commit 67a513f686ec9c203b7d38a3df1abbe7079320d4
Merge: 08a5c9c33e a161eed1e3
Author: Jianqing Peng <jianqing.peng@sap.com>
Date:   Mon Feb 5 19:05:57 2018 +0800

    Merge pull request #2086 from sfofficial/pjq/testing_new_feature
    
    add new feature 1

commit a161eed1e3d200f6121690494dd6df1d065163fb (origin/pjq/testing_new_feature, pjq/testing_new_feature)
Author: Jianqing Peng <Jianqing.Peng@sap.com>
Date:   Mon Feb 5 19:05:11 2018 +0800

    add new feature 1