Git常用
Git
第一次使用
从origin上clone代码
Eg:
cd myGitProj
git clone o2o-business-admin
本地会自动生成和origin/master相同的master
git pull origin dev_dosh:dosth
//git pull <远程库名> <远程分支名>:<本地分支名>
//从远程dev_dosh分支拉代码到本地dosth分支并merge,省略冒号及后面的,默认与当前分支merge
//等价于 git fetch origin dev_dosh:dosth && git merge dosth
git checkout origin/remoteName -b localName
获取远程分支remoteName 到本地新分支localName,并跳到localName分支
git branch
//可查看当前分支处于master
git checkout -b dev_dosth
//创建并切换到dev_dosh分支,该分支与当前分支(master)相同
...modify somefiles...
git add.
git commit -m "what I did"
git push origin dev_dosth:dosth
//git push <远程主机名> <本地分支名>:<远程分支名>
//在远程创建dosth分支并将本地dev_dosth分支内容merge上去
test my modifition
gitlab上提交merge request
上线后可删除分支
第二次用
git checkout master
git pull
//git "undo" cmd
add前撤销所有改动
git checkout .
撤销sfile.txt的改动
git checkout -- sfile.txt
撤销add
git reset filename
撤销commit
git reset oldCommitId
抛弃当前合并冲突的处理过程并尝试重建合并前的状态
git merge --abort
git 合并多个commit
git rebase -i commitId
fixup/squash/pick/……
https://www.jianshu.com/p/964de879904a
commit后、merge到master前撤销对file的改动,使其恢复到与master相同
git rm --cached file
############################
总结:
以dev merge到master为例,即:
git checkout dev
git merge/rebase master
merge:
多个分支的时间线将严格按照统一时间线进行合并,即dev和master的时间线上会有交叉
rebase:
会把整个dev分支移到master分支后面,master上如果有新的提交(时间上后于本dev),时间线上也会先于dev的commit【rebase代替合并】
也就是说,rebase会改写历史纪录-----黄金法则
可考虑 git rebase -i master
###########################
What is the difference between 'origin' and 'remote' in git commands?
No, they don't mean the same thing.
remote
, ingit
-speak, refers to any remote repository, such as your GitHub or anothergit
server.
origin
is the, by convention, default remote name ingit
. When you do agit clone <url>
,<url>
is automatically added to your local repo under the nameorigin
. You can, of course, add other remotes under different names usinggit remote add
.When you do
git push -u origin master
, what it means is "push everything from my local master to the remote namedorigin
". The structure of this command is, of course, more general - the more general form isgit push -u <remote> <branch>
, which will push the branch namedbranch
to the designated remote, creating it at the far end if the remote doesn't already have it (that's what the-u
flag does).As a further addendum,
git push
, by default, will push the current branch toorigin
, corresponding togit push origin <current-branch>
.
###########################
大部分情况下,不知道怎么办
git status 会告诉你
https://www.jianshu.com/p/67f20d19605a
将本地项目push到github参考: