Git 学习笔记,主要用来记忆常用命令。

.git 目录下文件

$>tree -L 1
.
|-- HEAD 			# git 项目当前处在哪个分支里
|-- config 			# 项目的配置信息,git config 命令会改动它
|-- description  	# 项目的描述信息
|-- hooks/ 			# 系统默认钩子脚本目录
|-- index 			# 索引文件
|-- logs/ 			# 各个 refs 的历史信息
|-- objects/ 		# Git 本地仓库的所有对象 (commits, trees, blobs, tags)
|-- refs/ 			# 标识你项目里的每个分支指向了哪个提交 (commit)。

Generating SSH keys

生成 SSH keys

ssh-keygen -t rsa -C "youremail@example.com"

一直回车,将自动生成id_rsaid_rsa.pub文件。使用以下命令验证连接是否成功

ssh -T git@github.com

创建 Git 仓库

创建本地新 Git 仓库

git init

克隆远端仓库

git clone ssh://user@domain.com/repo.git

git commit

提交

  • 修改上次提交 不要修改已经发布的提交记录

      git commit --amend
    
  • 创建签名提交

      git commit -sm "commit msg"
    
  • 撤销本地提交,上一次提交修改内容会添加到暂存区

      git reset --soft HEAD~1
    
  • 回退一个 commit 版本,就好像这个 commit 从来没有提交过

      git reset HEAD^
      git reset HEAD~1
    
  • 强制撤销提交,commit-id 之后的提交都会被抛弃

      git reset --hard <commit-id>
    
  • 撤销之前提交的一个 commit, revert 命令会创建一个新的 commit

      git revert HEAD
      git revert commitid
    

优雅的撰写 commit 信息

一份优雅的 Commit message 可以方便后期查看,也方便快速定位更新的内容,通常来说在标题中用简介的一行来描述更改的内容,在描述中使用更多的细节来描述具体修改内容。

更多关于 Git message 的内容可以查看这里

git checkout

  • 替换本地改动

    此命令会使用 HEAD 中的最新内容替换掉你的工作目录中的文件。已添加到暂存区的改动以及新文件都不会受到影响。

      git checkout -- <filename>
    
  • 分离 HEAD

    一般 HEAD 都是隐藏在 master 后面,分离 HEAD 就是让其指向一个提交而不是分支名。使用以下命令可以分离 HEAD。

    git checkout commitID

git push

  • 将本地修改发布到远端

      git push <remote> <local-branch>
    
  • 强制推送本地版本到远端库

    如果想要删除远端仓库不小心提交的 commit,可以使用这条命令,远端仓库之后的 commit 会被抹去

      git push origin HEAD --force
      git push -f origin master  # 将本地 master 分支强行推送到远端并覆盖远端修改内容
    
  • 推送到镜像 repo

    例如本地关联的远端仓库是 gitlab 的,可以使用--mirror参数将本地库提交到 github

      git push --mirror https://github.com/einverne/dotfiles.git
    
  • 删除远端分支 delete remote branch 以下命令二选一,个人推荐使用第二种,因为简单方便嘛:

      git push origin --delete <branchname>
      git push origin :<branch name>
    

git branch

分支相关命令

  • 查看远端和本地分支

      git branch -a, -all
    
  • 新建分支

      git branch <new-branch-name>
    
  • 切换到已经存在的分支

      git checkout <branchname>
    
  • 创建分支并切换到该分支

      git checkout -b <branchname>
    
  • 切换到 master 分支,合并分支

    合并不一定完全成功,可能遇到冲突

      git merge <branchname>
    
  • 删除本地分支

      git branch -d <branchname>
    
  • 强制删除未合并的分支

      git branch -D <branchname>
    
  • 重命名分支,重命名 old-name 到 new-name,如果 new-name 存在,命令失败

      git branch -m old-name new-name
    
  • 创建新的空分支

    create a New and Empty branch , without any commit. And use git rm -rf . to delete all files in the new working directory. You need to run git version 1.7.2 or higher in order for the –orphan option to be supported.

      git checkout --orphan <branch name>
    

