Git 常用命令

一. 理解

git图解

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

二. 新建Repository

  1. 创建本地仓库
    1
    2
    3
    4
    5
    6
    7
    git init #git 初始化 
    git add . #添加所有文件到暂存区
    git commit -m "content" #添加备注提交本地仓库
    git remote add origin http://url.git # 添加远程仓库路径
    git push -u origin master #push 代码到远程仓库

    git push remote [remote][branch] #上面两行代码的合并

2.复制远程仓库

1
git clone [url]

三. 配置

1
2
3
git config --list #显示所有Git配置
git config --global user.name "username"
git config --global user.email "email address"

四. 需要记住的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git add . 
git commit -m "content"
git branch #列出所有本地分支
git branch -r #列出所有远程分支
git branch -a #列出所有本地和远程分支
git branch [分支名字] #新建分支
git checkout [分支名字] #切换到指定分支
git merge [分支名字] #合并指定分支到当前分支
git status #显示有变更的文件
git log #显示当前分支的版本历史
git diff #显示暂存区和工作区的代码差异
git checkout [file] #恢复暂存区的指定文件到工作区
git checkout . #恢复暂存区的所有文件到工作区
git reset --hard [commit] #让工作区回到上次commit的状态,同时重置暂存区和工作区,回滚到某个版本,该版本之后的所有代码丢失,慎用!
git revert #未完
坚持记录世界,分享世界