上一篇文章介绍了GitHub与SVN的区别,这篇文章主要介绍GitHub本地仓库的一些基本操作
Git的安装
因为本人使用的是Linux系统,因此在这里只介绍Linux上安装过程
先判断自己是否已经安装好git:
[tangyanjun@VM_216_80_centos GIT]$ git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
[-p|--paginate|--no-pager] [--no-replace-objects]
[--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]
这意味着你的系统已经有git了。
如果没有,那么我们用以下命令进行安装:
[tangyanjun@VM_216_80_centos GIT]$ yum install curl-devel expat-devel gettext-devel \
openssl-devel zlib-devel
[tangyanjun@VM_216_80_centos GIT]$ yum -y install git-core
[tangyanjun@VM_216_80_centos GIT]$ git --version
git version 1.7.1
如果没有安装包,那么我们可以进行以下操作:
[tangyanjun@VM_216_80_centos GIT]$ sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[tangyanjun@VM_216_80_centos GIT]$ sudo yum install gcc-c++ perl-ExtUtils-MakeMaker
[tangyanjun@VM_216_80_centos GIT]$ wget https://www.kernel.org/pub/software/scm/git/git-2.7.3.tar.gz
[tangyanjun@VM_216_80_centos GIT]$ tar czxf git-2.7.3.tar.gz
[tangyanjun@VM_216_80_centos GIT]$ cd git-2.7.3
[tangyanjun@VM_216_80_centos GIT]$ sudo make configure
[tangyanjun@VM_216_80_centos GIT]$ sudo ./configure --prefix=/usr/git ##配置目录
[tangyanjun@VM_216_80_centos GIT]$ sudo make profix=/usr/git
[tangyanjun@VM_216_80_centos GIT]$ sudo make install
[tangyanjun@VM_216_80_centos GIT]$ sudo echo "export PATH=$PATH:/usr/git/bin" >> /etc/profile
[tangyanjun@VM_216_80_centos GIT]$ sudo source /etc/profile
配置
[tangyanjun@VM_216_80_centos GIT]$ git config --global user.name "tangyanjun"
[tangyanjun@VM_216_80_centos GIT]$ git config --global user.email 519656780@qq.com
[tangyanjun@VM_216_80_centos GIT]$ git config --list
user.name=tangyanjun
user.email=519656780@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:tangyanjun/peter.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
工作流程
- 克隆 Git 资源作为工作目录。
- 在克隆的资源上添加或修改文件。
- 如果其他人修改了,你可以更新资源。
- 在提交前查看修改。
- 提交修改。
- 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。
创建仓库
[tangyanjun@VM_216_80_centos git]$ git init
Reinitialized existing Git repository in /home/tangyanjun/test/boke/git/.git/
[tangyanjun@VM_216_80_centos git]$ ls -a
. .. .git
[tangyanjun@VM_216_80_centos git]$
Git的数据和资源就放在.git这个目录中
基本操作
git clone [url] //复制链接上的项目
在git目录下创建两个文件:
[tangyanjun@VM_216_80_centos git]$ touch aa.txt
[tangyanjun@VM_216_80_centos git]$ touch hh.txt
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt hh.txt
[tangyanjun@VM_216_80_centos git]$ git status -s //查看项目当前状态
?? aa.txt
?? hh.txt
[tangyanjun@VM_216_80_centos git]$
将文件添加到缓存:
[tangyanjun@VM_216_80_centos git]$ git add aa.txt hh.txt
[tangyanjun@VM_216_80_centos git]$ git status -s
A aa.txt
A hh.txt
[tangyanjun@VM_216_80_centos git]$
A的意思是我们的文件已添加到缓存,-s 为简短结果输出
接下来修改我们的文件:
[tangyanjun@VM_216_80_centos git]$ echo hello > aa.txt
[tangyanjun@VM_216_80_centos git]$ cat aa.txt
hello
[tangyanjun@VM_216_80_centos git]$ git status -s
AM aa.txt
A hh.txt
M的意思为有改动
查看文件修改前后的区别:
- 尚未缓存的改动:git diff
- 已缓存的改动:git diff –cached
- 所有改动:git diff HEAD
- 显示摘要:git diff –stat
[tangyanjun@VM_216_80_centos git]$ git diff
diff --git a/aa.txt b/aa.txt
index e69de29..ce01362 100644
--- a/aa.txt
+++ b/aa.txt
@@ -0,0 +1 @@
+hello
可查看我们刚才改动的aa.txt文件中的内容
提交到本地仓库:
[tangyanjun@VM_216_80_centos git]$ git commit -m "commit aa.txt"
[master (root-commit) d44f785] commit aa.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 aa.txt
create mode 100644 hh.txt
[tangyanjun@VM_216_80_centos git]$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
修改第二个文件的内容并取消其中一个的缓存,看两个文件的状态
[tangyanjun@VM_216_80_centos git]$ ech ooooo > hh.txt
-bash: ech: command not found
[tangyanjun@VM_216_80_centos git]$ echo ooooo > hh.txt
[tangyanjun@VM_216_80_centos git]$ cat hh.txt
ooooo
[tangyanjun@VM_216_80_centos git]$ git commit -m "hh"
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aa.txt
# modified: hh.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[tangyanjun@VM_216_80_centos git]$ git add hh.txt
[tangyanjun@VM_216_80_centos git]$ git commit -m "hh"
[master 6b46afd] hh
1 files changed, 1 insertions(+), 0 deletions(-)
[tangyanjun@VM_216_80_centos git]$ git reset HEAD -- hh.txt
Unstaged changes after reset:
M aa.txt
[tangyanjun@VM_216_80_centos git]$ git status -s
M aa.txt
[tangyanjun@VM_216_80_centos git]$ git commit -m "modify"
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
可以看出hh.txt文件已经被取消缓存了,只剩下aa.txt文件了
将文件移除缓存区
[tangyanjun@VM_216_80_centos git]$ git rm hh.txt
rm 'hh.txt'
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt
[tangyanjun@VM_216_80_centos git]$ git rm --cached aa.txt
rm 'aa.txt'
–cached 是不从工作区删除文件
移动或重命名一个文件或目录或链接
[tangyanjun@VM_216_80_centos git]$ touch cc.txt
[tangyanjun@VM_216_80_centos git]$ git add cc.txt
[tangyanjun@VM_216_80_centos git]$ git mv cc.txt bb.txt
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt bb.txt
分支管理:由于这部分并没有深入理解,因此以下内容仅供本人自己参考
我们可以建立不同的分支来进行我们不同的工作,即在不同的分支中的文件是不同的,做的事情也不同,但是最后可以将这些分支合并起来
查看、建立分支
[tangyanjun@VM_216_80_centos git]$ git branch
* master
[tangyanjun@VM_216_80_centos git]$ git branch testing
[tangyanjun@VM_216_80_centos git]$ git branch
* master
testing
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt bb.txt
[tangyanjun@VM_216_80_centos git]$ cat bb.txt
[tangyanjun@VM_216_80_centos git]$ echo 'hello' > bb.txt
[tangyanjun@VM_216_80_centos git]$ git add .
[tangyanjun@VM_216_80_centos git]$ git commit -m "add"
[master 44f2117] add
3 files changed, 2 insertions(+), 1 deletions(-)
create mode 100644 bb.txt
delete mode 100644 hh.txt
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt bb.txt
[tangyanjun@VM_216_80_centos git]$ git checkout testing
Switched to branch 'testing'
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt hh.txt
[tangyanjun@VM_216_80_centos git]$ git checkout master
Switched to branch 'master'
[tangyanjun@VM_216_80_centos git]$ ls
aa.txt bb.txt
可见新创建的分支和原有的master分支的内容是不同的,开始删除的hh.txt文件又回来了,而且换至master下面去的时候又不见了。
删除分支
[tangyanjun@VM_216_80_centos git]$ git branch -D newtest
Deleted branch newtest (was 9c9513d).
[tangyanjun@VM_216_80_centos git]$ git branch
* master
testing
合并分支&&合并冲突
[tangyanjun@VM_216_80_centos git]$ git branch
* master
newtest
$ ls
README test.txt test2.txt
[tangyanjun@VM_216_80_centos git]$ git merge newtest
Updating 2e082b7..556f0a0
Fast-forward
test2.txt | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 test2.txt
[tangyanjun@VM_216_80_centos git]$ ls
README test.txt
将newtest分支合并到主分支去
冲突:同时修改主分支和其他分支的同一个文件内容,合并的时候就会出现冲突
[tangyanjun@VM_216_80_centos git]$ git merge change_site
Auto-merging bb.txt
CONFLICT (content): Merge conflict in bb.txt
Automatic merge failed; fix conflicts and then commit the result.
[tangyanjun@VM_216_80_centos git]$ vim bb.txt
解决这种冲突我们要手动修改文件内容,然后add,告诉它们文件冲突已经解决。
[tangyanjun@VM_216_80_centos git]$ git diff
diff --cc bb.txt
index 6bc663c,639e78c..0000000
--- a/bb.txt
+++ b/bb.txt
@@@ -1,2 -1,2 +1,7 @@@
hello
+ ssssssss
++<<<<<<< HEAD
+xxxxxx
++=======
++ssssssss
++>>>>>>> change_site
[tangyanjun@VM_216_80_centos git]$ git status -s
UU bb.txt
[tangyanjun@VM_216_80_centos git]$ git add bb.txt
[tangyanjun@VM_216_80_centos git]$ git status -s
M bb.txt