Git日常管理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git日常管理相关的知识,希望对你有一定的参考价值。

 初始化一个仓库

[[email protected] ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history .bash_logout  .bash_profile  .bashrc .cshrc  .pki  .tcshrc .viminfo
[[email protected] ~]# git init      #初始化(新建)git使用当前目录作为Git仓库,我们只需使它初始化。
Initialized empty Git repository in /root/.git/
[[email protected] ~]# ls -a      #查看,git为隐藏文件
.  ..  anaconda-ks.cfg  .bash_history .bash_logout  .bash_profile  .bashrc .cshrc  .git  .pki .tcshrc  .viminfo
[[email protected] ~]# cd .git/
[[email protected] .git]# ls  
branches config  description  HEAD hooks  info  objects refs
[[email protected] .git]# tree
.
├── branches                    #项目分支信息
├── config                     #Git项目配置信息
├── description                   #Git项目描述信息
├── HEAD                      #指向当前分支的末端
├── hooks                      #默认的hooks脚本,由特定事件触发
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info                       #内有exclude文件:指定git要忽略的文件
│   └── exclude
├── objects                     #Git数据对象:commit、tree、blob、tag
│   ├── info
│   └── pack
└── refs                       #Git引用:指向(远程)分支、标签的指针
    ├── heads
    └── tags
 
9 directories, 13 files

 

设置名字

[[email protected] .git]# git config --global user.name"syaving"

设置邮箱

[[email protected] .git]# git config --global user.email"[email protected]"

设置差异颜色显示

[[email protected] .git]# git config --global color.ui true

设置别名

[[email protected] .git]# git config --global alias.it init
[[email protected] .git]# git config --global alias.olog"log --pretty=oneline"
[[email protected] .git]# git olog
e435fe88da4d214f5256130e720b1faf21f0f011 add readme
2525062715ac09c1da5d5e5b8d2294200d33e791 add readme

参数选项:

  • --global:修改全局配置文件,去配置~/.gitconfig

用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。

  • --system:修改所有用户的配置文件:/etc/gitconfig

系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。

  • 无参数:修改本仓库配置文件: .git/config

这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。

设置自动补全功能

[[email protected] ~]# pwd
/root
[[email protected] ~]# rz ## 上传git-bash-completion-master.zip
 
[[email protected] ~]# ls
anaconda-ks.cfg git  git-bash-completion-master.zip
[[email protected] ~]# unzip git-bash-completion-master.zip  ##解压git-bash-completion-master.zip
Archive: git-bash-completion-master.zip
d1d25024c4a68f7724a1876598664f13c01aac19
   creating:git-bash-completion-master/
  inflating:git-bash-completion-master/README.markdown 
  inflating:git-bash-completion-master/git-completion.bash 
[[email protected] ~]# ls
anaconda-ks.cfg git  git-bash-completion-master  git-bash-completion-master.zip
[[email protected] ~]# cd git
[[email protected] git]# cd ..
[[email protected] ~]# cd git-bash-completion-master/
[[email protected] git-bash-completion-master]# ls
git-completion.bash README.markdown
[[email protected] git-bash-completion-master]# mvgit-completion.bash ..
[[email protected] git-bash-completion-master]# ls
README.markdown
[[email protected] git-bash-completion-master]# cd ..
[[email protected] ~]# ls
anaconda-ks.cfg git-bash-completion-master      git-completion.bash
git             git-bash-completion-master.zip
[[email protected] ~]# rm -rf git-bash-completion-master*
[[email protected] ~]# ls
anaconda-ks.cfg git  git-completion.bash
[[email protected] ~]# source ~/git-completion.bash  #
[[email protected] ~]# echo source ~/git-completion.bash>> ~/.bashrc  ##加入环境变量
[[email protected] ~]# source ~/.bashrc              #使环境变量生效

 

文本编辑器

设置Git默认使用的文本编辑器, 一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Emacs 的话,可以重新设置::

 

[[email protected] .git]# git config --global core.editor emacs

差异分析工具

还有一个比较常用的是,在解决合并冲突时使用哪种差异分析工具。比如要改用 vimdiff 的话:

[[email protected] .git]# git config --global merge.toolvimdiff

Git 可以理解 kdiff3tkdiffmeldxxdiffemergevimdiffgvimdiffecmerge,和 opendiff 等合并工具的输出信息。

 

查看配置信息

要检查已有的配置信息,可以使用 git config --list 命令:

[[email protected] .git]# git config --list
user.name=syaving
[email protected]
color.ui=true
alias.it=init
alias.olog=log --pretty=oneline
core.editor=emacs
merge.tool=vimdiff
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

 

有时候会看到重复的变量名,那就说明它们来自不同的配置文件(比如 /etc/gitconfig ~/.gitconfig),不过最终 Git 实际采用的是最后一个。

这些配置我们也可以在 ~/.gitconfig /etc/gitconfig 看到,如下所示:

vim ~/.gitconfig

显示内容如下所示:

[http]
    postBuffer = 2M
[user]
    name = syaving
    email = [email protected]


也可以直接查阅某个环境变量的设定,只要把特定的名字跟在后面即可,像这样:

# git config user.name
"syaving"


!!!以上一段,为网上收集信息,没看明白。。



本文出自 “宋某人c” 博客,请务必保留此出处http://syaving.blog.51cto.com/5614476/1909240

以上是关于Git日常管理的主要内容,如果未能解决你的问题,请参考以下文章

GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段

GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段

GIT科普系列2:git代码检出与日常维护

日常开发使用GIT命令

巧用vscode编辑器 ,利用vscode可视化git跟踪git历史记录,简单实现git日常操作

巧用vscode编辑器 ,利用vscode可视化git跟踪git历史记录,简单实现git日常操作