git

git常用命令

Posted by eye on 04-20,2023
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 onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH
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 for the next commit. Replace with a to change a specific file.
git commit -m
提交暂存区的修改,使⽤指定的
作为提交信息,⽽不是打开⽂
本编辑器输⼊提交信息。
Commit the staged snapshot, but instead of launching a text
editor, use as the commit message.
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
对指定创建⼀个undo的
commit,并应⽤到当前分⽀。
Create new commit that undoes all of the changes made in
, then apply it to the current branch.
git reset 从暂存区移除,但保持⼯作区不
变。此操作不会修改⼯作区的任何⽂件。
Remove from the staging area, but leave the working
directory unchanged. This unstages a file without overwriting
any changes.
REWRITING GIT HISTORY
git commit -m
--amend
将当前staged修改合并到最近⼀次的
commit中。
Replace the last commit with the staged changes and last
commit combined.
git rebase 基于对当前分⽀进⾏rebase。
可以是commit、分⽀名称、tag或
相对于HEAD的commit。
Rebase the current branch onto . can be a
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 . Drop the
-c flag to switch to an existing branch.
git merge
将指定分⽀合并到当前分⽀。 Merge into the current branch.
REMOTE REPOSITORIES
git remote add
添加⼀个新的远程连接。添加后可使⽤
作为指定远程连接的名称。
Create a new connection to a remote repo. After adding a
remote, you can use as a shortcut for in other
commands.
git fetch
从指定抓取指定的所
有commit到本地repo。去掉
抓取远程所有分⽀的修改。
Fetches a specific , from the repo. Leave off
to fetch all remote refs.
git pull 从指定抓取所有分⽀的commit
并⽴刻合并到本地repo。
Fetch the specified remote’s copy of current branch and
immediately merge it into the local copy.
git push
将本地指定推送到指定远程
。如果远程没有对应的分⽀,将
⾃动在远程创建此分⽀。
Push the branch to , along with necessary commits
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. arg should be the command that launches
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 . E.g. ”git log -5” will limit
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 and . Args
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 , reset the
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 . Launches
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.