概述:
工作区:workspace ,即 .git 目录的父目录,文件所在目录
暂存库:index ,.git目录中的一块区域,用于记录所有工作区的变换,并暂存,(即:git add 提交修改到git,未 commit 到分支)
当前分支:repository,.git目录中的一块区域,本地仓库,存放版本
一 配置
git config --global user.email "sl"
git config --global user.name "sl"
|
二 本地仓库
1)创建版本库
在本地创建目录,进入该目录,调用git init 即创建本地仓库,完成后文件夹下会出现 .git 目录
[root@gittest ~]# mkdir gittest
[root@gittest ~]# cd gittest/
[root@gittest gittest]# git init
Initialized empty Git repository in /root/gittest/.git/
|
2)增加文件,提交
(a)在工作区添加修改文件,直接编辑修改即可
echo ’testgit’ >> README.md
|
(b)添加到暂存库(index): git add
git add README.md
|
(c)查看工作区情况: git status
[root@gittest gittest]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#
|
(d)删除添加: git rm —cached
[root@gittest gittest]# git rm --cached README.md
rm 'README.md'
[root@gittest gittest]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# README.md
nothing added to commit but untracked files present (use "git add" to track)
|
(e)提交 : git commit
[root@gittest gittest]# git add README.md
[root@gittest gittest]# git commit README.md
[master (root-commit) fdfa139] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
|
期间会出现vi编辑器,添加comment,wq即可
3)工作区回退到最近一次 commit 或 add 修改: git checkout --
[root@gittest gittest]# cat README.md
gittest
[root@gittest gittest]# echo 'test redo' >> README.md
[root@gittest gittest]# cat README.md
gittest
test redo
[root@gittest gittest]# git checkout -- README.md
[root@gittest gittest]# cat README.md
gittest
|
注:撤销的两种情况:
(a)修改还没有被放到暂存区,现在,撤销回到版本库的状态
(b)修改已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
注2:必须带 -- 否则是切换分支命令
4)回退到某个提交的版本:git reset --hard 版本号
注:版本可用 HEAD 替代 :上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
(a)查看命令操作日志:git reflog
[root@gittest gittest]# git reflog
3ecc7a9 HEAD@{0}: commit: add date
fdfa139 HEAD@{1}: commit (initial): first commit
|
(b)回退
[root@gittest gittest]# git reset --hard fdfa139
HEAD is now at fdfa139 first commit
[root@gittest gittest]# git reflog
fdfa139 HEAD@{0}: reset: moving to fdfa139
3ecc7a9 HEAD@{1}: commit: add date
fdfa139 HEAD@{2}: commit (initial): first commit
|
5)删除文件提交: git rm
在工作区删除后文件,需要通知git 在仓库中删除,方法 git rm
[root@gittest gittest]# rm delete.file
rm: remove regular file 'delete.file'? y
[root@gittest gittest]# git status
# On branch master
# 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: delete.file
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git rm delete.file
rm 'delete.file'
[root@gittest gittest]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: delete.file
#
[root@gittest gittest]# git commit
[master 184c5b3] Please enter the commit message for your changes. Lines starting
1 file changed, 1 deletion(-)
delete mode 100644 delete.file
[root@gittest gittest]# git status
# On branch master
nothing to commit, working directory clean
|
三 远程仓库操作
在gitlib上创建项目,完成后会自动提示相关命令:
1)clone远程仓库
git clone git@git.xxxcom:xxx/gittest.git
cd gittest
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
|
2)关联远程
关联:git remote add origin git@server-name:path/repo-name.git;
提交分支全部:git push -u origin master
cd existing_folder
git init
git remote add origin git@git.xxx.com:xxx/gittest.git
git add .
git commit
git push -u origin master
|
配置ssh 方法略;
3)从远程拉取最新数据:git fetch origin master
四 分支
1)创建分支:git branch dev
2)切换分支:git chechout dev
[root@gittest gittest]# git checkout dev
Switched to branch 'dev'
|
3)查看分支:git branch
[root@gittest gittest]# git branch
* dev
master
|
4)merge 分支到主干: git merge dev
切换到 master : git checkout master , git marge dev
[root@gittest gittest]# git branch
* dev
master
[root@gittest gittest]# echo '20170729' >> README.md
[root@gittest gittest]# git commit README.md
[dev 405d48e] add date
1 file changed, 1 insertion(+)
[root@gittest gittest]# git checkout master
Switched to branch 'master'
[root@gittest gittest]# git branch
dev
* master
[root@gittest gittest]# cat README.md
gittest
[root@gittest gittest]# git merge dev
Updating 184c5b3..405d48e
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
[root@gittest gittest]# cat README.md
gittest
20170729
|
5)解决冲突
[root@gittest gittest]# git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
[root@gittest gittest]# git status
# On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
|
(a)编辑冲突文件手动解决
[root@gittest gittest]# vi README.md
gittest
<<<<<<< HEAD
201707291632
=======
201707291631
>>>>>>> dev
|
(b)提交:git add + git commit
[root@gittest gittest]# git status
# On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git add README.md
[root@gittest gittest]# git commit README.md
fatal: cannot do a partial commit during a merge.
[root@gittest gittest]# git commit -i README.md
[master f3cf6f9] Merge branch 'dev'
|
注:出现error: fatal: cannot do a partial commit during a merge.
使用:git commit -i
(c)查看分支合并图:git log --graph --pretty=oneline
[root@gittest gittest]# git log --graph --pretty=oneline
* f3cf6f9742af4ba69a956a10e1a63cd43a5a51ab Merge branch 'dev'
|\
| * 6bded575d923bfbfd9d57211f0006ddf3bbe7b5e add time
* | cc33cbf542f16e4fc2cbb79d78573967fb9115e7 add time
|/
* 405d48e85df83aa686dd00d4de01cc44aa12f5d5 add date
* 184c5b3bef0be97eb483e414424e8d25c29e0090 Please enter the commit message for
* 0f7346b632bd645b73646a4d2b03f413a1dfdf8c delete
* fdfa139bc11a1c105c2ac7fa166b98797174a2f8 first commit
|
(d)合并完,分支可以删除了:git branch -d dev
注:开发分支使用策略(略)
五 保存现场
[root@gittest gittest]# 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: tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git checkout dev
error: Your local changes to the following files would be overwritten by checkout:
tmp.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
|
注: 当工作区有关联的文件修改,未提交时不能切换分支,此时要不提交完成,要不回退,也可以使用’git stash’暂存
1)暂存:git stash
[root@gittest gittest]# git stash
Saved working directory and index state WIP on master: 1caf27f tmp file
HEAD is now at 1caf27f tmp file
[root@gittest gittest]# git status
# On branch master
nothing to commit, working directory clean
|
2)查看暂存列表:git stash list
[root@gittest gittest]# git stash list
stash@{0}: WIP on master: 1caf27f tmp file
|
3)暂存后的两种恢复操作
(a)恢复并删除:git stash apply恢复,用git stash drop删除;
[root@gittest gittest]# git stash apply
# 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: tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# 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: tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a”)
[root@gittest gittest]# git stash drop
Dropped refs/stash@{0} (6288235160baf1e05c2f954428377bc6e4642b0a)
|
(b)不恢复直接删除:git stash drop
六 管理Tag:
1)创建:git tag
[root@gittest gittest]# git tag v0.0.1
[root@gittest gittest]# git tag
v0.0.1
|
注:在某个commit 上打版本:git tag v0.0.1 commit_id
[root@gittest gittest]# git reflog
1caf27f HEAD@{0}: commit: tmp file
cade529 HEAD@{1}: commit (initial): first commit
[root@gittest gittest]# git tag v0.0.1 1caf27f
[root@gittest gittest]# git tag
v0.0.1
|
2)删除:git tag -d
[root@gittest gittest]# git tag -d v0.0.1
Deleted tag 'v0.0.1' (was 1caf27f)
[root@gittest gittest]# git tag
|
3)推送tag 到远程:git push origin v0.0.1
相关推荐
git 常用命令使用教程 里面包含有 pull push clone checkout 分支等教程总结Xmind
git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git常用命令总结git...
git是linux系统管理源码常用工具,本资源归纳总结git常用命令使用。
git常用命令速查表
git常用命令教程,新老手必看,研究透可视化git工具git常用命令教程,新老手必看,研究透可视化git工具git常用命令教程,新老手必看,研究透可视化git工具git常用命令教程,新老手必看,研究透可视化git工具git常用...
git常用命令markdown版
一张图展示 Git 常用命令.pdf
Git 常用命令整理及解析 Git 是一个开源的分布式版本控制系统,用于跟踪项目中的代码变化,可以帮助开发者更好地管理代码。下面是 Git 中一些常用的命令,这些命令可以帮助开发者快速掌握 Git 的使用。 基本命令 ...
### Git常用命令手册详解 #### 一、概览 本篇文档主要介绍了一套由国外开发者整理的Git常用命令速查表,旨在帮助用户快速掌握并应用Git的基础操作。通过这些简洁明了的命令,即使是Git新手也能迅速上手进行版本...
标题《git常用命令大全》所涉及的知识点覆盖了使用Git进行版本控制的日常操作。Git是一个开源的分布式版本控制系统,被广泛用于代码管理,可以有效地追踪文件的变更,管理分支的合并与冲突,以及回退到旧版本等。...
以下是对Git常用命令的详细整理,包括配置、基本操作、分支管理、合并、标签、回滚以及日志和远程操作。 1. **配置操作** - `git config --global user.name "Your Name"`:进行全局配置,设置Git的用户名。 - `...
平时常用的git命令
Git常用命令速查表,喜欢的同学自行下载0.0 啦啦啦啦啦啦
Git 常用命令 Git 是一个版本控制系统,广泛应用于软件开发和项目管理中。以下是 Git 中一些常用的命令: 1. Git 文件撤销命令 在 Git 中,可以使用 git reset HEAD . 命令撤销所有已经添加的文件。如果只想撤销...
### git常用命令总结 在日常开发工作中,Git作为版本控制系统被广泛使用,它能帮助团队高效地管理代码版本,提高协同工作的效率。本文将根据给定的文件信息,详细总结并解释Git中的常见命令及其用途。 #### 1. Git...
对开发中常用的git命令的整理,全部都是实际用到的 对开发中常用的git命令的整理,全部都是实际用到的 对开发中常用的git命令的整理,全部都是实际用到的
常用git命令总结,常用git命令总结。 常用git命令总结。
### Git常用命令详解:提交代码相关操作 #### 一、Git Commit 命令概述 在版本控制系统中,Git 提供了一系列强大的命令用于管理和维护代码版本。`git commit` 是其中一个核心命令,用于将暂存区(staging area)内...
### git的常用命令使用 #### 基本概念与初始化 - **Git**:一种分布式版本控制系统,可以高效地管理项目源代码的变化。 - **初始化仓库**:通过`git init`命令可以将当前目录初始化为Git仓库。若需在特定目录下...