Git 常见命令用法

Git命令大全:掌握高效版本控制技巧

 Git 常见命令用法

   git clone git@gitlab.alibaba-inc.com:zhangchen.zc/moneyshield.git 克隆远程工程到本地

   git fetch:相当于是从远程获取最新版本到本地,不会自动merge

   git pull:相当于是从远程获取最新版本并merge到本地(相当于git fetch 和 git merge)

   git pull --rebase 将本地的工程与远程同步

   git pull -X theirs origin local_branch 当远程分支进行了修改,pull 的时候两者自动进行合并,可以通过指定合并策略来接受远程修改,丢弃本地的所有修改

   git add filename 将修改过文件添加到changlist 里面,为git commit 创建change list

   git checkout -- filename  将已经修改过的文件revert 回去

   git checkout -b A-branch origin/A-branch 将远程的分支创建到本地,并且关联起来

   git checkout -f -b release release-integrate 重命名分支

   git commit --amend 修改当前的commit(还没有push)

   git diff HEAD~1 比较之前在HEAD上的一个commit 的改动

   git branch -r   #查看远程所有的分支
   git branch -lvv(或-vv) #查看每个branch 对应的服务器端的分支
   git branch -lv(或-v)   #查看各个分支最后一个提交对象的信息
   git branch --merge #查看哪些分支已被并入当前分支(译注:也就是说哪些分支是当前分支的直接上游)
   git branch -D local_branch 删除本地分支
   git branch --set-upstream-to=origin/mainline temp #将本地temp branch与远程的origin/mainline 映射起来

 ######## 删除commit ####### 

  git reset HEAD filename 将已经“git add”过的文件从commit 的change list 里面移除 
   git reset --soft commitId:回退到某个commitId,commitId之前的改动的代码仍然存在,处于“git add”过的状态
   git reset --mixed commitId:回退到某个commitId,commitId之前的改动的代码仍然存在,处于“git add”之前的修改状态(此为默认方式,参数--mixed可以省略)
   git reset --hard HEAD~1 删除之前在HEAD上的一个commit
   git reset --hard commitId 删除HEAD 至commitId之前的所有提交(不包括commitId)

   git push origin HEAD --force 强制将远程的HEAD和本地HEAD设置一致,一般和git reset配合使用删除远程的commit】

######## 合并commit ####### 

      git rebase -i HEAD~2 合并HEAD上两个commit

      git rebase -i commit-id  当在本地commit多笔,用这个命令可以选择把当前分支HEAD至commit-id(不包括commit-id) 的所有commit合并起来作为一个commit。合并时提示有如下参数选择:

    // pick 使用前commit
    // r 合并当前commit到前一个,但是可以修改commit提交的message
    // s 合并当前commit到前一个。但是不能修改commit提交的message
    // drop 删除对应的commit

######## 合并分支 ####### 

 $ git rebase branch-A // 将A分支的commit都搬到当前分支,然后将当前分支commit放在最上面

  $ git merge --ff branch-A. // 将A分支commit都搬到当前分支,然后将当前分支的commit放在上面:若有冲突的话会提示解决冲突,解决完会提示提交一个新的commit;若没冲突不会提交新commit(--ff为默认参数,可以不写)
  $ git merge --no-ff branch-A // 无论是否冲突,都会merge完后提交一个新的commit
  $ git merge --squash branch-A // 不保留原始commit信息,直接将所有commit合并提交一个新的commit


   ########基于master分支在远程创建一个新分支########
   1. git branch new_branch
   2. git push origin new_branch (git push origin : remote_branch  冒号表示删除远程分支)

   ########如何将本地分支创建到远程服务器########
   1. git checkout -b local-branch-name
   2. git push remote-branch-name local-branch-name:remote-branch-name
   3. git branch --set-upstream-to=origin/remote-branch-name (或者 git branch --set-upstream master origin/remote-branch-name)

   ########如何把B分支integrate 到A分支,首先应切换到A 分支(HEAD为A分支)########
   1. git rebase --onto HEAD commit-id-1  commit-id-3

       注意:上面的命令只会integrate  commit-id-2 到  commit-id-3,起点是从commit-id-1的后一个开始!
      (参见 http://weblog.avp-ptr.de/20120928/git-how-to-copy-a-range-of-commits-from-one-branch-to-another/)
   2. git rebase HEAD A-Branch 
   3. git commit --amend

       

 

   git log origin/mainline 查看远程的log
   git log    查看本地的log
   git log -2 查看最近的两个commit 
   git log -2 branch 查看某个分支最近的两个commit 
   git log -n 2 查看最新提交的两个commit(等同git log -2 )
   git log -n 1 --stat 查看最新一次commit修改了哪些文件
   git log -n 1 -p 查看最新一次commit修改细节
   git log filename(带路径)#查看该文件commit 历史
   git blame filename #查看该文件每行对应的commit 

   git stash 隐藏当前不需要commit 的所有file
   git stash pop 恢复当前隐藏的所有file
   git stash save 保存当前隐藏的所有file
   git stash list  查看当前隐藏的list

   git cherry-pick <commit id> 把另一个本地分支A的commit merge到当前B分支(在B分支上进行操作)

   git pull -f origin remote-branch 从远端origin拉取remote-branch分支的内容然后合并到当前所处的本地分支

   git pull -f origin remote-branch:local-branch 从远端origin拉取remote-branch分支的内容然后合并到本地local-branch分支。直接git pull的话默认远程库是orgin,默认的分支是master
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值