Git常用进阶操作之一

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

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

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

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

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

[email protected]: ~/git_repo$ git config --global user.name "philleer"
[email protected]: ~/git_repo$ git config --global user.email "[email protected]"

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

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

[email protected]:~/git_repo$ git config user.name "philleer"
[email protected]:~/git_repo$ git config user.email "[email protected]"

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

[email protected]:~/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 = [email protected]163.com

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

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

[email protected]: ~/git_repo $ git config --list
user.email[email protected]163.com
user.name=philleer
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=philleer
user.email[email protected]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 ] 状态,也就是游离状态。

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

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

[email protected]:~/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就处于我们所说的游离状态

[email protected]:~/git_repo$ git branch
* (HEAD detached at 44066c6)
  detach_test
  feature_x
  master

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

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

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

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

[email protected]:~/git_repo$ git branch tmp 2959523

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

[email protected]:~/git_repo$ git branch
* (HEAD detached from 44066c6)
  detach_test
  feature_x
  master
  tmp

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

[email protected]:~/git_repo$ git checkout detach_test
Previous HEAD position was 2959523... detached state
Switched to branch ‘detach_test‘

[email protected]:~/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.
[email protected]:~/git_repo$ vim ../src/CacheFunction.cc

[email protected]:~/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

[email protected]:~/git_repo$ vim ../src/CacheFunction.h 

[email protected]:~/git_repo$ git add ../src/CacheFunction.h

[email protected]:~/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

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

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

[email protected]:~/git_repo$ git branch
* detach_test
  feature_x
  master

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

未完待续

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

时间: 2024-07-30 23:16:43

Git常用进阶操作之一的相关文章

Git常用的操作指令

修改最后一次提交 有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了.想要撤消刚才的提交操作,可以使用--amend 选项重新提交: 1 $ git commit --amend -m"修改 提交 说明" 此命令将使用当前的暂存区域快照提交.如果刚才提交完没有作任何改动,直接运行此命令的话,相当于有机会 重新编辑提交说明,但将要提交的文件快照和之前的一样. 启动文本编辑器后,会看到上次提交时的说明,编辑它确认没问题后保存退出,就会使用新的提交说明覆盖刚才失误的提交. 如果

git常用的操作记录一下

代码仓库 创建仓库 进入需要创建代码库的文件夹-----cd 文件路径 创建初始化仓库-----git init 拉取远程仓库到本地-----git clone 仓库路径 添加文件到仓库 添加文件到暂存区 添加单个文件-----git add 文件名 添加所有文件-----git add . .gitignore文件中指定的文件会被忽略 空目录也会被忽略 提交到本地仓库 git commit -----填写mssage 不建议使用git commit -m 'message' 提交遵循commi

Git常用的操作

1.git使用的常规操作 git pull-->编辑-->git add-->git commit-->git push 用git add把文件添加进去,实际上就是把文件修改添加到暂存区: 用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支: 用git push 提交到远程的仓库. 其实就是工作区--git add-->暂存区--git commit-->HEAD 2.比较本地仓库与远程仓库不同: git status 只能查看未push提交的次

git常用命令---操作本地仓库

命令中:<>表示可选填写    []表示选项必须填写   ()表示可以等价替换为()里内容 1.暂存相关命令(前三个命令最常用) git add [file]      提交某文件到暂存区([file]可以是文件名或目录名) git add -A   <path>       提交所有变化到暂存区,省略<path>表示.(即当前目录) git add  .         提交新文件和被修改文件(不包括被删除文件)到暂存区 git add -u  <path>

Git 常用场景操作

git init 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹. git clone 获取一个url对应的远程Git repo, 创建一个local copy. 一般的格式是git clone [url]. clone下来的repo会以url最后一个斜线后面的名称命名,创建一个文件夹,如果想要指定特定的名称,可以git clone [url] newname指定. git status 查询repo的状态. git

git常用命令操作

目前市场主流的版本控制系统主要分为:集中式版本控制系统与分布式版本控制系统.集中式版本控制目前主流的:SVN分布式版本控制系统目前主流的:Git 两者之间的区别: 集中式有中央服务器,开发人员需要从中央服务器获得最新版本的项目然后在本地开发,开发完推送到中央服务器中,因此无法脱离服务器. 分布式没有中央服务器,开发人员本地都有local repository 集中式必须要联网才能工作,而且对网络的依赖性较强,如果推送的文件比较大而且网络状态欠佳,则提交文件的速度会受到很大的限制. 分布式在没有网

git 常用命令操作

目录 一.用户和邮箱 用户和邮箱的作用 查看用户名和邮箱地址 修改用户名和邮箱地址 用户名和邮箱地址的作用 用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. github的contributions统计就是按邮箱来统计的. 查看用户名和邮箱地址 $ git config user.name $ git config user.email 修改用户名和邮箱地址: $ git config --global user.name "usern

git 常用操作集锦

创建仓库 新建普通仓库: [email protected]:~/workspace/git$ git init Reinitialized existing Git repository in /home/jxdong/workspace/git/.git/ 新建 bare 仓库: [email protected]:~/workspace/git.git$ git init --bare Initialized empty Git repository in /home/jxdong/wor

git报错:&#39;fatal:remote origin already exists&#39;怎么处理?附上git常用操作以及说明。

git添加远程库的时候有可能出现如下的错误, 怎么解决? 只要两步: 1.先删除 $ git remote rm origin 2.再次执行添加就可以了. ----------------------------------------------git常用操作------------------------------------------------ 说明,以下整理来自廖雪峰大神的<git教程>. 各位童鞋要下载git但是网速不给力的,可以从这里下载:https://pan.baidu.