浅谈Git

  
  
Git 核心概念 
  
  
Git 最核心的一个概念就是工作流 
  
  
 工作区(Workspace)是电脑中实际的目录。 
 暂存区(Index)类似于缓存区域,临时保存你的改动。 
 仓库区(Repository),分为本地仓库和远程仓库。 
  
  
  
  
  
  
  
  
                 
初始化 
                 
$ git init# 在当前目录新建一个Git代码库
  
$ git clone [url]# 下载一个项目和它的整个代码历史 [Git only]
                 
                 
                 
配置--- Git 用户的配置文件位于 ~/.gitconfig  Git 单个仓库的配置文件位于 ~/$PROJECT_PATH/.git/config
                 
$ git config -l# 列举所有配置
  
$ git config --global alias.co checkout# 为命令配置别名
$ git config --global alias.ci commit 
$ git config --global alias.st status 
$ git config --global alias.br branch 
  
$ git config [--global] user.name "[name]"# 设置提交代码时的用户信息
$ git config [--global] user.email "[email address]" 
                 
                 
                 
增删文件---把文件名 file1 添加到 .gitignore 文件里,Git 会停止跟踪 file1 的状态
                 
$ git add .# 添加当前目录的所有文件到暂存区
  
$ git add <file1> <file2> ...# 添加指定文件到暂存区
  
$ git add <dir># 添加指定目录到暂存区,包括其子目录
  
$ git rm [file1] [file2] ...# 删除工作区文件,并且将这次删除放入暂存区
  
$ git rm --cached [file]# 停止追踪指定文件,但该文件会保留在工作区
  
$ git mv [file-original] [file-renamed]# 改名文件,并且将这个改名放入暂存区
                 
                 
                 
分支
                 
$ git branch# 列出所有本地分支/不带参数列出本地已经存在的分支,并且在当前分支的前面用"*"标记
  
$ git branch -r# 列出远程版本库分支列表
  
$ git branch -a# 列出所有本地分支和远程分支
  
$ git branch [branch-name]# 新建一个分支,但依然停留在当前分支
  
$ git checkout -b [new_branch] [remote-branch]# 新建一个分支,并切换到该分支
         示例:git checkout -b master 如果分支存在则只切换分支,若不存在则创建并切换到master分支,repo start是对git checkout -b这个命令的封装,将所有仓库的分支都切换到master,master是分支名
         
 
$ git checkout [branch-name]# 切换到指定分支,并更新工作区
  
$ git merge [branch]# 合并指定分支到当前分支
  
$ git cherry-pick [commit]# 选择一个 commit,合并进当前分支
  
$ git branch -d [branch-name]# 删除本地分支,-D 参数强制删除分支
                 
$ git branch -vv # 可以查看本地分支对应的远程分支
                 
$ git branch -m oldName newName# 以查看本地分支对应的远程
                 
$ git push [remote] :[remote-branch]# 删除远程分支
                 
                 
                 
                 
提交 
                 
$ git commit -m [message]# 提交暂存区到仓库区
  
$ git commit -a# 提交工作区与暂存区的变化直接到仓库区
  
$ git commit -v# 提交时显示所有 diff 信息
  
$ git commit --amend -m [message]# 提交暂存区修改到仓库区,合并到上次修改,并修改上次的提交信息
  
$ git push [remote] [remote-branch]# 上传本地指定分支到远程仓库
                 
                 
                 
                 
拉取 
                 
$ git fetch [remote]# 下载远程仓库的所有变动 (Git only)
  
$ git remote -v# 显示所有远程仓库 (Git only)
  
$ git remote show [remote]# 显示某个远程仓库的信息 (Git only)
  
$ git remote add [remote-name] [url]# 增加一个新的远程仓库,并命名 (Git only)
  
$ git pull [remote] [branch]# 取回远程仓库的变化,并与本地分支合并,(Git only), 若使用 Git-SVN,请查看第三节
 
$ git pull --rebase [remote] [branch]# 取回远程仓库的变化,并与本地分支变基合并,(Git only), 若使用 Git-SVN,请查看第三节
         
                 
                 
                 
                 
撤销 
                 
$ git checkout [file]# 恢复暂存区的指定文件到工作区/放弃单个文件的修改
  
$ git checkout .# 恢复暂存区当前目录的所有文件到工作区/放弃当前目录下的修改
  
$ git checkout [commit]# 恢复工作区到指定 commit
  
$ git reset [file]# 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
  
$ git reset --hard# 重置暂存区与工作区,与上一次 commit 保持一致
  
$ git reset [commit]# 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
  
$ git reset --hard [commit]# 重置当前分支的HEAD为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
  
$ git revert [commit]# 新建一个 commit,用于撤销指定 commit
  
$ git stash# 将未提交的变化放在储藏区
  
$ git stash pop# 将储藏区的内容恢复到当前工作区
                 
                 
                 
                 
查询 
                 
$ git status # 查看工作区文件修改状态
  
$ git diff [file]# 查看工作区文件修改具体内容 
  
$ git diff --cached [file] # 查看暂存区文件修改内容
  
$ git log # 查看版本库修改记录
  
$ git log --author=someone # 查看某人提交记录
  
$ git log -p [file] # 查看某个文件的历史具体修改内容
  
$ git show [commit]# 查看某次提交具体修改内容