Anolis8 Git安装与Git命令

一、Git安装

在你开始使用 Git 前,需要将它安装在你的计算机上。 即便已经安装,最好将它升级到最新的版本。
可以通过软件包或者其它安装程序来安装,或下载源码编译安装。

1.1、通过DNF来进行Git安装
dnf install git-all
git --version //查看 Git 版本
dnf update git //升级 Git 版本
1.2、通过源代码来进行Git安装

有人觉得从源码进行Git安装更实用,因为你能得到最新的版本。 二进制Git安装程序倾向于有一些滞后,当然近几年Git已经成熟,这个差异不再显著。如果你想从源码进行Git安装,需要安装 Git 依赖的库:
autotools、curl、zlib、openssl、expat 和 libiconv。

dnf install dh-autoreconf curl-devel expat-devel gettext-devel openssl-devel perl-devel zlib-devel
如果需要添加文档的多种格式(doc、html、info),需要以下附加的依赖。
dnf install asciidoc xmlto docbook2X
如果你使用基于 RPM 的发行版(Fedora/RHEL/RHEL),你还需要 getopt 包。
dnf install getopt

如果你使用 Fedora/RHEL/RHEL衍生版例如Centos8,那么你需要执行以下命令,以此来解决二进制文件名的不同。

ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi

下载最新版Git服务。

  • Kernel 含有文件下载签名: https://www.kernel.org/pub/software/scm/git
  • GitHub 上的是最新版本: https://github.com/git/git/releases

安装Git服务。

# tar -zxf git-2.9.5.tar.gz -C /usr/src/
# cd /usr/src/git-2.9.5/ 
# make configure
GIT_VERSION = 2.9.5
    GEN configure
# ./configure --prefix=/usr
configure: loading site script /usr/share/config.site
configure: Setting lib to 'lib' (the default)
configure: Will try -pthread then -lpthread to enable POSIX Threads.
configure: CHECKS for site configuration
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for size_t... yes
checking for working alloca.h... yes
checking for alloca... yes
configure: CHECKS for programs
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking for inline... inline
checking if linker supports -R... no
checking if linker supports -Wl,-rpath,... yes
checking for gar... no
checking for ar... ar
checking for gtar... gtar
checking for gnudiff... no
checking for gdiff... no
checking for diff... diff
checking for asciidoc... asciidoc
checking for asciidoc version... asciidoc 8.6.10
Using 'grep -a' for sane_grep
configure: CHECKS for libraries
checking for SHA1_Init in -lcrypto... yes
checking for curl_global_init in -lcurl... yes
checking for Curl_ssl_init in -lcurl... no
checking for curl-config... curl-config
checking for XML_ParserCreate in -lexpat... yes
checking for iconv in -lc... yes
checking for deflateBound in -lz... yes
checking for socket in -lc... yes
checking for inet_ntop... yes
checking for inet_pton... yes
checking for hstrerror... yes
checking for basename in -lc... yes
checking for gettext in -lc... yes
checking libintl.h usability... yes
checking libintl.h presence... yes
checking for libintl.h... yes
configure: CHECKS for header files
checking sys/select.h usability... yes
checking sys/select.h presence... yes
checking for sys/select.h... yes
checking sys/poll.h usability... yes
checking sys/poll.h presence... yes
checking for sys/poll.h... yes
checking for inttypes.h... (cached) yes
checking for old iconv()... no
configure: CHECKS for typedefs, structures, and compiler characteristics
checking for socklen_t... yes
checking for struct itimerval... yes
checking for struct stat.st_mtimespec.tv_nsec... no
checking for struct stat.st_mtim.tv_nsec... yes
checking for struct dirent.d_type... yes
checking for struct passwd.pw_gecos... yes
checking for struct sockaddr_storage... yes
checking for struct addrinfo... yes
checking for getaddrinfo... yes
checking for library containing getaddrinfo... none required
checking whether the platform regex can handle null bytes... yes
checking whether system succeeds to read fopen'ed directory... no
checking whether snprintf() and/or vsnprintf() return bogus value... no
checking whether the platform uses typical file type bits... yes
configure: CHECKS for library functions
checking libgen.h usability... yes
checking libgen.h presence... yes
checking for libgen.h... yes
checking paths.h usability... yes
checking paths.h presence... yes
checking for paths.h... yes
checking libcharset.h usability... no
checking libcharset.h presence... no
checking for libcharset.h... no
checking for strings.h... (cached) yes
checking for locale_charset in -liconv... no
checking for locale_charset in -lcharset... no
checking for clock_gettime... yes
checking for library containing clock_gettime... none required
checking for CLOCK_MONOTONIC... yes
checking for setitimer... yes
checking for library containing setitimer... none required
checking for strcasestr... yes
checking for library containing strcasestr... none required
checking for memmem... yes
checking for library containing memmem... none required
checking for strlcpy... no
checking for uintmax_t... yes
checking for strtoumax... yes
checking for library containing strtoumax... none required
checking for setenv... yes
checking for library containing setenv... none required
checking for unsetenv... yes
checking for library containing unsetenv... none required
checking for mkdtemp... yes
checking for library containing mkdtemp... none required
checking for mkstemps... yes
checking for library containing mkstemps... none required
checking for initgroups... yes
checking for library containing initgroups... none required
checking for getdelim... yes
checking for library containing getdelim... none required
checking for BSD sysctl... no
checking for POSIX Threads with ''... no
checking for POSIX Threads with '-mt'... no
checking for POSIX Threads with '-pthread'... yes
configure: creating ./config.status
config.status: creating config.mak.autogen
config.status: executing config.mak.autogen commands
# make all doc info
# make install install-doc install-html install-info
# install docbook-xsl.css /usr/share/doc/git/docbook-xsl.css
make[1]: Leaving directory '/usr/src/git-2.9.5/Documentation'
make -C Documentation install-info
make[1]: Entering directory '/usr/src/git-2.9.5/Documentation'
make[2]: Entering directory '/usr/src/git-2.9.5'
make[2]: 'GIT-VERSION-FILE' is up to date.
make[2]: Leaving directory '/usr/src/git-2.9.5'
    DB2TEXI gitman.texi
Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/\(\@pxref{ <-- HERE \[(URLS|REMOTES)\]}\)/ at cat-texi.perl line 14.
make[1]: *** [Makefile:375: gitman.texi] Error 255
make[1]: Leaving directory '/usr/src/git-2.9.5/Documentation'
make: *** [Makefile:2376: install-info] Error 2

