git使用介绍

时间:2022-07-22
本文章向大家介绍git使用介绍,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

git的简单介绍

git是一类分布式版本控制系统, 客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来。这么一来,任何一处协同工作用的服务器发生故障,事后都可以用任何一个镜像出来的本地仓库恢复。因为每一次的提取操作,实际上都是一次对代码仓库的完整备份

运行git前的配置

1 2 3 4 5 6 7 8 9

[root@localhost /git]# git config --global user.name "fei" [root@localhost /git]# git config --global user.email fei@devilf.cc [root@localhost /git]# git config --list user.name=fei user.email=fei@devilf.cc core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true

1、首先是在一个目录中初始化仓库

1 2

mkdir /git git init

#查看会有一个.git的隐藏目录出现

2、从现有仓库克隆

<code>git clone </code><a href="git://git.kernel.org/pub/scm/git/git.git"><code>git://git.kernel.org/pub/scm/git/git.git</code></a>

如果是想改名的话,可以

<code>git clone </code><a href="git://git.kernel.org/pub/scm/git/git.git"><code>git://git.kernel.org/pub/scm/git/git.git</code></a><code> chenfei</code>

3、检查当前文件状态

1 2 3 4 5 6 7 8 9 10 11 12

# git status On branch master No commits yet Untracked files:   (use "git add &lt;file>..." to include in what will be committed)         chenfei/ nothing added to commit but untracked files present (use "git add" to track)

执行此命令会知道处于哪个分支,如果当前分支下有处于未跟踪的新文件,也会被列出,像上面的提示就是 git/ 这个目录下的文件是处于未跟踪状态的。

4、跟踪新的文件

<code>git add chenfei</code>

再次查看状态,会发现该目录已经处于跟踪状态,并且已经被放在暂存区中去了

<code># git status</code>



On branch master






No commits yet






Changes to be committed:



  (use &#8220;git rm &#8211;cached <file>&#8230;" to unstage)






        new file:   chenfei






Untracked files:



  (use &#8220;git add <file>&#8230;" to include in what will be committed)






        install.log

5、忽略某些文件

因为有时候有些文件是不需要纳入版本控制的,例如一些文件的副本文件等,这个时候就需要使用 .gitignore来编写略过哪些文件

# 此为注释 – 将被 Git 忽略



# 忽略所有 .a 结尾的文件



*.a



# 但 lib.a 除外



!lib.a



# 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO



/TODO



# 忽略 build/ 目录下的所有文件



build/



# 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt



doc/*.txt



# 忽略 doc/ 目录下所有扩展名为 txt 的文件



doc/**/*.txt

6、查看尚未加入暂存区的文件都修改了哪些内容

<code>git diff install.log</code>








<code>#  git diff install.log</code>



diff &#8211;git a/install.log b/install.log



index 7fcbbba..4974cc6 100644



&#8212; a/install.log



+++ b/install.log



@@ -4,3 +4,4 @@



#add str two



#add num and str 1 two



+#del num adn str

7、提交更新

<code>git commit</code>








<code># git commit -m "open source" chenfei</code>



[master a19a408] open source



1 file changed, 1 insertion(+)



create mode 160000 chenfei

8、移除文件

从仓库中删除,但本地目录中的文件不删除,也就是只从暂存区中删除,

<code>git rm --cached install.log</code>








<code># git rm --cached install.log</code>



rm &#8216;install.log&#8217;



[root@localhost /git]# ll



total 24



drwxr-xr-x. 25 root root 20480 Jul 26 16:49 chenfei



-rw-r&#8211;r&#8211;.  1 root root    80 Jul 26 17:01 install.log



[root@localhost /git]# git status



On branch master



Changes to be committed:



  (use &#8220;git reset HEAD <file>&#8230;" to unstage)






        deleted:    install.log






Untracked files:



  (use &#8220;git add <file>&#8230;" to include in what will be committed)






        install.log

可以看到有明显的变化

9、移动文件

<code>git mv </code>








<code># git mv chenfei cf</code>








# git status



On branch master



Changes to be committed:



  (use &#8220;git reset HEAD <file>&#8230;" to unstage)






        renamed:    chenfei -> cf



        deleted:    install.log






Untracked files:



  (use &#8220;git add <file>&#8230;" to include in what will be committed)






        install.log

执行git mv命令实际上是执行了三条命令

<code>mv chenfei cf</code>



<code>git rm chenfei</code>



<code>git add cf</code>

10、查看历史

<code>git log</code>








<code># git log</code>



commit a19a408f42f4e99a84929252d4b939dbc4fb3300 (HEAD -> master)



Author: fei <<a href="mailto:fei@devilf.cc">fei@devilf.cc</a>>



Date:   Thu Jul 26 17:09:01 2018 +0800






    open source






commit d70d0f47540527f29a23db665d76224bf4e12835



Author: root <<a href="mailto:root@localhost.localdomain">root@localhost.localdomain</a>>



Date:   Thu Jul 26 17:04:23 2018 +0800






    first cm

这样会按照时间列出所有的更新,但是如果有非常多的提交的话,我们可以显示最近的几次几次提交

<code>git log -p -3</code>

这样可以显示最近的三次提交

其他的一些参数:

#仅显示简要的增改行数统计



<code>git log --stat</code>

11、撤销操作

1)假如我们在暂存区放了一个文件后,发现加错了,这个时候我们需要移除放在暂存区的文件,方法:

<code># git reset HEAD test.sh</code>



Unstaged changes after reset:



M       test.sh



[root@localhost /git]# git status



On branch master



Changes to be committed:



  (use &#8220;git reset HEAD <file>&#8230;" to unstage)






        renamed:    chenfei -> cf






Changes not staged for commit:



  (use &#8220;git add <file>&#8230;" to update what will be committed)



  (use &#8220;git checkout &#8212; <file>&#8230;" to discard changes in working directory)






        modified:   test.sh

现在暂存区中已经不存在该文件了。

2)取消对文件的修改

如果觉得对某个文件的修改没有必要,则需要取消修改,方法:

<code>git checkout -- test.sh</code>