github的基本用法:克隆,上传,查看,删除,分支

来源:互联网 发布:无间道为什么经典 知乎 编辑:程序博客网 时间:2024/06/05 06:45

下载github for windows ,在github 上注册创建自己的repository(仓库),保存自己的https和ssh 的URL。

打开git bash或linux putty环境:

 远程https方式登录  env|grep http  然后export 环境变量就可以登录到linux上联网

 [root@WCK-eNodeB ~]#cd /home

[root@WCK-eNodeB home]# cd hxj

[root@WCK-eNodeB hxj]# cd daisy_hu

[root@WCK-eNodeB daisy_hu]# ls

data_structure LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git clone https://github.com/daisyhxj/daisy_hu.git

Cloning into daisy_hu...

remote: Counting objects: 13, done.

remote: Compressing objects: 100% (9/9), done.

remote: Total 13 (delta 3), reused 8 (delta 1), pack-reused0

Unpacking objects: 100% (13/13), done.

将下载下来的项目文件夹的所有文件及文件夹,包括.git文件夹在内,全部拷贝到你的托管项目的根目录(或者将你的托管项目拷贝到该目录)

至此已经将整个program克隆到本地

 

在linux根目录上创建好自己的 file(eg  pig.c),编辑好之后,使用git add “pig.c”,使用gitstatus 查看状态,gitcommit –m “pig.c”提交到本地仓库但还没提交到远程仓库,git push origin master (需要提交用户名和密码)或者直接gitpush 就可以将你本地的仓库提交到你的github账号里,在网页上图形界面上就可以查看到repository内容的变化。

[root@WCK-eNodeB data_structure]# touch pig.c

[root@WCK-eNodeB data_structure]# ls

first.c  main.c  pig.c

[root@WCK-eNodeB data_structure]# git add "pig.c"

 [root@WCK-eNodeBdata_structure]# gitstatus –s

A  pig.c

[root@WCK-eNodeB data_structure]# git status

# On branch master

# Changes to be committed:

#   (use "gitreset HEAD <file>..." to unstage)

#

#       new file:   pig.c

执行git commit –m 之前需要config一下

root@ubuntu-daisy:/home/xiujuan_daisy/git_daisy/daisy_hu#git commit -m "red.txt"

 

*** Please tell me who you are.

 

Run

 

  git config --globaluser.email "you@example.com"

  git config --globaluser.name "Your Name"

 

to set your account's default identity.

Omit --global to set the identity only in this repository.

 

fatal: unable to auto-detect email address (got'root@ubuntu-daisy.(none)')

root@ubuntu-daisy:/home/xiujuan_daisy/git_daisy/daisy_hu# git config --global user.email"daisyhxj@1413966385"

root@ubuntu-daisy:/home/xiujuan_daisy/git_daisy/daisy_hu# git config --global user.name"daisyhxj"

root@ubuntu-daisy:/home/xiujuan_daisy/git_daisy/daisy_hu#git commit -m "red.txt"

[master ee99f09] red.txt

 1 file changed, 0insertions(+), 0 deletions(-)

 create mode 100644red.txt

 

[root@WCK-eNodeB data_structure]# git commit -m "pig.c"

[master d26a1b8] pig.c

 0 files changed, 0insertions(+), 0 deletions(-)

 create mode 100644data_structure/pig.c

[root@WCK-eNodeB data_structure]# ls

first.c  main.c  pig.c

[root@WCK-eNodeB data_structure]# git push origin master

Username:

Password:

Counting objects: 5, done.

Delta compression using up to 16 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 298 bytes, done.

Total 3 (delta 1), reused 0 (delta 0)

remote: Resolving deltas: 100% (1/1), completed with 1 localobjects.

To https://github.com/daisyhxj/daisy_hu.git

  f05fac5..d26a1b8  master ->master

