纯记录

生成密钥对

$ ssh-keygen -t rsa -C "username@gmail.com"

Git 初始设置

$ git config --global user.name "username"
$ git config --global user.email username@gmail.com

建立仓库并初始化

$ cd overlay && git init

添加项目暂存

$ git add .

提交修改

$ git commit -m 'commit'
$ git commit --amend -m "commit other"          # 修正之前的提交注释

上传 origin 到远程的 master 分支

$ git remote add origin git@github.com:Havanna/overlay.git
$ git push origin master

一般性步骤

$ cd overlay
$ git add .
$ git commit -m 'commit'
$ git remote add origin git@github.com:Havanna/overlay.git      #仅第一次指定 remote 地址,以后 push 时可以省略
$ git push origin master

更新文件

$ git pull origin            #更新,chekout 代码
$ git fetch origin           #更新远端 repository 信息
$ git merge origin master    #合并
$ vi README
$ git commit -a
$ git push origin master     #提交

创建分支

$ git branch
$ git branch new-branch
$ git checkout new-branch
$ touch newreadme
$ git add newreadme
$ git commit -a -m 'add newreadme'
$ git push origin new-branch

创建空白分支

$ git checkout --orphan gh-pages
$ git rm -rf .
$ git add .
$ git commit -m 'new branch: gh-pages'
$ git push origin gh-pages

删除远程分支

$ git push origi :new-branch

删除本地分支

$ git branch -d new-feature

合并进 master

$ git checkout master
$ git merge new-feature
$ git branch
$ git push

github 回滚

$ git branch old                               #创建 old 分支
$ git push origin old:old                      #push 到远程 github
$ git reset --hard id                          #本地回滚到某版本,并随后将 github 上默认分支改为 old
$ git push origin :master                      #删除 github 上的 master 分支
$ git push origin master                       #push重新创建的master分支
$ git push origin :old                         #将 github 默认分支改为 master,并删除远程 old 分支
$ git clone git@github.com:Havanna/overlay.git #重新 clone

撤销本地更改

$ git checkout file                             # 检出修改的文件

撤销暂存更改

$git revert HEAD file

撤销提交的版本

$ git revert HEAD                              #撤销前一次commit
$ git revert HEAD^                             #撤销前前一次commit
$ git revert commit id                         #撤销指定的版本

$ git reset --hard id
$ git push origin HEAD --force

如果仅撤销 commit message,则

$ git reset --soft id
$ git commit -m 'xxx'

.gitIgnore 文件忽略哪些不被 Git 跟踪的文件,在根目录下生成 .gitignore 文件

# Can ignore specific files
.DS_Store
# Use wildcards as well
*~
*.swp
# Can also ignore all directories and files in a directory.
tmp/**/*
# Ignore all dotfiles...
.*
# except for .gitignore
!.gitignore

删除悬空对象(dangling objects)

$ git gc --prune=0
$ git reflog expire --expire-unreachable=0 --all