Git 日常使用

来源:互联网 发布:linux导出数据库命令 编辑:程序博客网 时间:2024/06/14 10:19

转自:    http://blog.csdn.net/congspark/article/details/53037896

初识git, 只是了解一个大概, 和一些基本概念, 比如暂存, 提交,分支等. 总结一下常用的指令, 足够应付日常的工作:



1. 本地代码提交

察看工作空间的代码状态:

git status
  • 1
  • 1

终端会显示类似的信息:

On branch master        Changes to be committed:      (use "git reset HEAD <file> ..." to un-stage)       new file:   src/main/res/layout/letv_live_time_vs_content.xml      Changes not staged for commit:      (use "git add <file> ..." to update what will be committed)      (use "git checkout -- <file> ..." to discard changes in working directory)          modified:   src/main/res/layout/letv_live_time_live_item.xml          modified:   src/main/res/values/colors.xml          modified:   src/main/res/values/dimens.xml          modified:   src/main/res/values/strings.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

    根据提示信息可知: 目前正处于分支master 上, 有一个新增的文件, 已暂存准备提交; 有四个修改过的文件, 还未暂存.

接下来暂存文件, 多种方式:

git add src/file1  暂存src目录下的file1文件git add src/       暂存src目录下所有的文件git add .          暂存当前目录下的所有文件
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

    这里我们使用最多的是第二条命令, 暂存一个目录下的所有文件. 第一种单个暂存的方式过于繁琐, 第三种全部添加可能会暂存一些不需要的文件.

在终端执行:

git add src/
  • 1
  • 1

然后再察看工作空间中的状态信息:

git status     On branch master     Changes to be committed:        (use "git reset HEAD <file>..." to unstage)      modified:   src/main/res/layout/letv_live_time_live_item.xml      new file:   src/main/res/layout/letv_live_time_vs_content.xml      modified:   src/main/res/values/colors.xml      modified:   src/main/res/values/dimens.xml      modified:   src/main/res/values/strings.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

终端提示信息: 目前正处在master 分支, 
四个修改的文件, 一个新增的文件已暂存, 准备提交.

然后是提交文件, 也有多种方式, 这里只用最保险的两种, 完全够用:\

git commit -m "commit note"    创建新的提交, 注释为"commit note"git commit --amend     追加上一次提交, 会启动vim 编辑器, 编辑注释. 注释为空放弃提交.
  • 1
  • 2
  • 1
  • 2

在终端执行: Git commit -m "modify setting page layout" 得到类似的提示信息:

[master dcd6316] modify settings page layout5 files changed, 24 insertions(+), 24 deletions(-)
  • 1
  • 2
  • 1
  • 2

    在master 分支上提交, commit-id: dcd6316 5 个文件发生改变, 新增了24行代码, 删除24行代码, 本地提交成功.



2. 从服务端的代码库中同步代码

    代码在本地提交后, 工作空间为clean 状态, 便于一些后续的操作, 就可以拉取服务端的代码了.

git fetch origin dev_letv_global // 同步远程分支 dev_letv_global 的代码, 到本地的一个临时分支.git fetch origin dev_letv_global:temp // 同步远程分支 dev_letv_global 的代码到本地的temp 分支.
  • 1
  • 2
  • 1
  • 2

其中: 
origin 为默认的远程仓库的名称(可自己命名) 
dev_letv_global 为远程仓库中的某个分支(管理员命名)

终端控制台会出现如下类似的信息:

From ssh://legit.letv.cn:29418/terminal/tv-letv-global        * branch            dev_letv_global -> FETCH_HEAD          1fe4874..1948cbe  dev_letv_global -> origin/dev_letv_global
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

执行:

git rebase FEATCH_HEAD 
  • 1
  • 1

或:

git rebase temp
  • 1
  • 1

将代码服务器的代码与本地代码 rebase 下.

注意: 在执行此操作之前, 要先提交本地代码, 确保工作空间 
为clean 状态, 否则无法操作, git 会给出相应的提示.

rebase 的过程中git 会返回相关信息:

Current branch master is up to date.
  • 1
  • 1

说明该分支所处的代码已经是最新, 
或者是:

First, rewinding head to replay your work on top of it...   Applying: 1.add loading view to Live and Category page.
  • 1
  • 2
  • 1
  • 2

    说明rebase过程中没有任何冲突(运气不错), master 分支上的代码已经是最新, 可向服务端提交. 但多数情况下git会给出这样的提示:

localhost:letv congguangzi$ git rebase FETCH_HEADFirst, rewinding head to replay your work on top of it...Applying: 1. modify the category layoutUsing index info to reconstruct a base tree...M   src/main/res/values/dimens.xmlFalling back to patching base and 3-way merge...Auto-merging src/main/res/values/dimens.xmlCONFLICT (content): Merge conflict in src/main/res/values/dimens.xmlRecorded preimage for 'src/main/res/values/dimens.xml'error: Failed to merge in the changes.Patch failed at 0001 1. modify the category layoutThe copy of the patch that failed is found in: .git/rebase-apply/patchWhen you have resolved this problem, run "git rebase --continue".If you prefer to skip this patch, run "git rebase --skip" instead.To check out the original branch and stop rebasing, run "git rebase --abort".
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

    提示在rebase 的过程中出现冲突,这里 使用Android studio, (其他亦可) 处理冲突. Android Studio 默认集成解决冲突的插件, 而且功能强大, 推荐使用. 
    冲突处理完成后, 查看工作空间的状态:

