[Git] commonly used commands and configuration

principle

Many Git commands, but for everyday use six major command Just remember that in the figure, on it.
image.png
Workspace (Working Directory)
at normal git directory under management are regarded as a work area. It is where you usually store the project code.
Temporary Area (Stage / Index)
staging area for temporary storage of your changes, in fact it's just a file, save the file to the list of information to be submitted.
Warehouse District (Repository or Git Directory)
Warehouse District (or repository), where the data is secure, and there are you committed to all versions of the data. HEAD which points to the latest version into the warehouse.
Remote repository (Remote Directory)
remote repository, managed code server (such as Github, Bitbucket, etc.), you can simply think of the project team in a computer for remote data exchange.

Comparing the difference between the three data regions, can be used diff command:

  • git diff Workspace vs staging area
  • git diff head Workspace vs repository
  • git diff --cached Staging area vs repository

git general workflow:

  • 1, in the working directory to add, modify files;
  • 2, will need to be versioned file into the staging area;
  • 3, will be submitted to the staging area file git repository.
    Therefore, git managed file has three states: modified (modified), it has been staging (staged), has been submitted (committed).

Common Commands

New code base

git init #在当前目录新建一个Git代码库
git init <project-name> # 新建一个目录,将其初始化为Git代码库
git clone <url> # 下载一个项目和它的整个代码历史

Configuration

# 设置提交代码时的用户信息
git config --global user.name "<your name>"
git config --global user.email "<your email>"

Add or remove files

git add <file1> <file2> # 添加指定文件到暂存区
git add <dir> # 添加指定目录到暂存区,包括子目录
git add .  # 添加当前目录的所有文件到暂存区
git add -p  #同一文件多处变化时,分次提交

git rm <file1> <file2>  # 删除工作区文件,并且将这次删除放入暂存区
git rm --cached <file> #停止追踪该文件,但该文件会保留在工作区

git mv  <old_file>  <new_file> # 改名文件,并且将这个改名放入暂存区

Code Submit

git commit -m <message> # 提交暂存区到仓库区
git commit <file1> <file2>  -m <message>  # 提交暂存区的指定文件到仓库区

git commit -a #自上次commit变化后一次提交到仓库

git commit -v  #所有时显示所有diff信息

git commit --amend -m <message>  #替代上次commit
git commit --amend <file1> <file2> # 重做上一次commit,并包括指定文件的新变化

Branch

git branch #查看所有本地分支
git branch -r #查看所有远程分支
git branch -a #查看所有分支

git branch <new_branch> #新建分支,但仍在当前分支
git checkout -b <new_branch> #新建分支并切换到该分支,更常用
git branch --track <new_branch> <remote_branch> #新建分支并追踪远程分支

git checkout <branch_name> #切换到指定分支
git checkout -  #切换到上一分支

git branch --set-upstream <branch> <remote_branch> #在现有分支和远程分支间建立追踪关系
git merge <branch> #合并指定分支到当前分支

git branch -d <branch> #删除分支
git push origin --delete <branch> #删除远程分支

label

git tag # 列出所有tag
git tag <tag_name>  # 新建一个tag在当前commit
git tag <tag_name> <commit>  #新建一个tag在指定commit

git tag -d <tag> # 删除本地tag
git push origin :refs/tags/<tag_name> # 删除远程tag

git show <tag>  # 查看tag信息

View information
command too much, so hard to remember. Find when needed.

git status # 显示有变更的文件
git log # 显示当前分支的版本历史
git log --stat # 显示commit历史,以及每次commit发生变更的文件
git log -S <keyword> # 搜索提交历史,根据关键词
git log <tag> HEAD --pretty=format:%s # 显示某个commit之后的所有变动,每个commit占据一行

git log --follow <file> # 显示某个文件的版本历史,包括文件改名
git whatchanged <file>  # 显示某个文件的版本历史,包括文件改名

git log -p <file> # 显示指定文件相关的每一次diff
git log -5 --pretty --oneline # 显示过去5次提交

git diff <first_branch> <second_branch> # 显示两次提交之间的差异
git reflog # 显示当前分支的最近几次提交

Remote synchronization

git fetch <remote> # 下载远程仓库的所有变动
git remote -v # 显示所有远程仓库
git remote show <remote> # 显示某个远程仓库的信息

git remote add <new_remote> <url> # 增加一个新的远程仓库,并命名
git pull <remote> <branch>       #pull=fetch + merge  取回远程仓库的变化,并与本地分支合并

gut push <remote> <branch>  # 上传本地指定分支到远程仓库
git push <remote> --force  # 强行推送当前分支到远程仓库,即使有冲突
git push <remote> --all # 推送所有分支到远程仓库

Undo
too many orders, too lazy to remember.

git checkout <file> # 恢复暂存区的指定文件到工作区
git checkout <commit> <file> # 恢复某个commit的指定文件到暂存区和工作区
git checkout . # 恢复暂存区的所有文件到工作区

other

git archive  # 生成一个可供发布的压缩包

other

Linux configuration alias
configuration alias git command in Linux, similar to the alias Linux commands. The following settings were added in ~ / .bashrc:

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

When later use, simply git stor git ci -m "message"view, similar to other commands.
Git GUI
Git has a lot of graphical tools, recommended SourceTree

Ref:
Common Git commands list
Liao Xuefeng Git
git work area, staging area, repository, a remote repository
git tag description

Guess you like

Origin www.cnblogs.com/jessepeng/p/12519002.html