git使用

git 工作栈

保存工作区的内容通过栈来管理。

分支

  1. 查看远程分支: git branch -a获取。
  2. 查看tag :git tag dev_20160324_04
  3. 创建分支 git branch -b
  4. 更新代码 git pull 相当于svn 的update。
  5. 提交代码 git push 把本地仓库提交到远程仓库。
  6. git clone 拷贝一个新的分支。
  7. git checkout :拉取新的代码覆盖本地代码。 (会在本地切换路径)
  8. 还原代码: git revert HEAD,git revert commit-id
  9. 如果有untracted file 使用 git clean -f
  10. 查看git 版本库的状态: ls .git/refs ls .git/refs/tags等等。
  11. 有冲突:git中有冲突 .swp文件,git add后就可以解决冲突。

使用场景

修改了某个文件提交到远程分支 dev_20160324

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
$ git add ./

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
$ git commit -m '测试 日志级别修改成error'
[dev_20160324 dceab09] 测试 日志级别修改成error
 2 files changed, 7 insertions(+), 4 deletions(-)
xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)

$ git push origin dev_20160324 ##分支名字一定要写对
Counting objects: 15, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (15/15), 8.61 KiB | 0 bytes/s, done.
Total 15 (delta 8), reused 0 (delta 0)
To git@gitlab.lujs.cn:asset/p2p-app
   64e5a80..dceab09  dev_20160324 -> dev_20160324

本地有修改文件的情况下切换分支

1.先通过stash 保存工作区栈。
2.利用checkout 来切换分支(没有switch命令)

#查看git状态
$ git status  
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    batch/push/LUFAX-ANSHUO2014021714241020009000000681.txt
        modified:   build.gradle
        modified:   release/20160315.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        db/batch/

no changes added to commit (use "git add" and/or "git commit -a")

#由于工作区有改动需要保存到缓冲区里
xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (master)
$ git stash
Saved working directory and index state WIP on master: 5c11336 add release 20160315
HEAD is now at 5c11336 add release 20160315

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (master)
#查看有哪些缓冲区
$ git stash list
**stash@{0}: WIP on master: 5c11336 add release 20160315**

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (master)
#重新拉取代码(相当于切换分支)
$ git checkout dev_20160609
Branch dev_20160609 set up to track remote branch dev_20160609 from origin.
Switched to a new branch 'dev_20160609'

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (dev_20160609)
$ git status
On branch dev_20160609
Your branch is up-to-date with 'origin/dev_20160609'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        db/batch/

nothing added to commit but untracked files present (use "git add" to track)

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (dev_20160609)

本地有修改文件的情况下更新代码

$ git  pull
remote: Counting objects: 28, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 28 (delta 16), reused 0 (delta 0)
Unpacking objects: 100% (28/28), done.
From gitlab.lujs.cn:asset/anshuo-app
   e0ff732..b22f5bf  dev_20160324 -> origin/dev_20160324
Updating 82c5a6d..4ae4565
error: Your local changes to the following files would be overwritten by merge:
        build.gradle
##有冲突        src/main/java/com/lufax/anshuo/anshuoInterface/loaneeCredentials/service/ASLoaneeCredentialsService.java
        src/main/resources/anshuo-app-env.properties
        src/main/webapp/WEB-INF/applicationContext.xml
Please, commit your changes or stash them before you can merge.
Aborting

$ git stash
Saved working directory and index state WIP on dev_20160609: 82c5a6d add filter to exclude LC product for message trx.settled
HEAD is now at 82c5a6d add filter to exclude LC product for message trx.settled

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/anshuo-app (dev_20160609)
$ git stash list
stash@{0}: WIP on dev_20160609: 82c5a6d add filter to exclude LC product for message trx.settled
stash@{1}: WIP on master: 5c11336 add release 20160315

##把工作区的内容恢复
$ git stash pop
Auto-merging src/main/webapp/WEB-INF/applicationContext.xml
Auto-merging src/main/resources/anshuo-app-env.properties
Auto-merging src/main/java/com/lufax/anshuo/anshuoInterface/loaneeCredentials/service/ASLoaneeCredentialsService.java
CONFLICT (content): Merge conflict in src/main/java/com/lufax/anshuo/anshuoInterface/loaneeCredentials/service/ASLoaneeCredentialsService.java
Auto-merging build.gradle
CONFLICT (content): Merge conflict in build.gradle

分支中修改了代码,想切换到另外一个分支使用

##先保存临时分区
$ git stash
Saved working directory and index state WIP on dev_20160324: b14c198 提交修改
HEAD is now at b14c198 提交修改

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
$ git stash list | grep b14c198
stash@{0}: WIP on dev_20160324: b14c198 提交修改

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
#下载 dev_20160329分支
$ git checkout  dev_20160329
Branch dev_20160329 set up to track remote branch dev_20160329 from origin.
Switched to a new branch 'dev_20160329'

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160329)
$ git status
On branch dev_20160329
Your branch is up-to-date with 'origin/dev_20160329'.
nothing to commit, working directory clean

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160329)
$ git stash pop #恢复临时工作区->这样在新的分支上就可以做了。
Auto-merging src/test/java/com/pingan/p2p/Test2.java
CONFLICT (content): Merge conflict in src/test/java/com/pingan/p2p/Test2.java
Auto-merging src/main/java/com/pingan/p2p/biz/transfer/resources/TransferRequestsResource.java
CONFLICT (content): Merge conflict in src/main/java/com/pingan/p2p/biz/transfer/resources/TransferRequestsResource.java
Auto-merging src/main/java/com/pingan/p2p/biz/pledge/resources/PledgeRequestsResource.java
CONFLICT (content): Merge conflict in src/main/java/com/pingan/p2p/biz/pledge/resources/PledgeRequestsResource.java

拉取tag分支,把tag分支push到远程仓库

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
$ git tag dev_20160324_07

xujianfeng981@LDEV-D061 MINGW64 /d/workspace/lufax/p2p-app (dev_20160324)
$ git push  origin dev_20160324_07
Total 0 (delta 0), reused 0 (delta 0)
To git@gitlab.lujs.cn:asset/p2p-app
 * [new tag]         dev_20160324_07 -> dev_20160324_07
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页