- yum install -y git
-
- git --version
-
- [root@git-server ~]# git --help
- usage: git [--version] [--help] [-c name=value]
- [--exec-path[=
]] [--html-path] [--man-path] [--info-path] - [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
- [--git-dir=
] [--work-tree=] [--namespace=] - <command> [
] -
- The most commonly used git commands are:
- add Add file contents to the index
- bisect Find by binary search the change that introduced a bug
- branch List, create, or delete branches
- checkout Checkout a branch or paths to the working tree
- clone Clone a repository into a new directory
- commit Record changes to the repository
- diff Show changes between commits, commit and working tree, etc
- fetch Download objects and refs from another repository
- grep Print lines matching a pattern
- init Create an empty Git repository or reinitialize an existing one
- log Show commit logs
- merge Join two or more development histories together
- mv Move or rename a file, a directory, or a symlink
- pull Fetch from and merge with another repository or a local branch
- push Update remote refs along with associated objects
- rebase Forward-port local commits to the updated upstream head
- reset Reset current HEAD to the specified state
- rm Remove files from the working tree and from the index
- show Show various types of objects
- status Show the working tree status
- tag Create, list, delete or verify a tag object signed with GPG
-
- 'git help -a' and 'git help -g' lists available subcommands and some
- concept guides. See 'git help
' or 'git help ' - to read about a specific subcommand or concept.
git 应用:
git身份设置:
因为git是分布式版本控制系统,不同的人提交的代码需要分区,所以每个人都要设置一个身份标志。如果不设置的话,谁会知道你是谁呢?
- [root@git-server ~]# git config --global user.name "changchunhua"
- [root@git-server ~]# git config --global user.email "chang_chunhua@qq.com"
- [root@git-server ~]# git config --global color.ui true
- [root@git-server ~]#
- [root@git-server ~]# git config --list
- user.name=changchunhua
- user.email=chang_chunhua@qq.com
- color.ui=true
创建本地仓库:
工作目录,work directory,也叫工作区,是存放项目代码文件的一个目录。
仓库(repository),也叫版本库,在git init命令初始化工作目录后会产生一个隐藏的子目录.git,可以将其理解为git的仓库或版本库。
仓库分为本地仓库和远程仓库。


创建本地仓库的步骤:
1)创建工作目录:

