Git入门操作

版权声明:皆为本人原创,复制必究 https://blog.csdn.net/m493096871/article/details/88924731

参考

https://git-scm.com/book/zh/v2

yum install git -y

创建一个目录(Git仓库)

mkdir gitdemo

cd gitdemo

[root@foundation11 gitdemo]# git init

[root@foundation11 gitdemo]# echo file1 > file1
[root@foundation11 gitdemo]# git add file1
[root@foundation11 gitdemo]# git status -s
A  file1

 git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

[root@foundation11 gitdemo]# git config --global user.mail "[email protected]"
[root@foundation11 gitdemo]# git config --global user.name hzp

[root@foundation11 gitdemo]# git commit -m "add file1"
# On branch master
nothing to commit, working directory clean
[root@foundation11 gitdemo]# git status -s


因为之前提交过了 所以没有状态

修改状态

[root@foundation11 gitdemo]# git status -s
 M file1
[root@foundation11 gitdemo]# echo file1>> file1
[root@foundation11 gitdemo]# git status -s
 M file1
[root@foundation11 gitdemo]# git add file1
[root@foundation11 gitdemo]# git status -s
M  file1
[root@foundation11 gitdemo]# echo file1>> file1
[root@foundation11 gitdemo]# git status -s
MM file1


两个M说明提交后在缓冲区进行了修改

要再次提交

[root@foundation11 gitdemo]# git add file1
[root@foundation11 gitdemo]# git status -s
M  file1

[root@foundation11 gitdemo]# git commit -m "add file1"
[master aef1b8d] add file1
 Committer: hzp <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 3 insertions(+)

[root@foundation11 gitdemo]# l.
.  ..  .git

[root@foundation11 gitdemo]# vim .gitignore

.*
[root@foundation11 gitdemo]# git status -s
[root@foundation11 gitdemo]# l.
.  ..  .git  .gitignore

修改文件后还原文件

[root@foundation11 gitdemo]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   file1
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@foundation11 gitdemo]# git checkout -- file1
[root@foundation11 gitdemo]# cat file1
file1
file1
file1
file1


查看日志

[root@foundation11 gitdemo]# git log
commit aef1b8d45c5d8d3692b6c4866480b17101c0ff4b
Author: hzp <[email protected]>
Date:   Sun Mar 31 10:48:01 2019 +0800

    add file1

commit 5991ab4dc29acb187c83533f580141be9c9140f6
Author: hzp <[email protected]>
Date:   Sun Mar 31 10:37:44 2019 +0800

    add file1

commit 02661c76cfdf312c3d52849b517b2434090a1f68
Author: root <[email protected]>
Date:   Sun Mar 31 10:32:02 2019 +0800

    add file1

git log --pretty=oneline

git reflog

[root@foundation11 gitdemo]# git reflog
aef1b8d HEAD@{0}: commit: add file1
5991ab4 HEAD@{1}: commit: add file1
02661c7 HEAD@{2}: commit (initial): add file1
[root@foundation11 gitdemo]# git log --pretty=oneline
aef1b8d45c5d8d3692b6c4866480b17101c0ff4b add file1
5991ab4dc29acb187c83533f580141be9c9140f6 add file1
02661c76cfdf312c3d52849b517b2434090a1f68 add file1

版本回滚

git reset --hard HEAD^

HEAD is now at 5991ab4 add file1
[root@foundation11 gitdemo]# git reflog^C
[root@foundation11 gitdemo]# cat file1
file1

也可指定

[root@foundation11 gitdemo]# git reflog
5991ab4 HEAD@{0}: reset: moving to HEAD^
aef1b8d HEAD@{1}: commit: add file1
5991ab4 HEAD@{2}: commit: add file1
02661c7 HEAD@{3}: commit (initial): add file1
[root@foundation11 gitdemo]# git reset --hard 5991ab4
HEAD is now at 5991ab4 add file1

误删

rm -fr file1

git checkout --file1

提交删除

git rm file1

git commit -m "del file1"

也可以返回

git reflog  找到删除之前的id

git reset --hard   86e1f9b

猜你喜欢

转载自blog.csdn.net/m493096871/article/details/88924731