git使用笔记

https://blog.phpgao.com/note_for_git.html


GIT虽然概念比较难理解,但不得不说他是一款开发利器。

老高总结出了一些GIT中很常见的操作命令,分享给大家。但由于GIT命令繁多,所以我将分为基础和进阶两部分。

I. 基础篇:

帮助

git help # 获取帮助,内容如下usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches   checkout   Checkout a branch or paths to the working tree   clone      Clone a repository into a new directory   commit     Record changes to the repository   diff       Show changes between commits, commit and working tree, etc   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one   log        Show commit logs   merge      Join two or more development histories together   mv         Move or rename a file, a directory, or a symlink   pull       Fetch from and merge with another repository or a local branch   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status   tag        Create, list, delete or verify a tag object signed with GPG

配置git

# 查看配置git config -l/--list# 以下是可能出现的配置core.symlinks=falsecore.autocrlf=truecolor.diff=auto
color.status=auto
color.branch=auto
color.interactive=truepack.packsizelimit=2g
help.format=html
...
...# 配置全局信息git config --global user.name=phpgao# 配置局部信息git config --system [email protected]# 查看某一个配置信息git config user.email

初始化仓库

git init # 在当前目录初始化一个git仓库git init --bare # 在当前目录初始化一个git裸仓库

查看

git status # 显示工作流中的状态git diff # 显示工作目录(Working tree)和暂存区域快照(index)之间的差异git diff --stat # 简报git diff --cached # 显示已经暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异git diff --staged # 下一次commit时会提交到HEAD的内容(不带-a情况下)git diff dev # 比较当前目录和dev分支 git diff HEAD # 工作目录和HEAD的差别git diff HEAD^ HEAD # 比较上次和上上次提交的不同git diff dev master # 比较两个分支最新提交git diff dev..master # 同上git diff dev...master # 比较从分支开始时至今所有的修改git log --pretty=oneline # 显示日志### 美化格式一git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short### 美化格式二git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

II. 增删提

先读懂这个图

提交关系图

git add # 添加工作区修改的文件提交至Stage(index)git commit -m "comment" # 将Stage(index)中的文件提交至本地库中(commit),并添加注释git commit -am "comment" # 省略了add步骤,直接提交Working Directory和Stage(index)中的内容git rm <文件名> # 删除库中的文件git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改git reset -- . # 从暂存区恢复到工作文件

III. 分支与合并

git branch <分支名> <老分支名># 根据分支创建新分支git branch -r # 查看远程分支git branch -v # 查看各分支最近的提交git branch -d <分支名> # 删除一个分支git br -D <分支名> # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)git branch -m <分支名> <新分支名> # 重命名一个分支git checkout <分支名> # 切换至某分支git checkout -b <分支名> # 创建并切换至新分支git merge dev # 将当前分支与dev分支合并git merge dev --no-ff # 不使用Fast-Foward合并,为了保持项目的清晰的轨迹,推荐合并时使用此选项

这里前者到一个概念叫分支策略,可以参考这篇文章Git分支管理策略

IV. 远程操作

clone

git clone /xx/xxx/xxx.git # 克隆某个项目git支持很多协议,如ssh、git、https等。

remote

git remote -v # 查看远程库git remote add origin xxxx.git # 添加一个远程主机git remote rm # 删除一个远程主机

fetch

git fetch <远程主机名> # 取回所有信息git fetch <远程主机名> <分支名> # 只取回某分支git branch -a # 查看所有分支git branch -r # 查看远程分支

pull

git push

push


V. jetbrains系列软件问题

GIT Remotes "Can't push, because no remotes are defined"

软件中没有添加remote的功能,所以如果你要新加入一个远程库就需要在terminal中使用以下命令

git remote add origin "path to .git"

fatal: No existing author found with 'john doe'

先使用git config -l查看配置,得到name和email如下

user.name=aaa
[email protected]

软件配置里填入

aaa [email protected]

这样软件转化的命令就变成

git commit --author="aaa <[email protected]>" -m "Note"

参考:

https://ruby-china.org/topics/939


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章