git remote

  • 添加远端仓库

    Git 将本地 master 分支内容推动到远程新的 master 分支,并且将本地 master 分支与远程 master 分支关联

      git remote add <name> <url>
      git remote add origin https://github.com/einverne/xxx.git
      git remote add origin git@github.com:einverne/xxx.git
    
  • 将本地分支推送到远端仓库

      git push <remote name> <local-branch-name>
      git push -u origin master
    
  • 显示远端仓库地址

      git remote -v
    
  • 删除远端仓库

      git remote rm <name>
    
  • 设置远端仓库地址,修改远端仓库地址

      git remote set-url origin git@github.com:einverne/repo.git
    
  • 给已存在 origin 远端添加 URL

    如果远端 origin 存在多个 url,使用 git push origin master 可同时将本地库推送到多个远端仓库。

      git remote set-url --add origin https://github.com/einverne/dotfiles.git
    
  • 显示远端信息

      git remote show origin
    

git fetch

下载远端所有改动到本地,不自动合并到当前

git fetch <remote>

git pull

更新与合并,更新本地仓库至最新改动

git pull

git tag

打标签

  • 创建标签

    创建 <tagname> 的标签,9fceb02 为 commit id,使用git log可以查看提交的 ID,一般可以选用前 10 位,如果唯一也可少选

      git tag -a <tagname> [9fceb02]
    
  • 显示标签

      git show v1.2
    
  • 推送特定 tag

      git push origin [tagname v1.5]
    
  • 推送所有 tags

      git push origin --tags
    

git log

查看提交历史

  • 从最新提交开始显示所有提交历史

      git log
    
  • 显示指定文件的所有修改

      git log -p <file>
    
  • 谁,在什么时间,修改了文件的那些内容

      git blame <file>
    

git aliases

  • 修改 config

      git config -e
    
  • 别名

      git config --global alias.co checkout  # 设置之后 git co 就代表 git checkout 了
      git config --global alias.br branch
      git config --global alias.ci commit
      git config --global alias.st status
    
  • unstage 在 smartGit 中经常使用的命令,存入暂存区,从暂存区移出

      git config --global alias.unstage 'reset HEAD --'
    

然后我就可以使用,git add <file> 来将文件加入暂存区,使用 git unstage <file> 来将文件移出暂存。

推荐小游戏

最后来推荐一个 git 小游戏,用 ruby 写成,Github,在游戏过程中学习 git 基本使用。有几十个关卡,通关了大概也就对 git 非常了解了。

gem install githug

运行 githug 进入游戏。

Tips

从 git 移除文件而不删除硬盘文件

Remove file from git source control but not delete it from drive 从 git 移除文件

The git rm command will allows you to remove a file from git control. The -cached option to git remove allows you to leave it on your hard drive. Every once in awhile a file gets checked into git that isn’t supposed to be there. Common examples are configuration files, project files generated by your IDE with personal settings and even the occasional object file that someone decided to check in. These files are needed, so often you can’t delete them entirely and the process of copying them somewhere else, removing them from git and then replacing is painful, not to mention prone to error. By adding the -cached option to the git rm command, you are able to remote the file file from git control while keeping the file in your working tree. They command syntax is:

git rm --cached file

Git will no longer track this file even though it is still on your hard drive. After running the above command, be sure to add an entry to your .gitignore file so that ‘file’ doesn’t show up in git status and that it can’t accidentally be re-added later.

from : gitguys

clone into a empty directory

克隆项目到空文件夹中,在空目录中

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master

NOTE: -t will set the upstream branch for you, if that is what you want, and it usually is.

合并他人分支

查看当前项目下远程
git remote
增加新的分支链接,例如
git remote add other giturl
获取他人的远程更新
git fetch other
将他人的远程更新合并到本地分支
git merge other/master
同样也可用此方法同步更新 fork 的项目

为某一个 Git 仓库单独配置提交者信息

在需要修改的仓库中运行如下语句,写入当前仓库的 config 中

git config user.name "Your Name"
git config user.email your@email.com

该配置会写入 .git/config 设置中。

网站

强烈推荐项目:https://github.com/einverne/my-git 这是我从 @xirong fork 而来的项目,以后有好的 Git 内容我也会更新到该项目。