git上传

来源:互联网 发布:英语六级哪个软件好 编辑:程序博客网 时间:2024/05/23 18:18

Git doesn't have a central server like Subversion. All of the commands so far have been done locally, just updating a local database. To collaborate with other developers in Git, you have to put all that data on a server that the other developers have access to. The way Git does this is to synchronize your data with another repository. There is no real difference between a server and a client - a Git repository is a Git repository and you can synchronize between any two easily.

Git不像Subversion一样有一个中央服务器。到目前为止,所有的命令都是在本地完成的,只向本地服务器上传。为了和在git中的其他开发者合作,你必须将所有数据上传到一个别的开发者能够访问的服务器上。git做这件事的方法是将你的数据与另外一个git仓库同步。服务器和客户端之间是没有区别的——Git仓库就是Git仓库,你可以和另一个轻易的同步。

Once you have a Git repository, either one that you set up on your own server, or one hosted someplace like GitHub, you can tell Git to either push any data that you have that is not in the remote repository up, or you can ask Git to fetch differences down from the other repo.

你有一个Git仓库,另一个被你发布到了你自己的服务器上,或者像GitHub一样被布置在别处的服务器,你可以让Git上传任何远程仓库里没有的东西,也可以让Git去取回远程仓库中和你自己仓库里不一样的东西。

You can do this any time you are online, it does not have to correspond with a commit or anything else. Generally you will do a number of commits locally, then fetch data from the online shared repository you cloned the project from to get up to date, merge any new work into the stuff you did, then push your changes back up.

任何时候你都可以在线做这些事情,它不需要用一个commit命令或者其他的什么。一般说来,你将在本地做一些提交,然后从在线分享仓库中取回那些你克隆这个项目用的数据,然后把它们更新,合并你新做的工作到你刚才下载的数据中,再把他们上传回远程服务器。

In a nutshell you can update your project with git fetch and share your changes with git push. You can manage your remote repositories with git remote.

简而言之,你能用git fetch命令上传你的项目,用git push命令上分享你的更改。你可以用git remote命令管理你的远程仓库。

DOCS   BOOKGIT REMOTE list, add and delete remote repository aliases

Unlike centralized version control systems that have a client that is very different from a server, Git repositories are all basically equal and you simply synchronize between them. This makes it easy to have more than one remote repository - you can have some that you have read-only access to and others that you can write to as well.

不像那些中央型的版本控制系统有一个和服务器非常不同的客户端,Git仓库们差不多都一样,你可以在他们中间很轻易的同步。这让拥有不止一个远程仓库变得容易了许多。你可以有一些仓库是你只可读的,还有一些是可写的。

 

So that you don't have to use the full URL of a remote repository every time you want to synchronize with it, Git stores an alias or nickname for each remote repository URL you are interested in. You use the git remote command to manage this list of remote repos that you care about.

为了不让你每次在同步时都输入完整的仓库的URL地址,Git为每一个你感兴趣的远程仓库都保存了一个别名或者昵称。你可以用git remote命令区管理一个远程你关心的仓库列表。

git remote list your remote aliases

Without any arguments, Git will simply show you the remote repository aliases that it has stored. By default, if you cloned the project (as opposed to creating a new one locally), Git will automatically add the URL of the repository that you cloned from under the name 'origin'. If you run the command with the -v option, you can see the actual URL for each alias.

不用任何参数,git将输出它已经存储的远程仓库别名。默认情况下,如果你克隆了一个项目(在本地说来就是创建了一个新的项目),git将自动给你克隆的仓库添加一个前缀名为‘origin’的URL地址。如果你运行git remote命令 加 -v参数,你可以看到每个别面的真是URL地址。

$ git remote origin $ git remote -v origin git@github.com:schacon/git-reference.git (fetch) origin git@github.com:schacon/git-reference.git (push)

You see the URL there twice because Git allows you to have different push and fetch URLs for each remote in case you want to use different protocols for reads and writes.

