git 本地操作(一、配置与常用命令)

Posted on Posted in git
Tips: 本文创建于2016年5月10日,已超过 2 年,内容或图片可能已经失效!

git 简单配置

基本配置

git config --global user.name liuhonghe  
git config --global user.email git@liuhongh.me  

git 的三个级别

  • git config --local #针对当前仓库
  • git config --global #针对当前用户
  • git config --system

查看 git 帮助文档

git config --help  
git help config  
man git-config  

git 配置的增删改查

git config --global --add user.name eoe # add 参数表示可以有多个同样的键值对  
git config user.name # 查询  
git config -get user.name  
git config --list --global  # 查看键值列表  
  
git config --global --unset user.name eoe # 删除,当 user.name 只有一个值时,eoe 可以省略  
  
git config --global user.name eoe # 修改 user.name 键值  

为 git 子命令配置别名

git config --global alias.co checkout # 给 checkout 起别名,叫 co  
git config --global alias.br branck  
git config --global alias.st status  
git config --global alias.ci commit  
git config --global alias.lol "log --oneline"  

git 基本工作流程

获取工作仓库

git init git_repo # 新建一个 git_repo 仓库,自动创建文件夹 git_repo  
git init --bare git_bare_repo # 建立裸仓库,没有 .git 目录  
cd git_dir && git init # 基于存在的目录建立 git 仓库  
git clone #后面可以加本地地址、远程地址  

git 三个区

工作区:日常编辑代码的地方,维护着一个树形结构
暂存区:工作区和历史提交中间的一个缓存,代表需要提交的一个工作的状态,维护一个虚拟的树形结构
历史仓库:是一个 commit 指向的一个树形结构

git 最常用命令

  • git add
  • git add -A #把整个工作区添加到暂存区
  • git add . #把整个工作区添加到暂存区
  • git commit
  • git status
  • git rm
  • git mv # 把文件重命名或移动,同时操作暂存区
  • gitignore
-> dreamoon@lhh-mac ~/git ☺ git init git_basics  
Initialized empty Git repository in /Users/dreamoon/git/git_basics/.git/  
-> dreamoon@lhh-mac ~/git ☺ cd git_basics  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ touch a b  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git add a b  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
  
Initial commit  
  
Changes to be committed:  
  (use "git rm --cached <file>..." to unstage)  
  
    new file:   a  
    new file:   b  
  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git commit -m "Initial commit"  
[master (root-commit) 1b10f91] Initial commit  
 2 files changed, 0 insertions(+), 0 deletions(-)  
 create mode 100644 a  
 create mode 100644 b  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ echo "test status" >> a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes not staged for commit:  
  (use "git add <file>..." to update what will be committed)  
  (use "git checkout -- <file>..." to discard changes in working directory)  
  
    modified:   a  
  
no changes added to commit (use "git add" and/or "git commit -a")  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git add a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes to be committed:  
  (use "git reset HEAD <file>..." to unstage)  
  
    modified:   a  
  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git commit -m "modified a"  
[master a5ecfab] modified a  
 1 file changed, 1 insertion(+)  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ git rm a  
rm 'a'  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes to be committed:  
  (use "git reset HEAD <file>..." to unstage)  
  
    deleted:    a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git reset HEAD a # 还原  
Unstaged changes after reset:  
D   a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git checkout a #还原  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ git rm --cached a #只删除暂存区中的文件,不删除工作区中的文件  
rm 'a'  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes to be committed:  
  (use "git reset HEAD <file>..." to unstage)  
  
    deleted:    a  
  
Untracked files:  
  (use "git add <file>..." to include in what will be committed)  
  
    a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git add a #重新添加回暂存区  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git mv c a  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ mv a c  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes not staged for commit:  
  (use "git add/rm <file>..." to update what will be committed)  
  (use "git checkout -- <file>..." to discard changes in working directory)  
  
    deleted:    a  
  
Untracked files:  
  (use "git add <file>..." to include in what will be committed)  
  
    c  
  
no changes added to commit (use "git add" and/or "git commit -a")  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ ls  
b c  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git add a c  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git status  
On branch master  
Changes to be committed:  
  (use "git reset HEAD <file>..." to unstage)  
  
    renamed:    a -> c  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ☺ vim .gitignore # 忽略规则  
*.[oa] # 以o 或者 a 结尾的文件,忽略  
*~  
*.pyc  
!test.pyc #告诉 git,这个文件不要被忽略。上面忽略.pyc 文件,这个规则排除特例  
\!test.py # 以!为开头的文件,被纳入进来  
foo/ # 匹配的是一个目录  
**/res # 匹配0个或者多个目录,所有 res 目录  
.DS_Store  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git add .gitignore  
-> dreamoon@lhh-mac ~/git/git_basics git:(master) ✗ ☺ git commit -m "add ignore"  
[master b91f363] add ignore  
 1 file changed, 8 insertions(+)  
 create mode 100644 .gitignore  

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + sixteen =