Git是使用最广泛且功能最强大的版本控制系统,用于跟踪计算机文件中的更改并在多人之间协调这些文件上的工作。 它主要用于软件开发中的源代码管理,但可以用来跟踪任何文件集的更改。
Git由Linus Torvalds于2005年开发,是一种分布式开源软件版本控制软件,当然它是免费使用的。 作为分布式修订控制系统,它的目标是速度,数据完整性以及对分布式非线性工作流的支持。
虽然其他版本控制系统(例如CVS,SVN)将大多数数据(如提交日志)保留在中央服务器上,但是每台计算机上的每个git存储库都是具有完整历史记录和完整版本跟踪功能的成熟存储库,而与网络访问或中央服务器无关。
但是,几乎所有的IDE都支持git开箱即用,我们不需要手动提交git命令,但是了解这些命令始终是一件好事。 以下是一些git命令的列表,这些命令可有效使用git。
Git帮助
git中最有用的命令是
git help
提供了我们所需的所有帮助。 如果我们输入 git help
in terminal,我们会得到
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some concept guides.
See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
命令
git help -a
将为我们提供完整的git命令列表
Available git commands in '/usr/local/git/libexec/git-core'
add clone fetch interpret-trailers notes remote-testsvn submodule
add--interactive column fetch-pack log p4 repack submodule--helper
am commit filter-branch ls-files pack-objects replace subtree
annotate commit-tree fmt-merge-msg ls-remote pack-redundant request-pull svn
apply config for-each-ref ls-tree pack-refs rerere symbolic-ref
archimport count-objects format-patch mailinfo patch-id reset tag
archive credential fsck mailsplit prune rev-list unpack-file
bisect credential-cache fsck-objects merge prune-packed rev-parse unpack-objects
bisect--helper credential-cache--daemon gc merge-base pull revert update-index
blame credential-store get-tar-commit-id merge-file push rm update-ref
branch cvsexportcommit grep merge-index quiltimport send-email update-server-info
bundle cvsimport gui merge-octopus read-tree send-pack upload-archive
cat-file cvsserver gui--askpass merge-one-file rebase sh-i18n--envsubst upload-pack
check-attr daemon hash-object merge-ours rebase--helper shell var
check-ignore describe help merge-recursive receive-pack shortlog verify-commit
check-mailmap diff http-backend merge-resolve reflog show verify-pack
check-ref-format diff-files http-fetch merge-subtree remote show-branch verify-tag
checkout diff-index http-push merge-tree remote-ext show-index web--browse
checkout-index diff-tree imap-send mergetool remote-fd show-ref whatchanged
cherry difftool index-pack mktag remote-ftp stage worktree
cherry-pick difftool--helper init mktree remote-ftps stash write-tree
citool fast-export init-db mv remote-http status
clean fast-import instaweb name-rev remote-https stripspace
和命令
git help -g
会给我们列出git概念
The common Git guides are:
attributes Defining attributes per path
everyday Everyday Git With 20 Commands Or So
glossary A Git glossary
ignore Specifies intentionally untracked files to ignore
modules Defining submodule properties
revisions Specifying revisions and ranges for Git
tutorial A tutorial introduction to Git (for version 1.5.1 or newer)
workflows An overview of recommended workflows with Git
我们可以用
git help <command>
或 git help <concept>
命令可了解有关特定命令或概念的更多信息。
Git配置
描述 | Git命令 |
---|---|
配置要与提交一起使用的作者名称。 | git config --global user.name "Sam Smith" |
配置作者电子邮件地址以用于提交 | git config --global user.email sam@example.com |
将从存储库中删除用户凭证详细信息 | git config --local credential.helper "" |
列出所有当前配置的远程存储库URL | git remote -v |
如果尚未将本地存储库连接到远程服务器,请执行以下操作:将远程服务器添加到本地存储库 | git remote add origin <repo_url> |
Git提交并推送
描述 | Git命令 |
---|---|
使用Readme content 内容创建文件名README.md | echo "Readme content" >> README.md |
列出您已更改的文件以及仍需要添加或提交的文件 | git status |
将全部或一个文件添加到暂存 | git add . 或git add file_name |
用消息提交更改头部 | git commit -m 'message' |
提交使用git add 所有文件,以及提交自那时以来更改的任何文件 | git commit -a |
将所有提交从本地存储库发送到远程存储库 | git push |
进行git push 并为当前本地分支设置默认的远程分支。 因此,任何将来的git pull 命令都将尝试将<remote-branch> 中的提交引入当前的本地分支中 | git push -u <remote-branch> |
将更改发送到远程存储库的master分支 | git push origin master |
将特定分支推送到远程存储库 | git push origin <branch_name> |
将所有分支推送到您的远程存储库 | git push --all origin |
Git Checkout和拉
描述 | Git命令 |
---|---|
创建一个新的本地存储库 | git init |
将存储库克隆到新目录 | git clone repo_url |
将存储branch_name 到新目录中,并指向提到的branch_name | git clone --branch branch_name repo_url |
创建本地存储库的工作副本 | git clone /path/to/repository |
从远程存储库下载对象和引用作为master分支 | git fetch origin master |
将另一个分支合并到活动分支中 | git merge <branch_name> |
提取并合并远程服务器上的更改到您的工作目录: | git pull |
查看所有合并冲突,查看与基础文件的冲突,预览合并前的更改 | git diff , git diff --base <filename> , git diff <sourcebranch> <targetbranch> |
git分支
描述 | Git命令 |
---|---|
列出存储库中的所有分支,并告诉您当前所在的分支 | git branch |
从一个分支切换到另一个 | git checkout branch_name |
创建一个新分支并切换到该分支 | git checkout -b branch_name |
从本地存储库删除功能分支 | git branch -d <branch_name> |
删除远程存储库上的分支 | git push origin :<branch_name> |
Git清洁
描述 | Git命令 |
---|---|
从远程服务器获取最新历史记录(对象和引用)以进行主分支 | git fetch origin master |
清理存储库至初始阶段 | git clean -x -d -f |
重置本地存储库,并将本地master分支指向从远程服务器获取的最新历史记录 | git reset --hard origin/master |
将所有更改从远程存储库转移到本地存储库 | git pull origin master |
其他Git命令
描述 | Git命令 |
---|---|
您可以使用标记来标记重要的变更集,例如发布 | git tag 1.0.0 <commitID> |
提交ID是变更集ID的前导字符,最多10个,但必须唯一。 使用获取ID | git log |
将所有标签推送到远程存储库 | git push --tags origin |
如果您搞砸了,可以将工作树中的更改替换为head中的最后一个内容:已经添加到索引中的更改以及新文件将被保留 | git checkout -- <filename> |
在工作目录中搜索foo() | git grep "foo()" |
翻译自: https://www.javacodegeeks.com/2019/01/useful-git-commands.html