GitHub详细教程

教程来源:http://www.cnblogs.com/God-/p/5556531.html

教程中1.4~1.5小节,命令部分夹杂着其他代码,整理一下,方便日后再看。其余部分原教程已讲述的非常明确详细。

1.4   开始操作Git

1.4.1 创建内容下面创建一些文件,它们会被放到版本控制之中

cd ~   # Switch to home

mkdir ~/repo01  #Create a directory

cd repo01    #Switch into it

mkdir datafiles   #Create a new directory

touch test01   #Create a few files

touch test02

touch test03

ls > test01     #Put a little text into the first file

1.4.2 创建仓库、添加文件和提交更改

每个Git仓库都是放置在.git文件夹下.这个目录包含了仓库的所有历史记录,.git/config文件包含了仓库的本地配置。

以下将会创建一个Git仓库,添加文件到仓库的索引中,提交更改。

git init   #Initialize the local Git repository

cd repo01

git add test01    #Add test01 to the Git repository

git commit -m "Initial commit"   #Make a commit of your file to the local repository

git log   # Show the log file

如果嫌日志信息较多,可以用该命令:git log --pretty=oneline (补充)

1.4.3 diff命令与commit更改

通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看gitdiff命令输出什么,然后提交这个更改到仓库中

echo "This is a change" > test01     #Make some changes to the file

echo "and  this is another change" > test02 

git diff  #Check the changes via the diff command

git commit -a -m  "These are new changes"    #Commit the changes, -a will commit changes for modified files , but will not add automatically new files

1.4.4 Status, Diff 和 Commit Log

下面会向你展示仓库现有的状态以及过往的提交历史

echo "This is a new change" > test01     #Make some changes in the file

echo "and this is another new change" > test02

git status   #See the current status of your repository(which files are changed / new / deleted)

git diff     #Show the differences between the uncommitted files  and the last commit in the current branch

git add . && git commit  -m  "More changes  in the commmit messages"   #Add the changes to the index and commit

git log    #Show the history of commits in the current branch

gitk --all   #This starts a nice graphical view of the changes(输入后提示:-bash: gitk: command not found)

1.4.5 更正提交的信息 - git amend

通过git amend命令,我们可以修改最后提交的的信息。上述的提交信息中存在错误,下面会修改这个错误。

git  commit  --amend  -m   "More changes  - now correct"

1.4.6 删除文件

如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成

touch nonsense.txt    #Create a file and put it under version control

git add . && git  commit  -m  "a new file has been created"

rm nonsense.txt    #Remove the file

git add . && git  commit  -m  "a new file has been created"   #Try standard way of committing -> will not work

git commit -a  -m "File nonsense.txt is now removed"   #Now commit with the -a flag

git add -A . && git commit  -m   "File nonsense.txt is now removed"     #Alternatively you could add deleted files to the staging index via

1.5 远端仓库(remote repositories)

1.5.1 设置一个远端的Git仓库

我们将创建一个远端的Git仓库。这个仓库可以存储在本地或者是网络上。

远端Git仓库和标准的Git仓库有如下差别:一个标准的Git仓库包括了源代码和历史信息记录。我们可以直接在这个基础上修改代码,因为它已经包含了一个工作副本。但是远端仓库没有包括工作副本,只包括了历史信息。可以使用–bare选项来创建一个这样的仓库。

为了方便起见,示例中的仓库创建在本地文件系统上

cd ~/repo01     #switch to the first repository

git  clone  --bare  .  ../remote-repository.git       (首次成功,删除库后再次输入后提示:fatal: repository '.' does not exist)

ls  ~/remote-repository.git     #Check the content, it is identical to the .git directory in repo01

1.5.2 推送更改到其他的仓库

做一些更改,然后将这些更改从你的第一个仓库推送到一个远端仓库

cd ~/repo01

echo "Hello, hello. Turn your radio on" > test01

echo "Bye, bye. Turn your radio off " > test02

git commit -a -m "Some changes"

git push ../remote-repository.git

1.5.3 添加远端仓库

除了通过完整的URL来访问Git仓库外,还可以通过git remote add命令为仓库添加一个短名称。当你克隆了一个仓库以后,origin表示所克隆的原始仓库。即使我们从零开始,这个名称也存在。

git  remote add origin  .. /remote-repository.git    #Add ../remote-repository.git with the name origin

echo "I  added a remote repo" > test02    #Again some changes

git commit -a  -m  "This is a test for the new remote origin"     #commit

git push origin   #if you do not label a repository it will push to origin

1.5.4 显示已有的远端仓库

通过以下命令查看已经存在的远端仓库

git remote     # show the existing  defined  remote repositories

1.5.5 克隆仓库

通过以下命令在新的目录下创建一个新的仓库

cd ~     #Switch to home

mkdir repo02      #Make new directory

cd ~/repo02       #Switch to new directory

git clone  . . /remote-repository.git            #clone

1.5.6 拉取(Pull)更改

通过拉取,可以从其他的仓库中获取最新的更改。在第二个仓库中,做一些更改,然后将更改推送到远端的仓库中。然后在第一个仓库拉取这些更改

cd  ~      #Switch to home

cd  ~/repo02          #Switch to second directory

echo "A change" > test01     #Make changes

git  commit  -a  -m " a change"           #commit

git  push  origin              #push changes to remote repository , Origin is automatically maintained as we cloned from this repository

cd  ~/repo01    

git pull   . . /remote-repository.git /              #Switch to the first repository and pull in the changes

less  test01     #Check the changes

1.5.7 还原更改

如果在你的工作副本中,你创建了不想被提交的文件,你可以丢弃它。

touch test04

echo "this is trash"  > test04       # Create a new file with content

git  clean  -n      # Make a dry-run to see what would happen   ,   -n is the same as --dry-run

git  clean  -f       # now delete

你可以提取老版本的代码,通过提交的ID。git log命令可以查看提交ID

cd   ~/repo01           # Switch to home

git log

git checkout  commit_name      # Copy one of the older commits and checkout the older revision via 译者注:checkout 后加commit id就是把commit的内容复制到index和工作副本中

如果你还未把更改加入到索引中,你也可以直接还原所有的更改

echo "nonsense  change" > test01     #Some nonsense change

git  checkout  test01                        #just checkout the old version,译者注:checkout后如果没有commit id号,就是从index中拷贝数据到工作副本,不涉及commit部分的改变

cat test01           #Check the result

echo "another nonsense change"  >  test01     #Another nonsense change

git add  test01           #We add the file to the staging index

git  reset  HEAD  test01        #Restore the file in the staging index , 译者注:复制HEAD所指commit的test01文件到index中

git check test01         #Get the old version from the staging index , 译者注:复制index中test01到工作副本中 ,译者注,以上两条命令可以合并为git checkout HEAD test01

git revert  commit_name       #也可以通过revert命令进行还原操作  ,  Revert a commit

版本回退:我想把当前的版本回退到上一个版本,要使用什么命令呢?可以使用如下2种命令,第一种是:git reset  --hard HEAD^ 那么如果要回退到上上个版本只需把HEAD^ 改成 HEAD^^ 以此类推。那如果要回退到前100个版本的话,使用上面的方法肯定不方便,我们可以使用下面的简便命令操作:git reset  --hard HEAD~100 即可(补充)

可以通过如下命令获取到已提交成功的版本号:git reflog

通过版本号回退的命令:git reset  --hard 版本号

即使你删除了一个未添加到索引和提交的文件,你也可以还原出这个文件

rm test01    #Delete a file

git check test01           #Revert the deletion

如果你已经添加一个文件到索引中,但是未提交。可以通过git resetfile 命令将这个文件从索引中删除

touch incorrect .txt         // Create a file

git add .                        // Accidently add it to the index

git reset  incorrect .txt     // Remove it from the index

rm  incorrect .txt            // Delete the file

如果你删除了文件夹且尚未提交,可以通过以下命令来恢复这个文件夹 。译者注:即使已经提交,也可以还原

git checkout  HEAD --  your_dir_to_restore

译者注:checkout和reset这两个命令的含义是不同的,可以参阅这篇文章http://marklodato.github.com/visual-git-guide/index-en.html

1.5.8 标记

Git可以使用对历史记录中的任一版本进行标记。这样在后续的版本中就能轻松的找到。一般来说,被用来标记某个发行的版本。可以通过git tag命令列出所有的标记,通过如下命令来创建一个标记和恢复到一个标记

git tag version1.6  -m  'version  1.6'

git checkout <tag_name>

That 's  all~~~~