Git使用笔记2

工做必备:git

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

//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不一致】app

【建立并提交分支】
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/FirstCommitwebstorm

【git remote add origin 网址】
添加远程主机,origin也是命名的,多是git最初的建议名ide

【分支】
删除本地分支
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.gitfetch


【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 ]ui


【【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]this


【【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

相关文章
相关标签/搜索