`

git常用

    博客分类:
  • git
 
阅读更多


 ------------------------ 自己整理:-----------------------------------------------

# 查看本地分支,带星号的代表当前所在分支

git branch

 

#切换本地分支到master

git checkout master

 

#拉远端新分支

git checkout --track origin/feature/20160127_888_532656

 

# 将远程git仓库里的指定分支拉取到本地(本地不存在的分支)

git checkout -b 本地分支名 origin/远程分支名

# 如失败,提示:fatal: Cannot update paths and switch to branch 'dev2' at the same time.

# 或提示:fatal: 'origin/dev_auto_purchase' is not a commit and a branch 'dev_auto_purchase' cannot be created from it 

# 如下后重试

git fetch 

 

#废弃分支代码合并过来

git merge release/20160105_new_502299

 

$ git checkout -b  “分支名称”;   新建本地分支

$ git branch;                             查看是否创建成功以及目前在哪个分支

$ git  push -u origin "分支名称";本地推送到远程

 

#本地分支关联远程分支

git branch --set-upstream-to=origin/daily/0.3.9 daily/0.3.9

 

git add --all .

 

git commit -am '备注'

#合并到远程分支

git push origin feature/20160127_888_532656

git pull

 

#恢复某个已修改的文件(撤销未提交的修改):

git checkout xxx.xxx

#还原最近一次提交的修改:

git revert HEAD

#还原指定版本的修改:

git revert commit-id

 

#推分支

git push origin feature/20160214_api_542543

 

git fetch --all && git fetch -ts

 

#版本差异 git diff 版本号码1 版本号码2  src 

git diff 35a0637 aff8b70

 

#备份当前的工作区的内容,并让工作区和上次提交的内容一致

git stash

#从Git栈中读取最近一次保存的内容,恢复工作区的相关内容

git stash pop

#显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复

git stash list

 

#已add但未commit

git rm --cached xxx.xx

git reset HEAD <file>

 

#已commit但未push

git reset --soft HEAD^

 

#本地与远程提交日志差别(未push的日志)

git log xxbranchnamexx ^origin/xxbranchnamexx

 

#其他分支35a0637版本的修改合并过来

git cherry-pick 35a0637

 

#查看版本记录(完整版本号)

git log

 

#基于当前主干/分支的某完整版本号(abc),创建新分支(bug-fix),并切换到新分支

git checkout -b bug-fix abc

#本地分支推到远程分支

git push origin bug-fix:bug-fix  # 本地新分支名:远程新分支名(不存在则创建)

 

# 删除本地分支

git branch -D <BranchName>

-------------------------- 自己整理结束 ------------------------------------------------------


-------------------------- git新仓库开始 ------------------------------------------------------

Git global setup

git config --global user.name "xxx"

git config --global user.email "xxx@163.com"

 

Create a new repository

git clone http://git.keytocaring.com:8082/shop/shop_console.git

cd shop_console

touch README.md

git add README.md

git commit -m "add README"

git push -u origin master

 

Push an existing folder

cd existing_folder

git init

git remote add origin http://git.keytocaring.com:8082/shop/shop_console.git

git add .

git commit -m "Initial commit"

git push -u origin master

 

Push an existing Git repository

cd existing_repo

git remote rename origin old-origin

git remote add origin http://git.keytocaring.com:8082/shop/shop_console.git

git push -u origin --all

git push -u origin --tags

-------------------------- git新仓库结束 ------------------------------------------------------

 

2.Git基本查看命令

1.工作区--暂存区--版本库的目录树浏览

    git ls-tree -l HEAD 浏览版本库中的目录树

    

    git ls-files -s 查看暂存区的目录树

 

3.Git配置文件相关命令

git config -l 显示所有配置

 

git config --global -e

git config --local -e 

git config --system -e 

分别编辑不同的配置文件   以默认的core.editor = emacs or vim 编辑

 

git config --unset --global user.name or user.email

删除 --global or local or system 级别文件下的对应配置

 

4.git 删除相关命令

删除文件

    

git rm 

强制删除文件

     git rm -f

恢复误删文件

     git checkout HEAD -- "FileName"

 

git checkout . (注意有个点) 或 git checkout -- filename

    使用暂存区的全部文件 或 指定文件 替换工作区的文件

    -- 这个命令很危险,因为会清除工作区中未提交的改动

 

git checkout HEAD .(注意有个点) 或 git checkout HEAD -- filename

    使用版本库中当前HEAD只想的分支中的全部文件 或 指定文件 替换 暂存区 和 工作区中的文件

    --这个命令也很危险,因为会清除暂存区和工作区数据

 

5.Git diff命令用法

git diff

    比较当前文件---git diff displays the changes that remain in your working directory and are not staged.

    比较工作区中的文件 和 暂存区中的文件

    

git diff --cached 

    ---git diff --cached shows changes that are staged and will therefore contribute to your next commit.

    with ?  the last commit.

    就是比较暂存区中的文件 和 版本库中的文件

 

git diff --cached 比较Index file和最近一次提交的区别

 

git维护的代码分成三部分,“当前工作目录”&lt;-&gt;“index file”&lt;-&gt;git仓库。git commit会将index file中的改变写到git仓库;git add会将“当前工作目录”的改变写到“index

file”;“commit -a”则会直接将“当前工作目录”的改动同时写到“index file”和“git仓库”。而git diff总会拿git仓库来作为比较对象之一。如果git diff的参数是HEAD,则另一个比较对象就确定为“当前工作目录”;如果参数是–cached,则另一个比较对象就被确定为“index file”。

 

git diff HEAD or another-branch

       比较工作区中的文件 和 指定分支版本库中的文件

 

6.git清理仓库命令

  git rm 清理仓库中的文件

 

如果清理时文件已经stage或者什么其他情况 --cached了。

 

使用git rm -f -A 强制清理

 

git commit -m "some message" 提交清理

 

git clean -fd 清理当前版本库中,没有加入到版本控制中的文件或目录

        -fd   file or directory ??

 

7.Git 日志查看命令

基本命令:

    1.git log显示日志信息

    2.git show "specific rsa key code" ---- 显示特定记录日志信息

    3.git show ---- 显示最近一条记录的日志信息

    4.git show-branch --more="specific number of branch" ---- 以行的形式,准确地展示最近的开发分支。

 

git status命令

    git status -s 显示精简格式输出命令

    -s simple ? or simplify? i guess.

 

8.Git添加忽略

        1.在.git同级目录下,添加.gitignore文件

        2.vim .gitignore

        3.在文件中编辑要忽略的文件,支持通配符。

 

9.解决  Could not resolve hostname github.com About github

        git push -u origin master

 

出现异常:ssh: Could not resolve hostname github.com: Non-recoverable failure in name resolution

         fatal: The remote end hung up unexpectedly

 

解决方法:ssh -T git@github.com

 

再次提交即可

 

关于git原理复杂用法: http://www.infoq.com/cn/news/2011/03/git-adventures-branch-merge/

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics