Git远端仓库(二)
1. 设置一个远端的Git仓库
我们将创建一个远端的Git仓库。这个仓库可以存储在本地或者是网络上。
远端Git仓库和标准的Git仓库有如下差别:一个标准的Git仓库包括了源代码和历史信息记录。我们可以直接在这个基础上修改代码,因为它已经包含了一个工作副本。但是远端仓库没有包括工作副本,只包括了历史信息。可以使用--bare选项来创建一个这样的仓库。
为了方便起见,示例中的仓库创建在本地文件系统上
# Switch to the first repository cd ~/ repo01 # git clone local repository git clone --bare . ../remote- repository.git # Check the content, it is identical to the .git directory in repo01ls ~/remote-repository.git
2. 推送更改到其他的仓库
做一些更改,然后将这些更改从你的第一个仓库推送到一个远端仓库
# Make some changes in the first repository cd ~/ repo01 # Make some changes in the file echo "Hello, hello. Turn your radio on" > test01echo "Bye, bye. Turn your radio off" > test02 # Commit the changes, -a will commit changes for modified files # but will not add automatically new files git commit -a -m "Some changes" # Push the changes git push .. /remote-repository.git
3. 添加远端仓库
除了通过完整的URL来访问Git仓库外,还可以通过git remote add命令为仓库添加一个短名称。当你克隆了一个仓库以后,origin表示所克隆的原始仓库。即使我们从零开始,这个名称也存在。
# Add ../remote- repository.git with the name origin git remote add origin .. /remote- repository.git # Again some changes echo "I added a remote repo" > test02 # Commit git commit -a -m "This is a test for the new remote origin" # If you do not label a repository it will push to origin git push origin4. 显示已有的远端仓库
通过以下命令查看已经存在的远端仓库
# Show the existing defined remote repositories git remote5. 克隆仓库
通过以下命令在新的目录下创建一个新的仓库
# Switch to home cd ~ # Make new directory mkdir repo02 # Switch to new directory cd ~/ repo02 # Clone git clone .. /remote-repository.git6. 拉取(Pull)更改
通过拉取,可以从其他的仓库中获取最新的更改。在第二个仓库中,做一些更改,然后将更改推送到远端的仓库中。然后第一个仓库拉取这些更改
# Switch to home cd ~ # Switch to second directory cd ~/ repo02 # Make changes echo "A change" > test01 # Commit git commit -a -m "A change" # Push changes to remote repository # Origin is automatically maintained as we cloned from this repository git push origin # Switch to the first repository and pull in the changes cd ~/ repo01 git pull .. /remote-repository.git/ # Check the changes less test01