码迷,mamicode.com
首页 > 其他好文 > 详细

Git常用进阶操作之一

时间:2019-09-10 23:42:50      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:clu   src   opencv   enc   内容   tac   就是   config   UNC   

提起Git,经常做项目的我们都不陌生,我们常用的功能有哪些呢?

这里按个人使用情况简单总结一下。

像新建远程仓库、新建分支这些就不说了,不熟的同学可以翻看我前面写的git基本操作

  • 1.首先提一下为每个项目建立不同的用户名和邮箱

通常我们直接在命令行可以查看和设置user.name和user.email

cv@cv: ~/git_repo$ git config --global user.name "philleer"
cv@cv: ~/git_repo$ git config --global user.email "phillee2016@163.com"

这是一种全局的git用户名和邮箱设置,那如果我们想要为当前项目指定用户名和邮箱而同时不会影响其他项目的这些设置,该怎么做呢?

很简单,直接在当前本地仓库目录下进行设置。一种是命令行直接修改(推荐)

cv@cv:~/git_repo$ git config user.name "philleer"
cv@cv:~/git_repo$ git config user.email "phillee2016@163.com"

另一种是找到当前目录下的.git文件夹进入,找到config文件

cv@cv:~/git_repo$ tree -L 1 .git
.git
├── branches
├── COMMIT_EDITMSG
├── config    <--- 就是这个
├── description
├── HEAD
├── hooks
├── index
├── info
├── logs
├── objects
└── refs

6 directories, 5 files

使用 VIM/gedit 等编辑器打开,在末尾加入三行

[user]
    name = philleer
    email = phillee2016@163.com

其实前面的方式也是在该文件中添加此项内容,只不过是通过命令形式直接进行的。

完成后可以使用 git config --list 查看

cv@cv: ~/git_repo $ git config --list
user.email=phillee2016@163.com
user.name=philleer
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=philleer
user.email=phillee2016@163.com
remote.origin.url=https://github.com/philleer/git_repo_for_trail.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

命令的输出中包含全局配置+当前项目配置,上面的是全局配置,使用时优先使用当前项目配置。

  • 2. git处于游离分支时如何处理才能保存修改?

一般情况下我们使用命令 git checkout <branch_name> 进行分支之间的切换,这是HEAD就会移动指向指定分支。

但是,如果我们使用的是 git checkout <commit_id> 也就是说无意间切换到了某次提交所在的状态上,而我们可能又在此状态进行了一系列修改,这时我们会发现HEAD处于一种 [ detached ] 状态,也就是游离状态。

如果此时我们对修改进行了提交,系统默认会新开一个匿名分支,也就是说我们的提交是无法可见保存的,一旦切换到其他分支,在游离状态进行的提交就不可追溯了。

此时查看本地仓库状态,结果如下所示,

cv@cv:~/git_repo$ git status 
HEAD detached at 44066c6
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:   ../CMakeLists.txt
    modified:   ../src/CacheFunction.cc
    modified:   ../src/CacheFunction.h

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

查看当前所在分支,由于目前处于之前本地提交的一个commit上,并未push到远程仓库,因此HEAD就处于我们所说的游离状态

cv@cv:~/git_repo$ git branch 
* (HEAD detached at 44066c6)
  detach_test
  feature_x
  master

此时先把修改的内容进行提交

cv@cv:~/git_repo$ git add -A
cv@cv:~/git_repo$ git commit -m "detached state"
[detached HEAD 2959523] detached state
 3 files changed, 14 insertions(+)

然后以提交后新的<commit_id>创建临时分支,也就是说通过此操作将游离状态的修改保存到一个新建的临时分支,这样就不用再担心之后分支切换导致无法追溯游离状态的问题

cv@cv:~/git_repo$ git reflog
2959523 HEAD@{0}: commit: detached state
44066c6 HEAD@{1}: checkout: moving from detach_test to 44066c6
0ca3a35 HEAD@{2}: commit: test the opencv link

cv@cv:~/git_repo$ git branch tmp 2959523

这时再查看本地仓库状态可以发现,已经成功新建了分支tmp,只是暂时还没有切换过去,目前仍处于游离状态

cv@cv:~/git_repo$ git branch
* (HEAD detached from 44066c6)
  detach_test
  feature_x
  master
  tmp

最后切换分支,将tmp分支merge到指定分支,然后删除临时分支tmp,over!

cv@cv:~/git_repo$ git checkout detach_test
Previous HEAD position was 2959523... detached state
Switched to branch detach_test

cv@cv:~/git_repo$ git merge tmp 
Auto-merging src/CacheFunction.h
CONFLICT (content): Merge conflict in src/CacheFunction.h
Auto-merging src/CacheFunction.cc
Automatic merge failed; fix conflicts and then commit the result.
cv@cv:~/git_repo$ vim ../src/CacheFunction.cc

cv@cv:~/git_repo$ git status 
On branch detach_test
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:
    modified:   ../src/CacheFunction.cc

Unmerged paths:
  (use "git add <file>..." to mark resolution)
    both modified:   ../src/CacheFunction.h

cv@cv:~/git_repo$ vim ../src/CacheFunction.h 

cv@cv:~/git_repo$ git add ../src/CacheFunction.h

cv@cv:~/git_repo$ git status 
On branch detach_test
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
    modified:   ../src/CacheFunction.cc
    modified:   ../src/CacheFunction.h

cv@cv:~/git_repo$ git commit -m "fix the merge conflict"
[detach_test 13c6720] fix the merge conflict

cv@cv:~/git_repo$ git status 
On branch detach_test
nothing to commit, working directory clean
cv@cv:~/git_repo$ git branch -d tmp 
Deleted branch tmp (was 2959523).

cv@cv:~/git_repo$ git branch 
* detach_test
  feature_x
  master

至此,处理完毕,HEAD指向正常分支。

未完待续

Git常用进阶操作之一

标签:clu   src   opencv   enc   内容   tac   就是   config   UNC   

原文地址:https://www.cnblogs.com/phillee/p/11497726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!