Getting Started with Git

Source: Internet
Author: User

Recently prepared to educate colleagues about git and found a good resource here: http://www.gitguys.com/topics/creating-a-shared-repository-users-sharing-the-repository/

The text is as follows and is available for translation:

Ommands discussed in this section:
    • Git init–bare
    • git clone
    • Git remote
    • Git pull
    • git push
Scenario:example Remote Repository

Let's set up we own little "remote" repository and then share it. (The repository would be "remote" to the users sharing it.)

In these examples, the other users sharing the repository won't be very remote since the repository would be on the same Disk as the users ' home directories. But the Git workflow and commands is identical, whether the users and repositories is just a few millimeters away on the Same disk, or on a remote network across the world.

Creating the Shared Repository

We ' ll have the repository created by the user gitadmin. The gitadmin' s repository'll be is the repository where everybody on the project both publishes their work and Also retrieves the latest work done by others.

The scenario:

    • Gitadmin would create a repository.
    • Other users, like Amy and Zack would then get ("git clone") copies of Gitadmin' s remote repository.
    • Changes'll is pulled and pushed to and from gitadmin' s repository.
Create Shared repositories "Bare"

If you're creating a git repository for only your own use on projects or days when you just don ' t feel like sharing, you Type

git init project1 Initialized Empty Git repository in/home/gitadmin/project1/.git/

However, if you is creating a git repository for sharing with git Clone/pull/fetch/push, use the –bare option to git init:

git init--bare project1.git Initialized Empty Git repository in/home/gitadmin/project1.git/

If you are want to know so, see GKFX repositories should be Bare repositories.

Bare repositories End in ". Git"

You might has noticed the –bare repository created above ended in . Git. By convention, bare git repositories should end in. git. For example, project1.git or usplash.git, etc. The . Git ending of a directory signals to others, the Git repository is bare.

Amy is a ready-to-add to the remote repository

In our example, since Amy's name begins with the first letter of the alphabet, she gets to work on the repository first.

Amy Clones it:

git clone file:///home/gitadmin/project1.git Initialized empty Git repository in/home/amy/project1/.git/warning:you appear to the cloned an empty repository.

Git just told us the repository that Amy just cloned is empty.

We can now start creating files and publishing ("git push") them to the shared repository.

Amy wants to see if there is any branches in the repository she just retrieved/cloned:

CD Project1 git branchamy$

The empty output from the git branch command showed is no branches in the new repository.

Amy creates her first file and commit's the new file to the repository.

echo the beginnings of Project1 > Amy.file git Add. git commit-m "Amy ' s initial commit" git branch* Master

The cloned, bare repository didn ' t has any branches and not even the Master repository. When Amy did the first git commit, the master branch is created in Amy's local repository.

Amy tries to publish she local repository to the remote repository:

git push No refs in common and none specified; Doing nothing. Perhaps should specify a branch such as ' Master '. Fatal:the remote end hung up unexpectedlyerror:failed to push some Refs to ' file:///home/gitadmin/project1.git '

Oops, that's didn ' t work. The above happens on brand new, completely empty, branchless repositories (immediately after doing the git init–bare ...).

Amy's local repository created the Master branch, but the GKFX repository that gitadmin created does n OT has any branches on it still.

Amy would take Git's advice and tell git the name of the branch she wants pushed to which remote repository. She must specify both the remote repository name and branch name.

What is the branch and repository names? Amy has been distracted lately and forgot the name of the remote repository, so she ' ll use the git remote command to List the names of her remote repositories:

git remoteorigin

She is shown there are only one remote repository named Origin. The default remote repository when you git clone a repository are named Origin, so the above output isn ' t Surprising.

Similarly, Amy can find out the branch name in her local repository by using the git branch command:

git branch* Master

The branch name master isn ' t surprising either, since master is the default branch name for Git.

Armed with the remote repository name (origin) and local branch name (master) Amy can now push (publish) The changes.

The git push syntax is:
git push [remote-repository-name] [Branch-or-commit-name].
Amy would push the branch named Master to the remote repository named Origin:

git push Origin master Counting Objects:3, done. Writing objects:100% (3/3), 245 bytes, done. Total 3 (delta 0), reused 0 (Delta 0) Unpacking objects:100% (3/3), done. To File:///home/gitadmin/project1.git * [New branch]      master-and master

The last line above reports a new branch is created:the Master Branch (referred to in some places as T He "source") on the local repository is mapped to the Master branch (referred to some places as the "Destinat Ion ") on the remote repository.

Amy would no longer need to type git push Origin master, but would be is able to type git push, since the master Branch now exists on the remote repository named Origin:

git push Everything up-to-date
Zack wants to play too

Now it's Zack ' s turn to play with the repository. He Clones it:

git clone file:///home/gitadmin/project1.git lsamy.file

Above, the file Amy added, amy.file is copied from the GKFX repository to Zack's working directory.

Zack adds a file and pushes it up to the shared repository:

CD Project1 echo I am Zack > Zack.file git Add. git commit-m ' zack initial commit ' git push Counting Objects:4, done. Delta compression using up to 2 threads.compressing objects:100% (2/2), done. Writing objects:100% (3/3), 283 bytes, done. Total 3 (delta 0), reused 0 (Delta 0) Unpacking objects:100% (3/3), done. To File:///home/gitadmin/project1.git   01d7520. 05AFFB3 Master,  Master

Note that Zack didn ' t has to does the GIT push Origin master to create the master branch on the remote RE Pository, since Amy had already created the Master branch on the remote repository.

Amy wants to get the latest
gitpull remote:counting objects:4, done.remote:Compressing objects:100% (2/2), Done.remote:Total 3 (Delta 0) , reused 0 (Delta 0) Unpacking objects:100% (3/3), done. From File:///home/gitadmin/project1   01d7520. 05AFFB3  Master--     origin/masterupdating 01d7520. 05affb3fast-forward Zack.file |    lsamy.file  zack.file

Things is working pretty well:amy and Zack is sharing nicely:they is contributing to ("git Push") and Receiv ing from ("gitPull") the shared repository.

The above summarizes how to get moving with shared, remote repostitories. But there's a lot more fun can has with remote repositories.

Getting Started with Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.