git 的常用用法

删除远程分支: 
git push origin --delete <branchName> 如: git push origin --delete release/release
切换分支:git checkout name
撤销修改:git checkout -- file
删除文件:git rm file
查看状态:git status
添加记录:git add file 或 git add .
添加描述:git commit -m "miao shu nei rong"
同步数据:git pull
提交数据:git push origin name
分支操作
查看分支:git branch
创建分支:git branch name
切换分支:git checkout name
创建+切换分支:git checkout -b name
合并某分支到当前分支:git merge name
删除分支:git branch -d name
删除远程分支:git push origin :name


git push <远程主机名> <本地分支名>:<远程分支名>  将本地分支push到远程分支上


git log --graph 最近的接交图


git branch -vv 查看分支最近的提交图

git clone url #克隆新的版本库
git init 
git pull repo_name #有关联的远程库,抽取并和本地合并
git fetch remote_repo_name #抽取并新建分支


rename:
git branch -m <oldname> <newname>

If you want to rename the current branch, you can simply do:

git branch -m <newname>


 #在当前commit对象上新建分支 指针head#head指向正在工作中的本地分支的指针(别名)#不会切换到新建的分支上git branch branch1#切换分支将head指向branch1git checkout branch1 #工作流程卡#在不同的分支里反复切换,并在时机成熟时把他们合并到一起#git的分支实际是一个包含所指向对象校验和的文件(40个字符长度SHA-1字串)#分支的新建和合并#0、自己工作分支mybranch,工作的好好的 #1、突然有新需求,先切换到生产环境分支product;git checkout product#2、为新需求新建分支branch_pack,切到其中,并在其中编码,直到通过测试用例# -b 新建分支并切换到其上git checkout -b issueXXXX #3、切换到生产环境分支product,将2中的开发工作分支branch_pack合并进来,然后推送到生产服务器上git checkout product# 合并git merge branch_pach#冲突解决,merge失败时很可能因对同一文件的同时更改,所以必须手动人工解决 取舍代码,确认冲突解决后需要把冲突文件 git add到staged area,运行git status查看所有状态确保无误后再git commit提交,注释好冲突为什么这么解决git push 远程仓库名 本地仓库名#分支删除git branch -d branch_pach4、切换到mybranch继续自己的工作git checkout mybranch #切换分支最好没有待提交的文件,即stage area是清洁的#查看各个分支最后一个提交对象的信息git branch -vgit branch --merge/no-merged#查看哪些分支已被并入当前分支(译注:也就是说哪些分支是当前分支的直接上游#给分支生成patch文件可以给git format-patch #把本地名为serverfix的分支推送到远程origin库的awesomebranch分支git push origin serverfix:awesomebranch #跟踪远程分支 从远程分支checkout出的本地分支成为 跟踪分支git checkout -b sf origin/serverfixgit checkout --track origin/serverfix #删除远程分支git push origin :serverfix git两种开发分支方法长期分支:一个主分支保持稳定代码,其余多个开放分支特性topic分支:多个短期单一功能的分支#给当前文件 打特定版本标签git tag -a beta0.1 -m "some 注释"#回退到某个taggit tag beta0.1






git cherry-pick   <commit-id>                      //能把别的分支的commit-id移到当前分支。   也可能恢复删除的commit;

git cherry-pick  <deleted commit-id>

 

git reset HEAD^                                           //删除本次的commit,恢复到上次的提交状态。本次提交的内容并不删除.

git reset --hard  HEAD^                              //删除本次的commit,   并将当前的代码恢复到上次提交的状态。

git reset --hard <delete commit-id>         //恢复已删除的commit.

 

git log                                                            //查看所有的commit历史.

git reflog                                                       //查看所有的log, 包括删除的log.

 

git blame    <commit-id>  <file>               //查看file的修改记录.

 

git am    -3    <patch-file>                         //应用一个patch

git apply                                                     //应用一个path到某个文件或commit;

git format-patch  HEAD^                         //将当前的commit 生成patch文件

 

git   bisect   start   <commit-id>              //开始一个二分法查错。

git   bisect   good                                     //标记一个正确的版本

git   bisect   bad                                       //标记一个错误的版本

 

git diff     <HEAD1> <HEAD2>              //对比这两个HEAD之间的区别.

git difftool  <HEAD1>  <HEAD2>         //同git diff,     但可以用可视化工具对比.

git difftool   -g  -t

 

git fetch                                                     //fetch一个别的地方的分支到当前仓库

git checkout                                             //切换分支

git checkout ./                                          //撤消当前项目所有未提交的改动

git checkout -b <new branch>  branch   //创建一个基于branch的新分支new branch

git branch                                                 //查看当前仓库的本地branch

git branch -a                                            //查看当前仓库的所有 branch,   包括远端的信息

git branch --track <new branch>   <remote branch>

//创建一个追踪branch,   用于本地与远端的同步。git pull, git push使用.

git pull                                                       //同步远端分支.

git push                                                    //将本地的commit提交的远端.

git remote -v                                            //查看远端信息

git remote add <remote name>  <remote url>

//添加一个远端,

git remote add r   [email protected].2.x:path/xx.git

//添加一个可提交的远端.

git  clone   <remote url>                       //从远端复制一个git仓库,   和fetch的功能类似

git  tag   <new tag>                               //给当前HEAD打一个tag

git  tag -d <tag name>                         //删除一个tag;

git add  .                                                 //将当前项目的改动添加到git cache中

git  commit -am 'msg'                           //将当前改动提交.

git grep    <str>                                     //在仓库中搜索字符串

 

git stash                                                //将未commit的index缓存起来。

git stash   list                                        //查看所有的index缓存.

git stash  apply                                    //应用最后一次缓存的index.

git stash apply stash@{2} //应用指定的缓存.

git stash  apply --index

git stash  pop                                      //应用最后的存储并从缓存中移除

git stash  branch testchanges          //从存储中创建分支.

git stash show -p stash@{0}  | git apply -R

git stash show -p | git apply -R


git下修改commit的message的方法

1.用git rebase 切换到要修改的commit上,接下来修改当前commit的信息,最后再还原commit.

一不小心commit时message写错了,找了好长时间,走了不少弯路,才找到方法……
git rebase -i master~5
这个是找出master分支最近5次的commit,看见那个写错的了吧?把pick改成edit,保存退出。
此时工作目录已经变成这次commit的样子了,不要慌,然后:
git commit --amend -m "message"
修改这次的message。最后:
git rebase --continue
怎么样,成功了吧?

利用这个方法也可以修改commit的文件。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章