rebase in progress; onto 72abc7dYou are currently rebasing branch 'master' on '72abc7d'.  (all conflicts fixed: run "git rebase --continue")Changes to be committed:  (use "git reset HEAD <file>..." to unstage)    modified:   src/main/java/com/letv/tv/category/presenter/OtherTitlePresenter.java    new file:   src/main/res/drawable/letv_category_other_title_bg.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

    提示在rebase 的过程中, 已经处理完毕了所有的冲突, 可以继续rebase, 根据提示执行: git rebase --continue 完成rebase 过程.

localhost:letv congguangzi$ git rebase --continueApplying: 1. modify the category layoutRecorded resolution for 'src/main/res/values/dimens.xml'.
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3


3. 将本地代码, 提交到服务器

同步了代码之后, 只要确保有足够的权限, 就可以像服务端提交代码了:

git push origin master:refs/for/dev_letv_global
  • 1
  • 1

其中: 
origin 远程仓库名称 
refs/for/dev_letv_global > 在dev_letv_global分支下, > refs/for表示待管理员审核. 
提交成功, git 会给出如下提示:

localhost:letv congguangzi$ git push origin master:refs/for/dev_letv_global     Counting objects: 19, done.     Delta compression using up to 8 threads.     Compressing objects: 100% (16/16), done.     Writing objects: 100% (19/19), 1.64 KiB | 0 bytes/s, done.     Total 19 (delta 13), reused 0 (delta 0)     remote: Resolving deltas: 100% (13/13)     remote: Processing changes: new: 1, refs: 1, done         remote:      remote: New Changes:     remote:   http://legit.letv.cn/13439     remote:      To ssh://legit.letv.cn:29418/terminal/tv-letv-global     * [new branch]      master -> refs/for/dev_letv_global
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

提示中包括了提交进度, 远程分支的一些信息, 待管理员审核. 
http://legit.letv.cn/13439 是此次提交的url, 在浏览器中打开此url, 添加review 代码的管理员, 或是执行其他的操作. 
在审核的过程中, 是可以修改代码, 并重新提交的审核的. 
代码在本地修改暂存之后, 执行:

localhost:letv congguangzi$ git commit --amend[master 9d0b2a3] 1. modify the category layout 2. add live content layoutDate: Thu Jul 21 11:25:05 2016 +08007 files changed, 93 insertions(+), 1 deletion(-)create mode 100644 src/main/res/drawable/letv_category_other_title_bg.xmlcreate mode 100644 src/main/res/layout/letv_live_time_vs_content.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

    --amend指令追加提交, 会启动vim 编辑器, 编辑提交注释, 有关于vim 编辑器的使用不再赘述, 自行谷歌度娘. 编辑完成后, 保存退出, 得到如上图相应的提示信息, 与普通的commit 指令类似, 只是增加了追加提交的文件. 
    之后的操作就与之前大同小异了, 从服务端fetch 代码, rebase 处理冲突, 重新push 提交到服务端. 
    审核通过后, 此部分代码入库, 就不可以再提交更新了, 如果要修改, 只能在下一次提交中修改.



4.git stash 应用

开发过程中经常会遇到这样的情况: 
    完成了一次提交之后, 下一个小需求已经开发到一半, 这时候老大跟你说, 上个提交有点问题, 需要修改. 可是新的功能已经写了一半, 不能提交, 如何? 
    其实这里有两种方法, branch 和 stash, 这里说一下比较简单的 stash 方式

git stash
  • 1
  • 1

    会提示已经将代码储存起来, 工作空间为clean 状态, 此时从服务端fetch 一下代码, 然后修改提交完成后, 执行:

git stash pop
  • 1
  • 1

    在这个过程中, 可能会与原来的代码有冲突, 处理冲突的过程与从服务端fetch 代码后处理冲突的方式相同, 冲突处理完成后, 就可继续之前的开发任务.



5.checkout 应用

    假设你正在修改 SettingsFragment.Java 这个类的代码, 或者是 SettingsLayout.xml这个布局文件, 此时,你突然发现自己的修改有误, 需要还原文件, 第一反应就是ctrl + z 撤销, 修改, 不过作为一个程序员, 这样做确实有点太 low, 而且如果修改的太多, 也不太可能仅仅使用撤销操作使文件恢复到原来的状态. 此时git 提供的checkout 指令就派上用场了. 先查看一下工作空间的状态:

git status     On branch master     Changes to be committed:       (use "git reset HEAD <file>..." to unstage)        modified:   src/main/res/layout/letv_settings_item.xml     Changes not staged for commit:       (use "git add <file>..." to update what will be committed)       (use "git checkout -- <file>..." to discard changes in working directory)        modified:   src/main/java/com/letv/tv/db/LetvDBHelper.java
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

    这里修改了两个文件, 暂存了其中一个. git 已经给出了相当详细的提示, 我们按照提示,先恢复未暂存的文件, 执行 git checkout src/main/java/com/letv/tv/db/LetvDBHelper.java , LetvDBHelper.java 就会恢复到上一次提交, 也就是修改之前的状态.

    对于已经暂存的文件, 先执行 git reset HEAD src/main/res/layout/letv_settings_item.xml 取消文件暂存, 然后在执行git checkout src/main/res/layout/letv_settings_item.xml, letv_settings_item.xml 布局文件就会恢复到修改之前的状态. 再次查看一下工作空间:

git statusOn branch masternothing to commit, working directory clean
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在Adnroid Studio 中查看下代码, 已经恢复到原来的状态.nice.

0 0
原创粉丝点击