1. 程式人生 > >Git使用筆記2

Git使用筆記2

att -s one resolv git merge delet apply 解決 工程

工作必備:

【更新master】
git checkout master
git pull
git checkout zyb/FirstCommit
git merge master

//git rebase master --> 更新 zyb/FirstCommit 分支到 master 的最新版本

【多次 commit,日誌太多,處理方法:】
git log --graph [git log 某次提交號(適用於很多次commit的情況)]
git reset **[創建此branch前的編號]
git log [創建此branch到最後一次commit間的log會被刪除]
git add . [撤銷:git reset HEAD <file>...]
git commit -m "commentment"
git push -f origin zyb/FirstCommit 【一定是 push 到自己的branch,-f 是因為log不一致】

【創建並提交分支】
git status
git pull
git checkout -b zyb/FirstCommit
git branch -b zyb/FirstCommit
git checkout zyb/FirstCommit
git diff --color
git add . [或*/*/*.sh]
git commit -m "commentment"
git push origin zyb/FirstCommit

【git remote add origin 網址】
添加遠程主機,origin也是命名的,可能是git最初的建議名

【分支】
刪除本地分支
git branch -d the_local_branch #### If you are sure you want to delete it, run ‘git branch -D zyb/modifyApiServiceDevconf‘.
git branch -D the_local_branch
刪除遠端分支(if you know what you are doing!)
git push origin :the_remote_branch
查看遠端分支
git branch -a
獲取遠端分支
git fetch,可以將遠程分支信息獲取到本地
git fetch origin master
git fetch origin zyb/certainBranch
git checkout -b local-branchname origin/remote_branchname 就可以將遠程分支映射到本地命名為local-branchname 的一分支


【測試機搞一把】
git clone http://code.huawei.com/cloud-service-dev-team-devops/cloudwatch.git


【git 緩存http用戶名&密碼】
git config --global credential.helper ‘cache --timeout=2592000‘
git config --global credential.helper wincred --replace-all
刪除配置: git config --global --unset core.excludesfile
timeout 的時間單位為秒,可以自行修改. (示例中表示緩存 30 天)
設置完後,再使用 http 協議操作倉庫時,只要輸入了一次用戶名和密碼,在緩存時效內就不需要再輸入了。
[可參考 http://pages.huawei.com/codeclub/guides ]


【【reset並解決沖突】】
1. reset到需要squash的版本號
git log ******* --graph
git reset *******的前一個
【這裏最好把版本號記錄在案,出錯後還能reset回去】
2. 更新master
git remote update
git pull origin master
一般在pull origin master的時候,git會提示無法執行,並建議move或remove一些文件,這些文件如果是自己改過的就move,pull之後再拷貝回來;如果不是自己的,直接rm
如果其建議你commit或stash,可以這樣
git stash
git pull origin master
git stash pop
3. 處理conflict
使用webstorm的GUI,右鍵工程任意目錄或文件,選擇 Git - Resolve conflicts
Changes to be committed:
如果是自己的,不用管了;
如果不是自己修改的,使用 git reset HEAD <file> ... 撤銷[從工作區放回暫存區]
Changes not staged for commit:
如果是自己的,使用 git add <file> ...
如果是自己刪除的,使用 git rm <file> ...
【Attention:git add/rm 都會被提交】
如果不是自己修改的,使用 git checkout -- <file>... 撤銷[從暫存區刪除]
Untracked files:
如果是自己的,使用 git add <file> ...
否則,忽略
4. 提交並推到遠端
git commit -m "comments"
git push origin zyb/FirstCommit -f [由於使用了reset,版本號已經不符合要求了,所以-f]


【【git stash】】
git stash list
git stash pop
git stash apply stash@{1}

git reset --hard origin/master
以後如果不小心在master上改代碼了,可以用這個恢復本地的master

git reset --hard HASH #返回到某個節點,不保留修改。
git reset --soft HASH #返回到某個節點。保留修改

【Your branch is ahead of ‘origin/master‘ by 3 commits.】
You get that message because you made changes in your local master and you didn‘t push them to remote. You have several ways to "solve" it and it normally depends on how your workflow looks like:

In a good workflow your remote copy of master should be the good one while your local copy of master is just a copy of the one in remote. Using this workflow you‘ll never get this message again.
If you work in another way and your local changes should be pushed then just git push origin assuming origin is your remote
If your local changes are bad then just remove them or reset your local master to the state on remote git reset --hard origin/master

Git使用筆記2