验证是否安装成功?
git --version //查看 Git 版本
git version 2.9.5
安装后虽然报错了,但是 主体安装成功了。

GIt更新完成后,你可以使用安装好的Git来获取Git安装更新。

git clone git://git.kernel.org/pub/scm/git/git.git
# ll
total 7260
drwxr-xr-x 28 root root   20480 Sep 21 11:21 git

二、Git安装和初始化

如果第一次使用 Git,你可以选择Git全局配置,创建一个新仓库,推送现有的文件夹,或者推送现有的仓库。

2.1、Git全局设置
git config --global user.name "username"
git config --global user.email "[email protected]"
2.2、创建一个新仓库
git clone https://www.urlpath.git
cd urlpath
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
2.3、推送现有文件夹
cd existing_folder
git init
git remote add origin https://www.urlpath.git
git add .
git commit -m "Initial commit"
git push -u origin master
2.4、推送现有的Git仓库
cd existing_repo
git remote rename origin old-origin
git remote add origin https://www.urlpath.git
git push -u origin --all
git push -u origin --tags

三、Git使用流程

Git安装后,一个正常的 Git使用流程,首先从本地提交文件到暂存区,暂存区提交到指定分支,指定分支提交到远程仓库。

3.1、使用Git命令初始化一个仓库
git init //仅第一次使用
3.2、使用Git命令将文件添加到暂存区
git add filename //添加一个文件到暂存区
git add filename filename //添加多个文件到暂存区,文件名用空格隔开
git add *.php //使用通配符 * 来批量提交文件
git add . //将工作区中所有未跟踪或者修改的文件添加到暂存区
git add --all //将工作区中所有未跟踪或者修改的文件添加到暂存区

其中,git add . 和 git add --all 使用区别

1.x版(是否处理删除文件)

git add all 可以提交未跟踪、修改和删除文件。
git add . 可以提交未跟踪和修改文件,但是不处理删除文件。

2.x版(两者功能在提交类型方面是相同的,但是所在目录不同导致差异)

git add all 无论在哪个目录执行都会提交所有相应文件。
git add . 只能够提交当前目录或者它后代子目录下相应文件。
3.3、使用Git命令将暂存区中的内容提交到当前分支
git commit -m "备注信息"
3.4、使用Git命令查看状态
git status
3.5、使用Git命令将当前分支提交到远程仓库
git push origin master

四、常用的Git命令汇总

按照之前操作,已经完成了 Git安装、初始化以及基本操作流程,但是仅掌握这些命令在实际生产环境是远远不够的,下面 试试吧 总结了一些简洁的常用的Git命令,方便大家使用。

4.1、使用Git命令本地暂存提交到远程
git add .
git commit -m "提交代码"
git push
或者
git push -u origin master
或者强制提交
git push -u origin master -f
4.2、使用Git命令远程覆盖本机暂存
git fetch --all
git reset --hard origin/master
4.3、使用Git命令本地回退到指定版本
git reflog //先用下面命令找到要回退的版本的commit id:
git reset --hard 92168c9f //接着回退版本92168c9f 就是你要回退的版本的commit id的前面几位
git push -f origin master //最后一步, 强制推送到远程
4.4、使用Git命令删除本地分支和远程分支
git branch -D 分支名字 //删除本地分支
git push origin :分支名字 //删除远程分支
4.5、合并develop分支到master主分支
第一步,develop分支已经开发完并提交完毕
git  add .
git  commit -m '备注信息'
git push -u origin develop
第二步,切换到master
git checkout master
第三步,将远程master上的代码pull下来
git pull origin master
第四步,将 develop 分支的代码合并到 master 上
git merge develop
第五步,查看状态
git status
第六步,如果存在未commit的代码,则使用如下命令
git push origin master
第七步,再次使用提交状态
git status 
4.6、使用Git命令标签操作
建立新标签并推送到远程
git tag //列出所有标签
git tag -a tagName -m "备注信息" //提交一个标签
git push origin tagName //推送一个本地标签
为指定历史版本打上标签
git tag 标签名称 版本号 //例如:git tag v1.0 f52c633
删除本地标签
git tag -d 标签名称
删除远端标签
git tag push origin :refs/tags/标签名称
推送全部未推送过的本地标签
git push origin --tags
显示标签的说明信息
git show 标签名称
4.7、使用Git命令撤销修改
git checkout -- filename //将filename文件就回到最近一次git commit或git add时的状态
git status //查看状态

转自:https://try8.cn/article/10007

你可能感兴趣的:(Anolis8 Git安装与Git命令)