标签 git 下的文章

你是不是已经厌倦了每次git push的时候每次都要输入用户名密码,使用下面的方法可以让你使用ssh协议通过密钥验证的方式让你得到解脱。

有两种修改方法

不过再实施前,请先准备好自己的密钥

ssh-keygen -t rsa -C "your_name"

然后登录https://github.com/settings/ssh,添加当前计算机的~/.ssh/id_rsa.pub公钥内容到github。

之后我们使用ssh [email protected]验证是否添加成功,如果返回以下内容,即代表添加成功!

Hi phpgao! You've successfully authenticated, but GitHub does not provide shell access.

下一步就是让我们的git使用公钥验证。

clone

保存你的最后一次修改并提交。

删除项目

使用下面的命令clone项目

# 采用ssh的方式克隆项目
# someaccount/someproject.git 中 some account为github用户名/someproject为仓库名

git clone [email protected]:phpgao/BaiduSubmit.git

修改https

git remote set-url origin [email protected]:someaccount/someproject.git

顺便提一下,老高的git push总是报warning: push.default is unset错误,今天终于知道为啥了。原来是版本兼容性的原因,低版本的git push如果不指定分支名,就会全部推送,而新版只会推送当前分支。

解决的办法也很简单,我们只需要明确指定应该推送方式即可,至于选择哪种方式,It's up to you.


# 全部推送
git config --global push.default matching

# 部分推送
git config --global push.default simple

转自 http://blog.csdn.net/hudashi/article/details/7664457

Git中从远程的分支获取最新的版本到本地有这样2个命令:

1.git fetch:相当于是从远程获取最新版本到本地,不会自动merge

git fetch origin master
git log -p master..origin/master
git merge origin/master

以上命令的含义:

首先从远程的origin的master主分支下载最新的版本到origin/master分支上 然后比较本地的master分支和origin/master分支的差别 最后进行合并

上述过程其实可以用以下更清晰的方式来进行:

git fetch origin master : tmp
git diff tmp 
git merge tmp

从远程获取最新的版本到本地的test分支上 之后再进行比较合并

2.git pull:相当于是从远程获取最新版本并merge到本地

git pull origin master

述命令其实相当于git fetch 和 git merge 在实际使用中,git fetch更安全一些 因为在merge前,我们可以查看更新情况,然后再决定是否合并

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

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

基础篇:

帮助

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=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.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 

增删提

先读懂这个图

提交关系图

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 -- . # 从暂存区恢复到工作文件

分支与合并

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分支管理策略

远程操作

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


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 user.email=[email protected]

软件配置里填入

aaa [email protected]

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

git commit --author="aaa " -m "Note"

参考:

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