至此文件已经保存到网上仓库了

  git pull 是将远程内容下载下来,与gitpush 相反

 若要更改文件内容,可以使用gitdiff 查看更改。添加内容到pig.c中,查看输出

[root@WCK-eNodeB data_structure]# echo "This is achange" > pig.c

[root@WCK-eNodeB data_structure]# git diff

diff --git a/data_structure/pig.c b/data_structure/pig.c

index e69de29..d0a432b 100644

--- a/data_structure/pig.c

+++ b/data_structure/pig.c

@@ -0,0 +1 @@

+This is a change

[root@WCK-eNodeB data_structure]# git status -s

 M pig.c

?? master

使用gitlog 命令查看log file

使用gitamend 命令修改最后提交信息

 [root@WCK-eNodeBdata_structure]# ls

first.c  main.c  master pig.c

[root@WCK-eNodeB data_structure]# vim pig.c

 [root@WCK-eNodeBdata_structure]# git status -s

 M pig.c

?? master

[root@WCK-eNodeB data_structure]# git add "pig.c"

 [root@WCK-eNodeBdata_structure]# git status -s

M  pig.c

?? master

[root@WCK-eNodeB data_structure]# git commit --amend -m "pig.c"

[master 722098c] pig.c

 1 files changed, 1insertions(+), 0 deletions(-)

 create mode 100644data_structure/pig.c

删除文件分两种,一种是在本地上删除但还没commit到仓库,一种是已经push到远程github仓库了

在本地上直接git  rm file,本地上就没有了(删除完后没有将状态commit到远程),若要恢复,先使用git reset HEAD,然后使用git checkout file,就恢复到本地了。(远程上一直都有,如果嫌麻烦,可以直接clone远程内容到本地)

但是若已经commit到远程了,仅仅git rm  file 远程上还是会保存有的,所以需要删除缓存,(删除文件夹)使用gitrm –r --cached directoryname,  git commit–m “directoryname” ,  git push  就ok。删除文件就不需要加-r: git rm–cached filename, git commit –m “filename”, git push 就ok。这种情况结果是远程是文件删除了但本地上还保存着。

Eg :delete data_structure 这个文件夹

[root@WCK-eNodeB daisy_hu]# git rm -r --cached data_structure

rm 'data_structure/main.c'

rm 'data_structure/master'

rm 'data_structure/pig.c'

[root@WCK-eNodeB daisy_hu]# ls

data_structure good.c  LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git commit -m "data_structure"

[master 9f2c097] data_structure

 0 files changed, 0insertions(+), 0 deletions(-)

 delete mode 100644data_structure/main.c

 delete mode 100644data_structure/master

 delete mode 100644data_structure/pig.c

[root@WCK-eNodeB daisy_hu]# git push

Username:

Password:

Counting objects: 17, done.

Delta compression using up to 16 threads.

Compressing objects: 100% (11/11), done.

Writing objects: 100% (12/12), 1.04 KiB, done.

Total 12 (delta 6), reused 0 (delta 0)

remote: Resolving deltas: 100% (6/6), completed with 2 localobjects.

To https://github.com/daisyhxj/daisy_hu.git

  d26a1b8..9f2c097  master ->master

此时查看github上已经没有data_structure 这个文件夹了,文件夹里的内容也都delete了

 

Eg:删除 flour.txt 文件

[root@WCK-eNodeB daisy_hu]# ls

flour.txt LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git rm flour.txt

rm 'flour.txt'

[root@WCK-eNodeB daisy_hu]# ls

LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git rm --cached flour.txt   //git rm git rm –cached不能同时使用,删除远程直接使用删除缓存这种方法

fatal:pathspec 'flour.txt' did not match any files

[root@WCK-eNodeB daisy_hu]# git checkout flour.txt

error: pathspec 'flour.txt' did not match any file(s) knownto git.

[root@WCK-eNodeB daisy_hu]# git reset HEAD

Unstaged changes after reset:

M       flour.txt