你在那看到两次这个URL地址是因为Git允许你对于每一个远程仓库有不同的上传(push)和取回(fetch)URL地址,为了让你可以用不同的端口号读取或上传。

git remote add add a new remote repository of your project

If you want to share a locally created repository, or you want to take contributions from someone elses repository - if you want to interact in any way with a new repository, it's generally easiest to add it as a remote. You do that by running git remote add [alias] [url]. That adds [url] under a local remote named [alias].
如果你想分享一个在本地创建的仓库,或者你想从别人的仓库中获取贡献——如果你想要通过任何方式与新的仓库之间进行交互。最简单的方式就是将它加为一个远程仓库。你可以执行 git remote add [alias] [url]命令来实现。它把[url]添加在一个本地的远程名[alias]下面。

For example, if we want to share our Hello World program with the world, we can create a new repository on a server (I'll use GitHub as an example), which should give you a URL, in this case "git@github.com:schacon/hw.git". To add that to our project so we can push to it and fetch updates from it we would do this:
比如说,如果我们想和这个世界分享我们的hello world 程序,我们可以在服务器上创建一个新的仓库(我将用git hub 当做一个例子),而后它会给你一个URL地址,比如地址是“git@github.com:schacon/hw.git”,为了将这个地址加入到我们的项目里以便让我们可以向远程仓库上传或者取回数据我们可以像下面这样做:

$ git remote $ git remote add github git@github.com:schacon/hw.git $ git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push)

Like the branch naming, remote alias names are arbitrary - just as 'master' has no special meaning but is widely used because git init sets it up by default, 'origin' is often used as a remote name because git clone sets it up by default as the cloned-from URL. In this case I've decided to name my remote 'github', but I could have really named it just about anything.

就像分支命名一样,远程别名是随意的。和'master'分支没有什么特殊含义一样,只是因为git init命令把它设置为默认明明所以被广泛使用了。'origin'经常被用在远程仓库的命名上,因为git clone命令将它用作默认的远程URL命名。

git remote rm removing an existing remote alias

Git addeth and Git taketh away. If you need to remove a remote - you are not using it anymore, the project is gone, etc - you can remove it with git remote rm [alias].
Git 的添加与删除。如果你不再需要某个远程仓库了,你可以执行git remote rm [alias]。命令来删除它。

$ git remote -vgithub git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push) $ git remote add origin git://github.com/pjhyett/hw.git $ git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push) origin git://github.com/pjhyett/hw.git (fetch) origin git://github.com/pjhyett/hw.git (push) $ git remote rm origin $ git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push)

In a nutshell with git remote you can list our remote repositories and whatever URL that repository is using. You can use git remote add to add new remotes and git remote rm to delete existing ones.
简而言之,用git remote命令你可以列出你的远程仓库,无论那个仓库正在使用什么URL。你可以用git remote add 去新加入一个远程仓库,用git remote rm 命令去删除现有的仓库。

DOCS   BOOKGIT FETCH download new branches and data from a remote repository
git fetch 是用来从远程仓库中下载新的分支和数据的。


DOCS   BOOKGIT PULL fetch from a remote repo and try to merge into the current branch
git pull 从远程仓库取回一个分支然后和现有分支合并。

Git has two commands to update itself from a remote repository. git fetch will synchronize you with another repo, pulling down any data that you do not have locally and giving you bookmarks to where each branch on that remote was when you synchronized. These are called "remote branches" and are identical to local branches except that Git will not allow you to check them out - however, you can merge from them, diff them to other branches, run history logs on them, etc. You do all of that stuff locally after you synchronize.
Git 有两个命令可以从远程仓库来更新自己。git fetch将把你的仓库和另一个仓库同步,下载那些你在本地没有的数据,当你同步的时候给你那些在远程仓库中的分支在哪的书签。这些被叫做“远程分支”的分支与本地分支完全一样,除非git不允许你查看其中的内容。但是,你可以对这些分支进行合并、查找它们与其他分支的不同、查看它们的历史日志等等操作。

