上传代码
git add *
推送暂存区
git commit -m “标记”
推送代码仓库
git push origin 分支名
创建文件模拟代码提交到仓库
1.在testgit目录下创建一个测试文件test.txt
[root@client ~]# cd testgit/
[root@client testgit]# vim test.txt #随便写点东西2.把文件添加到暂存区:使用 "git add" 建立跟踪
[root@client testgit]# git add test.txt
注: 这里可以使用 git add * 或者 git add -A3.提交文件到仓库分支:
[root@client testgit]# git commit -m "test1"
[master (root-commit) 2b51ff9] test1
1 file changed, 2 insertions(+)
create mode 100644 test.txt
-m:描述
4.查看git状态:
[root@client testgit]# git status
# On branch master #分支位于master
5.修改文件后再此查看状态:
[root@client testgit]# echo '1122334' >> test.txt
[root@client testgit]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: readme.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit "
6.先add
[root@client testgit]# git add -A
8.再次提交commit:
[root@client testgit]# git commit -m "add2"
[master 73bf688] add2
1 file changed, 1 insertion(+)
[root@client testgit]# git status
# On branch master
nothing to commit, working directory clean
版本回退
已经提交了不合适的修改到版本库时,想要撤销本次提交,使用版本回退,不过前提是没有推送到远程库。
查看现在的版本:
[root@client testgit]# git log
显示的哪个版本在第一个就是当前使用的版本。
版本回退(切换):在Git中,上一个版本就是HEAD^,当然往上100个版本写100个比较容易数不过来,所以写成HEAD~100(一般使用id号来恢复)
回到上一个版本
[root@client testgit]# git reset --hard HEAD^
HEAD is now at 0126755 test1
2.回到指定的版本(根据版本号):
[root@client testgit]# git reset --hard dd66ff
HEAD is now at dd66ff9 add2
==========================================================
注:消失的ID号:
回到早期的版本后再查看git log会发现最近的版本消失,可以使用reflog查看消失的版本ID,用于回退到消失的版本
[root@vm20 gittest]# git reflog
2a85982 HEAD@{0}: reset: moving to 2a859821a2385e136fe83f3a206b287eb0eb8c18
f5bc8c1 HEAD@{1}: commit: test-version2
2a85982 HEAD@{2}: commit (initial): test-version1[root@git-client testgit]# git reset --hard f5bc8c1
删除文件
从工作区删除test.txt,并且从版本库一起删除
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
工作区:
[root@client testgit]# touch test.txt
[root@client testgit]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# qf.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@client testgit]# rm -rf test.txt 未添加到暂存区,可直接删除
[root@client testgit]# git status
# On branch master
nothing to commit, working directory clean+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
已从工作区提交到暂存区:
第一种方法
[root@client testgit]# touch test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)[root@client testgit]# git add test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.txt
#[root@client testgit]# git rm --cache test.txt #从暂存区移除
rm 'test.txt'
[root@client testgit]# ls
test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@client testgit]# rm -rf test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)第二种方法:
[root@client testgit]# touch b.txt
[root@client testgit]# git add b.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: b.txt
#
[root@client testgit]# git rm -f b.txt
rm 'b.txt'
[root@client testgit]# ls
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
直接在暂存区rm掉文件,如何解决
[root@client testgit]# touch c.txt
[root@client testgit]# git add c.txt
[root@client testgit]# ls
c.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: c.txt
#
[root@client testgit]# rm -rf c.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: c.txt
#
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: c.txt
#
[root@client testgit]# git rm --cache c.txt
rm 'c.txt'
[root@client testgit]# ls
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@client testgit]#
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
修改文件
暂存区修改名称
[root@client testgit]# touch a.txt
[root@client testgit]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@client testgit]# git add a.txt
[root@client testgit]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: a.txt
#
[root@client testgit]# git mv a.txt d.txt
[root@client testgit]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: d.txt
#
[root@client testgit]# ls
d.txt test.txt
[root@client testgit]# git rm --cache d.txt
[root@client testgit]# rm -rf d.txt
将代码上传到仓库的master分支
[root@client testgit]# vi a.txt #创建一个新文件
hello world
[root@client testgit]# git add a.txt
[root@client testgit]# git commit -m "add"
[root@client testgit]# git push origin master #上传到中心仓库master分支
Counting objects: 11, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (11/11), 828 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@192.168.246.214:/git-test/testgit/
* [new branch] master -> master
测试:
在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件
[root@client testgit]# cd
[root@client ~]# rm -rf testgit/
[root@client ~]# git clone git@192.168.246.214:/git-test/testgit/
Cloning into 'testgit'...
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (11/11), done.
[root@client ~]# cd testgit/
[root@client testgit]# ls
a.txt
[root@client testgit]# cat a.txt
hello world
Gitlab基本命令
git add * 提交更改到暂存区
git commit -m "" 描述提交
git push origin 分支名 #推送本地代码到远程仓库
git pull origin 分支名 #拉取最新代码
git status 查看本地代码状态
git checkout 分支名 #切换到分支
git checkout -b 分支名 #切换到分支,如果不存在则创建
git version #查看git版本
git log #查看历史提交
git branch #查看分支列表
git merge 分支1 #把分支1合并到当前分支
git tag -a "V1" -m "描述" #打标签