[root@WCK-eNodeB daisy_hu]# git checkout flour.txt

[root@WCK-eNodeB daisy_hu]# ls           //恢复之后开始

flour.txt LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git rm --cached flour.txt

rm 'flour.txt'

[root@WCK-eNodeB daisy_hu]# ls        // 本地上还在

flour.txt LICENSE  README.md

[root@WCK-eNodeB daisy_hu]# git commit -m "flour.txt"

[master 1c463a1] flour.txt

 0 files changed, 0insertions(+), 0 deletions(-)

 delete mode 100644flour.txt

[root@WCK-eNodeB daisy_hu]# git push

Username:

Password:

Counting objects: 3, done.

Delta compression using up to 16 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (2/2), 212 bytes, done.

Total 2 (delta 1), reused 0 (delta 0)

remote: Resolving deltas: 100% (1/1), completed with 1 localobjects.

To https://github.com/daisyhxj/daisy_hu.git

   fe3ef2f..1c463a1  master -> master

[root@WCK-eNodeB daisy_hu]# ls

flour.txt LICENSE  README.md

此时本地上文件还在,但是远程上已经删除了。可以登录github账号查看

 

若在本地删除完并且commit了,此时要恢复删除文件,可以使用 gitreset –hard origin/master 将文件恢复到与远程上一样的状态(使用checkout并不能恢复文件)

Eg: 恢复flour.txt

[root@WCK-eNodeB daisy_hu]# git reset --hard origin/master

HEAD is now at fe3ef2f flour.txt

[root@WCK-eNodeB daisy_hu]# ls

data_structure flour.txt  LICENSE  README.md

 

注意:每次使用git时都要使用git fetch,github中的项目同步更新至你本地目录,因为有时做同一个 项目共用一个仓库时会有不同的程序员更新代码,需要及时更新下载下来。

 

分支

分支是将特性开发绝缘开来的,在创建仓库的时候,master是”默认”分支,在其他分支上进行开发,然后再将其合并到主分支上来。

查看分支:git branch

创建分支:git branch name

切换分支:git checkoutname

创建+切换分支:git checkout -b name

合并某分支到当前分支:git merge name

删除分支:git branch -dname

推送到远程分支:git push origin branch-name

删除远程上的分支:git push origin:branch-name (注意冒号前的空格不能少,原理是把一个空分支push到server上,相当于删除该分支)

只是删除本地对该远程分支的track:  git branch –r –d origin/branch-name

[root@WCK-eNodeBdaisy_hu]# git branch

* master

[root@WCK-eNodeBdaisy_hu]# git branch blue

[root@WCK-eNodeBdaisy_hu]# git branch

  blue

* master

[root@WCK-eNodeBdaisy_hu]# git checkoutblue

D       good.c

Switched to branch'blue'

[root@WCK-eNodeBdaisy_hu]# git branch

* blue

  master

 [root@WCK-eNodeB daisy_hu]# git commit -m "blue"

# On branch blue

# Changes not stagedfor commit:

#   (use "git add/rm <file>..."to update what will be committed)

#   (use "git checkout --<file>..." to discard changes in working directory)

#       deleted:    good.c

# Untracked files:

#   (use "git add <file>..." toinclude in what will be committed)

#       daisy_hu/

no changes added tocommit (use "git add" and/or "git commit -a")

 [root@WCK-eNodeB daisy_hu]# git push origin blue

Username:

Password:

Total 0 (delta 0),reused 0 (delta 0)

Tohttps://github.com/daisyhxj/daisy_hu.git

 * [new branch]      blue -> blue

创建一个叫做"feature_x"的分支,并切换过去:
git checkout-b feature_x
切换回主分支:

git checkoutmaster
再把新建的分支删掉:

git branch -dfeature_x
除非你将分支推送到远端仓库,不然该分支就是 
不为他人所见的

git pushorigin <branch>







0 0
原创粉丝点击