The second command that will fetch down new data from a remote server is git pull. This command will basically run a git fetch immediately followed by a git merge of the branch on that remote that is tracked by whatever branch you are currently in. I personally don't much like this command - I prefer runningfetch and merge seperately. Less magic, less problems. However, if you like this idea, you can read about it in more detail in the official docs.
第二个命令git pull将从远程服务器取回新的数据。这个命令将执行一个git fetch紧接着再跟你现在所在的分支执行一个git merge。我个人不是喜欢这个命令,我更喜欢将fetch和merge分开执行。少一点神奇少一点问题。但是,如果你喜欢这个主意,你可以在官方文档中深入了解一下这个命令。

Assuming you have a remote all set up and you want to pull in updates, you would first run git fetch [alias] to tell Git to fetch down all the data it has that you do not, then you would run git merge [alias]/[branch] to merge into your current branch anything new you see on the server (like if someone else has pushed in the meantime). So, if I were working on my Hello World project with several other people and I wanted to bring in any changes that had been pushed since I last connected, I would do something like this:
假设你有一个远程完整的配置(all set up),你现在想要从上面下载进行更新,你首先要运行git fetch [alias]命令去告诉git要取回所有的你没有的数据,然后你再执行 git merge [alias]/[branch]命令去与你现在的分支合并,将任何你在server上看到的新数据合并进去。如果我正在我的hello world 项目上与其他几个人一起工作者。我想要下载所有我上一次连接(指上一次和服务器同步)后被上传的内容,我将像下面这么做。

$ git fetch github remote: Counting objects: 4006, done. remote: Compressing objects: 100% (1322/1322), done. remote: Total 2783 (delta 1526), reused 2587 (delta 1387) Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done. Resolving deltas: 100% (1526/1526), completed with 387 local objects. From github.com:schacon/hw 8e29b09..c7c5a10 master -> github/master 0709fdc..d4ccf73 c-langs -> github/c-langs 6684f82..ae06d2b java -> github/java * [new branch] ada -> github/ada * [new branch] lisp -> github/lisp

I can see that since the last time I synchronized with this remote, five branches have been added or updated. The 'ada' and 'lisp' branches are new, where the 'master', 'c-langs' and 'java' branches have been updated. In this case, my team is pushing proposed updates to remote branches for review before they're merged into 'master'.
我可以看到自大上一次我和远程仓库同步以后,5个分支已经被添加或者更新了。'ada'和'lisp'分支是新的,'master','c-langs'和'java'分支应经被更新。为了在被合并到主分支前复查,我的团队正在上传将要进行的更新到远程分支上。

You can see the mapping that Git makes. The 'master' branch on the remote repository becomes a branch named 'github/master' locally. That way now I can merge the 'master' branch on that remote into my local 'master' branch by running git merge github/master. Or, I can see what new commits are on that branch by running git log github/master ^master. If your remote is named 'origin' it would be origin/master instead. Almost any command you would run using local branches you can use remote branches with too.
你能看到Git生成的映射。在远程仓库上的'master'主分支变成了本地的一个名为'github/master'分支。那样的话,现在我就可以将远程仓库上的主分支和我本地的主分支用git merge github/master命令合并起来了。或者,我能用git log github/master ^master查看那个远程仓库上的主分支有什么新提交。如果你的远程仓库名字叫'origin',那那个分支名字就叫origin/master。大多数情况下,你能对本地分支执行的命令都可以对远程分支执行。

If you have more than one remote repository, you can either fetch from specific ones by running git fetch [alias] or you can tell Git to synchronize with all of your remotes by running git fetch --all.
如果你有不止一个远程仓库,你能运行git fetch [alias]从指定的仓库中取回数据。也可以执行git fetch --all与所有远程仓库同步数据。

In a nutshell you run git fetch [alias] to synchronize your repository with a remote repository, fetching all the data it has that you do not into branch references locally for merging and whatnot.
简而言之,你执行git fetch [alias]让你的仓库与远程仓库同步,取回所有你没有的数据,然后用它们与你的分支合并或做些别的。

