GIT BASICS | ||
---|---|---|
git init |
在指定的⽬录下创建⼀个空的git repo。不带参数将在当前⽬录下创建⼀个git repo | Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository. |
git clone |
克隆⼀个指定repo到本地。指定的repo可以是本地⽂件系统或者由HTTP或SSH指定的远程路径。 | Clone repo located at |
git config user.name |
针对当前repo配置⽤户名。使⽤–global 参数将配置全局⽤户名。 | Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user. |
git add |
将指定⽬录的所有修改加⼊到下⼀次commit中。把 |
Stage all changes in |
git commit -m “ |
提交暂存区的修改,使⽤指定的 本编辑器输⼊提交信息。 |
Commit the staged snapshot, but instead of launching a text editor, use |
git status | 显示哪些⽂件已被staged、未被staged以 及未跟踪(untracked)。 |
List which files are staged, unstaged, and untracked. |
git log | 以缺省格式显示全部commit历史。更多⾃ 定义参数请参考后续部分。 |
Display the entire commit history using the default format. For customization see additional options. |
GIT DIFF | ||
---|---|---|
git diff | ⽐较⼯作区和暂存区的修改。 | Show unstaged changes between your index and working directory. |
git diff HEAD | ⽐较⼯作区和上⼀次commit后的修改 | Show difference between working directory and last commit. |
git diff --cached | ⽐较暂存区和上⼀次commit后的修改。 | Show difference between staged changes and last commit |
UNDOING CHANGES | ||
---|---|---|
git revert |
对指定 commit,并应⽤到当前分⽀。 |
Create new commit that undoes all of the changes made in |
git reset |
将 变。此操作不会修改⼯作区的任何⽂件。 |
Remove directory unchanged. This unstages a file without overwriting any changes. |
REWRITING GIT HISTORY | ||
---|---|---|
git commit -m |
将当前staged修改合并到最近⼀次的 commit中。 |
Replace the last commit with the staged changes and last commit combined. |
git rebase |
基于 相对于HEAD的commit。 |
Rebase the current branch onto commit ID, branch name, a tag, or a relative reference to HEAD. |
git reflog | 显示本地repo的所有commit⽇志。 | Show a log of changes to the local repository’s HEAD |
GIT BRANCHES | ||
---|---|---|
git branch | 显示本地repo的所有分⽀ | List all of the branches in your repo |
git switch -c |
创建并切换到⼀个新的名为 ⽀。去掉-c参数将切换到⼀个已有分⽀。 |
Create and switch to a new branch named -c flag to switch to an existing branch. |
git merge |
将指定 |
Merge |
REMOTE REPOSITORIES | ||
---|---|---|
git remote add |
添加⼀个新的远程连接。添加后可使⽤ |
Create a new connection to a remote repo. After adding a remote, you can use commands. |
git fetch |
从指定 有commit到本地repo。去掉 抓取远程所有分⽀的修改。 |
Fetches a specific |
git pull |
从指定 并⽴刻合并到本地repo。 |
Fetch the specified remote’s copy of current branch and immediately merge it into the local copy. |
git push |
将本地指定 ⾃动在远程创建此分⽀。 |
Push the branch to and objects. Creates named branch in the remote repo if it doesn’t exist. |
GIT CONFIG | ||
---|---|---|
git config – global user.name |
配置当前⽤户名,使⽤–global参数将针对 当前系统登录⽤户⽣效。 |
Define the author name to be used for all commits by the current user. |
git config – global user.email |
配置当前⽤户Email。 | Define the author email to be used for all commits by the current user. |
git config – global alias. |
配置⼀个git命令的快捷⽅式。例如:配 置”alias.glog log --graph --oneline”使”git glog”相当于”git log --graph --oneline”. |
Create shortcut for a Git command. E.g. alias.glog “log – graph --oneline” will set ”git glog”equivalent to ”git log --graph –oneline. |
git config – system core.editor |
配置⽂本编辑器,例如vi,在必要时⾃动 打开此⽂本编辑器。 |
Set text editor used by commands for all users on the machine. the desired editor (e.g., vi). |
git config – global --edit |
打开当前⽤户的git全局配置并编辑。 | Open the global configuration file in a text editor for manual editing. |
GIT LOG | ||
---|---|---|
git log - |
限制log的显示数量。例如:”git log -5”仅 显示最新5条commit。 |
Limit number of commits by to 5 commits. |
git log --oneline | 每⾏显示⼀条commit | Condense each commit to a single line. |
git log --author= “ |
按提交者名字搜索并显示commit。 | Search for commits by a particular author |
git log --grep= “ |
按指定内容搜索并显示commit。 | Search for commits with a commit message that matches |
git log |
显示指定范围的commit。范围参数可以是 commit ID、分⽀名称、HEAD或任意相对 位置。 |
Show commits that occur between can be a commit ID, branch name, HEAD, or any other kind of revision reference. |
git log – |
仅显示包含指定⽂件修改的commit。 | Only display commits that have the specified file. |
git log --graph | 使⽤–graph参数显示图形化的branch信 息。 |
–graph flag draws a text based graph of commits on left side of commit msgs |
GIT RESET | ||
---|---|---|
git reset | 移除所有暂存区的修改,但不会修改⼯作 区。 |
Reset staging area to match most recent commit, but leave the working directory unchanged. |
git reset --hard | 移除所有暂存区的修改,并强制删除所有 ⼯作区的修改。 |
Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory. |
git reset |
将当前分⽀回滚到指定 暂存区的修改,但保持⼯作区状态不变。 |
Move the current branch tip backward to staging area to match, but leave the working directory alone |
git reset --hard |
将当前分⽀回滚到指定 暂存区的修改,并强制删除所有⼯作区的 修改。 |
Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after |
GIT REBASE | ||
---|---|---|
git rebase -i |
以交互模式对当前分⽀做rebase。 | Interactively rebase current branch onto editor to enter commands for how each commit will be transferred to the new base. |
GIT PULL PUSH | ||
---|---|---|
git pull --rebase |
抓取所有远程分⽀,并以rebase模式并⼊ 本地repo⽽不是merge |
Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches. |
git push –force |
将本地分⽀推送到远程。不要使⽤–force 参数,除⾮你完全明⽩此操作的后果。 |
Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing. |
git push –tags |
使⽤push命令并不会⾃动将本地tag推送 到远程。加上–tags参数会将所有本地tag 推送到远程。 |
Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo. |