Git基本使用

基本使用

[root@testwxw01 ~]# git config --global user.name "wuxw"
[root@testwxw01 ~]# git config --global user.email "15234411774@163.com"
[root@testwxw01 ~]# git config --global color.ui true

创建一个本地仓库

[root@testwxw01 ~]# mkdir demo
[root@testwxw01 ~]# cd demo/

初始化git的仓库目录

[root@testwxw01 demo]# git init
Initialized empty Git repository in /root/demo/.git/

创建文件

[root@testwxw01 demo]# touch file{1..3}
[root@testwxw01 demo]# git status

提交暂存区

[root@testwxw01 demo]# git add .
[root@testwxw01 demo]# git status

提交到仓库

[root@testwxw01 demo]# git commit -m "新增文件3个"

如果更改了文件 需再次提交

[root@testwxw01 demo]# git add . #提交到暂存区
[root@testwxw01 demo]# git commit -m "修改文件1" #提交到仓库

git如何改名提交仓库?

[root@testwxw01 demo]# git mv file file1 #改名字
[root@testwxw01 demo]# git commit -m "修改file为file1"

如何比对工作目录和暂存文件内容和本地仓库文件内容的差异?

[root@testwxw01 demo]# git diff file1 #查看区别的 比对本地与暂存区的区别
[root@testwxw01 demo]# git add . #提交暂存区就会一致,但是暂存区与仓库还不一致

[root@testwxw01 demo]# git diff --cached file1 #比对缓存区与仓库的区别
[root@testwxw01 demo]# git commit -m "增加"

如何提交查看已提交的commit

[root@testwxw01 demo]# git log
[root@testwxw01 demo]# git log --oneline
[root@testwxw01 demo]# git log -1(123)

回退

git commit保存后的状态如何回退?

如果本地工作目录修改文件错误想回退,不小心文件内容清空了,暂存区回退

[root@testwxw01 demo]# git status
[root@testwxw01 demo]# git checkout -- file1

已经提交到暂存区想撤销

[root@testwxw01 demo]# > file1
[root@testwxw01 demo]# git add .
[root@testwxw01 demo]# git status
[root@testwxw01 demo]# git reset HEAD file1 #回退到暂存区
[root@testwxw01 demo]# git checkout -- file1 #回退到本地仓库

如果多次提交到本地仓库想回退某个版本(用ID号)

[root@testwxw01 demo]# cat file1 #原始
111
123
[root@testwxw01 demo]# echo 456 >> file1
[root@testwxw01 demo]# cat file1
111
123
456
[root@testwxw01 demo]# git add .
[root@testwxw01 demo]# git commit -m "add456" #第一次
[master 69d6059] add456
1 file changed, 1 insertion(+)
[root@testwxw01 demo]# echo 789 >> file1
[root@testwxw01 demo]# git add .
[root@testwxw01 demo]# git commit -m "add789" #第二次
[master 66220d6] add789
1 file changed, 1 insertion(+)

想回到原始的123456

[root@testwxw01 demo]# git log --oneline #查看ID号
66220d6 add789
69d6059 add456
2c74aaa add123
[root@testwxw01 demo]# git reset --hard 2c74aaa(ID)
[root@testwxw01 demo]# cat file1
111
123

如果不太对又想回到456

[root@testwxw01 demo]# git reflog #查看所有的历史纪录
66220d6 HEAD@{1}: commit: add789
69d6059 HEAD@{2}: commit: add456
[root@testwxw01 demo]# git reset --hard 69d6059
[root@testwxw01 demo]# cat file1
111
123
456

分支管理

列出所有本地分支

$ git branch

列出所有远程分支

$ git branch -r

列出所有本地分支和远程分支

$ git branch -a

新建一个分支,但依然停留在当前分支

$ git branch [branch-name]

新建一个分支,并切换到该分支

$ git checkout -b [branch]

新建一个分支,指向指定commit

$ git branch [branch] [commit]

新建一个分支,与指定的远程分支建立追踪关系

$ git branch --track [branch] [remote-branch]

切换到指定分支,并更新工作区

$ git checkout [branch-name]

切换到上一个分支

$ git checkout -

建立追踪关系,在现有分支与指定的远程分支之间

$ git branch --set-upstream [branch] [remote-branch]

合并指定分支到当前分支

$ git merge [branch]

git merge master
git merge master -m "he-bin"

选择一个commit,合并进当前分支

$ git cherry-pick [commit]

删除分支

$ git branch -d [branch-name]

删除远程分支

$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

本地存在一个分支,名称叫:develop_chen,但远程没有怎么办?

git push origin develop_chen

这样就在远程建立一个和本地一样的分支
git branch --set-upstream-to=origin/develop develop 本地分支和远程分支简历跟踪关系

标签

列出所有tag

$ git tag

新建一个tag在当前commit

$ git tag [tag]
git tag -a "v1.0" -m "项目发布"

新建一个tag在指定commit

$ git tag [tag] [commit]
git tag "v2.0" 9b46b1e -m "file1"

删除本地tag

$ git tag -d [tag]
git tag -d v2.0

删除远程tag

$ git push origin :refs/tags/[tagName]

查看tag信息

$ git show [tag]

提交指定tag

$ git push [remote] [tag]

提交所有tag

$ git push [remote] --tags

新建一个分支,指向某个tag

$ git checkout -b [branch] [tag]

git远程仓库

Git 全局设置:

git config --global user.name "武兴旺"
git config --global user.email "15234411774@163.com"

创建 git 仓库:

mkdir wuxw
cd wuxw
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/wu_xingwang/wuxw.git # https协议的需要用户名密码登录
git remote add origin git@gitee.com:wu_xingwang/wuxw.git # ssh协议的需要讲本机的公钥上传到远程仓库中
git push -u origin master

已有仓库?

cd existing_git_repo
git remote add origin https://gitee.com/wu_xingwang/wuxw.git # https协议的需要用户名密码登录
git remote add origin git@gitee.com:wu_xingwang/wuxw.git # ssh协议的需要讲本机的公钥上传到远程仓库中
git push -u origin master

[root@testwxw01 demo]# git remote -v #查看远程地址
设置计算机密钥
[root@testwxw01 demo]# ssh-keygen
[root@testwxw01 demo]# cat ~/.ssh/id_rsa.pub #公钥加密 私钥解密

删除远程仓库

[root@testwxw01 demo]# git remote remove origin

关联远程仓库

[root@testwxw01 demo]# git remote add origin git@gitee.com:wu_xingwang/wuxw.git

推送本地仓库到远程仓库

[root@testwxw01 demo]# git add .
[root@testwxw01 demo]# git commit -m "new file"
[root@testwxw01 demo]# git push -u origin master
然后远程仓库就会有文件了

新员工进项目直接克隆

[root@testwxw01 demo]# git clone git@gitee.com:wu_xingwang/wuxw.git

如何查看别人新提交的代码,跟新一下

[root@testwxw01 demo]# git pull origin master

参考:https://www.cnblogs.com/chenwolong/p/GIT.html

本文来自博客园,作者:武兴旺,转载请注明原文链接:https://www.cnblogs.com/wuxingwang/p/13530795.html

原文地址:https://www.cnblogs.com/wuxingwang/p/13530795.html