DOCS   BOOKGIT PUSH push your new branches and data to a remote repository

To share the cool commits you've done with others, you need to push your changes to the remote repository. To do this, you run git push [alias] [branch] which will attempt to make your [branch] the new [branch] on the [alias] remote. Let's try it by initially pushing our 'master' branch to the new 'github' remote we created earlier.
为了分享你和别人做的好的提交,你需要将你的修改上传到远程仓库。你可以执行git push [alias] [branch]将你的分支搬到远程仓库里。让我们试试将我们的主分支上传到'github'远程仓库。

$ git push github master Counting objects: 25, done. Delta compression using up to 2 threads. Compressing objects: 100% (25/25), done. Writing objects: 100% (25/25), 2.43 KiB, done. Total 25 (delta 4), reused 0 (delta 0) To git@github.com:schacon/hw.git * [new branch] master -> master

Pretty easy. Now if someone clones that repository they will get exactly what I have committed and all of its history.
非常简单!现在如果有人克隆了那个仓库,那么他们将获得我刚才上传的内容和所有它的历史。

What if I have a topic branch like the 'erlang' branch we created earlier and I just want to share that? You can just push that branch instead.
如果我现在有一个主题为'erlang'的分支,我想把它分享我该怎么做呢?你只要把它像刚才那样上传就可以了。

$ git push github erlang Counting objects: 7, done. Delta compression using up to 2 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 652 bytes, done.Total 6 (delta 1), reused 0 (delta 0) To git@github.com:schacon/hw.git * [new branch] erlang -> erlang

Now when people clone or fetch from that repository, they'll get an 'erlang' branch they can look at and merge from. You can push any branch to any remote repository that you have write access to in this way. If your branch is already on the server, it will try to update it, if it is not, Git will add it.
现在人们克隆或者从仓库中取回信息的时候,他们将获得他们可以查看和合并的'erlang'分支。你可以上传任何分支到任何一个远程你拥有写权限的仓库中。如果你的分支已经在服务器中,服务器将会更新它,如果没有,服务器会添加一个分支。

The last major issue you run into with pushing to remote branches is the case of someone pushing in the meantime. If you and another developer clone at the same time, you both do commits, then she pushes and then you try to push, Git will by default not allow you to overwrite her changes. Instead, it basically runsgit log on the branch you're trying to push and makes sure it can see the current tip of the server's branch in your push's history. If it can't see what is on the server in your history, it concludes that you are out of date and will reject your push. You will rightly have to fetch, merge then push again - which makes sure you take her changes into account.
最后一个关于上传的问题就是,当你上传的同时有别人也在上传该怎么办?如果你和其他开发者同时克隆项目,你们都要进行提交,然后她提交,接着你提交,git默认的情况下不允许你将她的更改覆盖。Git将查看你分支的历史,以确认你本地的原始数据已经包含了服务器中的最新数据。如果Git在你的本地服务器中找不到你要更新分支的最新版本,它就认为你的数据已经过时,将拒绝你的更新。你只能再次取回、合并分支--以确保你把它的最新版本包含在了你提交的版本里。

This is what happens when you try to push a branch to a remote branch that has been updated in the meantime:

$ git push github master To git@github.com:schacon/hw.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:schacon/hw.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.

You can fix this by running git fetch github; git merge github/master and then pushing again.

In a nutshell you run git push [alias] [branch] to update a remote repository with the changes you've made locally. It will take what your [branch] looks like and push it to be [branch] on the remote, if possible. If someone else has pushed since you last fetched and merged, the Git server will deny your push until you are up to date.
简而言之,你执行git push [alias] [branch]用你在本地的更改去更新远程仓库。如果可能,它将把你的分支上传到远程仓库。如果有人在你上次取回或合并分支以后提交,git服务器将拒绝你的上传,直到你更新自己的版本。

0 0
原创粉丝点击