- [root@git-server GitTest]# ls .git
- branches config description HEAD hooks info objects refs
小结:mkdir创建一个目录后,然后cd进取,使用git init命令就创建好了本地仓库,开发者就可以在工作目录里开发项目代码文件了。
暂存区:
暂存区 index、stage,也叫缓存区。
暂存区就看住是一个缓区区域,临时保存你的改动。
如果在工作目录中创建了一个新文件,需要将新文件添加到暂存区。
添加文件到暂存区:
1、准备一个文件:
- [root@git-server GitTest]# cat 1.py
- print("hello world")
-
- [root@git-server GitTest]# git add 1.py
-
- [root@git-server GitTest]# ls .git
- branches config description HEAD hooks index info objects refs
-
- [root@git-server GitTest]# strings .git/index
- DIRC
- 1.py
1)提交版本(第1个版本):
代码文件commit提交后才能纳入版本控制。
1)可以使用git status查看工作目录里有哪些文件需要提交。
- [root@git-server GitTest]# git status
- # On branch master
- #
- # Initial commit
- #
- # Changes to be committed:
- # (use "git rm --cached
..." to unstage) - #
- # new file: 1.py
- #
2)使用git commit -m 后提交的说明信息:
- [root@git-server GitTest]# git commit -m "commit 1.py"
- [master (root-commit) 0eb70c9] commit 1.py
- 1 file changed, 1 insertion(+)
- create mode 100644 1.py
-
-
- [root@git-server GitTest]# git status
- # On branch master
- nothing to commit, working directory clean
-
再查看git status,就没有文件可以提交的了。
3)修改再提交(第二个版本):
- [root@git-server GitTest]# vim 1.py
- [root@git-server GitTest]#
- [root@git-server GitTest]#
- [root@git-server GitTest]# git status
- # On branch master
- # Changes not staged for commit:
- # (use "git add
..." to update what will be committed) - # (use "git checkout --
..." to discard changes in working directory) - #
- # modified: 1.py
- #
- no changes added to commit (use "git add" and/or "git commit -a")
- [root@git-server GitTest]# git add 1.py
- [root@git-server GitTest]#
- [root@git-server GitTest]#
- [root@git-server GitTest]# git commit -m "Add a new line hello linux in 1.py"
- [master 32150cd] Add a new line hello linux in 1.py
- 1 file changed, 1 insertion(+)
小结:
工作目录中写好的代码文件需要git add 文件名添加到暂存区,再git commit 文件名提交,以后每次修改都要重复前面的步骤。
git status是查看工作目录中的状态;
git diff 文件名是查看修改了什么;
查看提交历史:
1、使用git log查看提交的历史信息:
- [root@git-server GitTest]# git log
- commit 32150cd35a9ccd888eff433068e54d776a24f7fb
- Author: changchunhua
- Date: Tue Sep 19 10:10:24 2023 +0800
-
- Add a new line hello linux in 1.py
-
- commit 22aba45892ccd930af92ae0ca12fdc261e035979
- Author: changchunhua
- Date: Tue Sep 19 10:09:26 2023 +0800
-
- Add a new line hello python in 1.py
-
- commit 0eb70c9e0fcc649cefab4577f2ee51f2146ccaca
- Author: changchunhua
- Date: Tue Sep 19 10:05:17 2023 +0800
-
- commit 1.py
使用git log --pretty=oneline:
- [root@git-server GitTest]# git log --pretty=oneline
- 32150cd35a9ccd888eff433068e54d776a24f7fb Add a new line hello linux in 1.py
- 22aba45892ccd930af92ae0ca12fdc261e035979 Add a new line hello python in 1.py
- 0eb70c9e0fcc649cefab4577f2ee51f2146ccaca commit 1.py
版本回退和还原:
1, 使用git reset --hard HEAD^回退到上一个版本(也就是第2个版本)
- [root@git-server GitTest]# git reset --hard HEAD^
- HEAD is now at 22aba45 Add a new line hello python in 1.py
- [root@git-server GitTest]#
- [root@git-server GitTest]#
- [root@git-server GitTest]# cat 1.py
- print("hello world")
- print("hello python")
2、使用git reset --hard 第三个版本号还原到第三个版本:
- [root@git-server GitTest]# git reflog
- 22aba45 HEAD@{0}: reset: moving to HEAD^
- 32150cd HEAD@{1}: commit: Add a new line hello linux in 1.py
- 22aba45 HEAD@{2}: commit: Add a new line hello python in 1.py
- 0eb70c9 HEAD@{3}: commit (initial): commit 1.py
- [root@git-server GitTest]#
-
- [root@git-server GitTest]# git reset --hard 32150cd
- HEAD is now at 32150cd Add a new line hello linux in 1.py
-
- [root@git-server GitTest]# cat 1.py
- print("hello world")
- print("hello python")
- print("hello linux")
-
另外:
回退到上上一个版本, 也就是回退两个版本,使用git reset --hard HEAD^^
回退三个版本,使用git reset --hard HEAD^^^, 以此类推。
如果回退100个版本,那用100个^符号不方便,可以换成git reset --hard HEAD~100
之前,学习的时候没有特别注意这块内容。还是需要加强。
小结:
提交后的代码文件,使用
git log查看当前版本及以前的历史版本。使用
git reset --hard HEAD^或者git reset --hard HEAD~100实现版本回退。使用
git reflog查看提交的所有操作及版本号使用
git reset --hard 版本号你可以自由的在不同版本之间来回切换。
git工作流再次理解与应用拓展:
工作目录中任何修改或增加的文件,都要git add到暂存区,让暂存区和工作目录的状态一致,这样才能提交一个版本。
git commit提交的是在暂存区里的所有文件状态。也就是说整个工作目录里的状态保存为一个版本,而不是某一个文件。
git版本控制不仅仅用于项目开发,你也可以用于一个软件包仓库的版本控制。
撤销修改:
开发者想撤销一段代码:
- [root@vm1 GitTest]# cat 1.py
- print("hello world")
- print("hello python")
- print("hello linux")
- print("hey,xxx is a gay") 这是写错的代码,需要反悔
想要撤销有以下几种方法:
1)直接把写错的代码删除;
2)使用git checkout -- 文件名 就可以直接撤销修改了。
- [root@git-server GitTest]# git checkout -- 1.py
- [root@git-server GitTest]#
- [root@git-server GitTest]#
- [root@git-server GitTest]# cat 1.py
- print("hello world")
- print("hello python")
- print("hello linux")
3)如果写乱了代码,添加到暂存区但是还没有commit提交,使用git reset HEAD 文件名取消暂存区添加,再git checkout -- 文件名 来撤销修改。
4)如果写乱了代码,添加到暂存区并提交了,则使用版本回退。
误删恢复:
1, 只要文件git add到了暂存区, 无论有没有git commit提交。误删除后都可以使用 git checkout -- 文件名来恢复。
- [root@git-server GitTest]# git add 2.py
- [root@git-server GitTest]#
- [root@git-server GitTest]# rm -f 2.py
- [root@git-server GitTest]# ls
- 1.py
- [root@git-server GitTest]# git checkout -- 2.py
- [root@git-server GitTest]# ll
- total 8
- -rw-r--r-- 1 root root 64 Sep 19 11:19 1.py
- -rw-r--r-- 1 root root 19 Sep 19 11:25 2.py
2、如果文件没有git add 到暂存区,误删除了也就没有了。
文件删除:
1、没有git add到暂存区的文件,直接rm删除就可以了。
2、git add 添加到暂存区,但是还没有git commit提交的文件,需要rm删除本地,还要git rm 文件名删除。
- [root@git-server GitTest]# vim 3.py
- [root@git-server GitTest]#
- [root@git-server GitTest]# git add 3.py
- [root@git-server GitTest]#
- [root@git-server GitTest]# rm -f 3.py
- [root@git-server GitTest]#
- [root@git-server GitTest]# git rm 3.py
- rm '3.py'
3、git add 添加到暂存区,并且已经git commit 提交的文件,需要rm 删除本地文件,在git rm文件名删除,最后再提交删除。
- [root@git-server GitTest]# touch 3.py
- [root@git-server GitTest]# git add 3.py
- [root@git-server GitTest]# git commit -m "committed 3.py"
- [master 99a16a7] committed 3.py
- 2 files changed, 2 insertions(+)
- create mode 100644 2.py
- create mode 100644 3.py
- [root@git-server GitTest]# git status
- # On branch master
- nothing to commit, working directory clean
- [root@git-server GitTest]# rm 3.py -f
- [root@git-server GitTest]#
- [root@git-server GitTest]# git rm 3.py
- rm '3.py'
- [root@git-server GitTest]# git commit -m "deleted 3.py"
- [master 8153d25] deleted 3.py
- 1 file changed, 1 deletion(-)
- delete mode 100644 3.py
git版本控制小结:
