Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionI'm using git here and there when I need some basic VCS functionality, but I've yet to fully understand how certain things work in Git.
Git, unlike SVN, is decentralized so that I could start a repository in one place and work with it locally, and then push my ch开发者_如何学编程anges to another repository, at least that's how I understand it.
I'd like to know a few key things:
If I want to create a new repository on my local machine, and than
push
(?) it to the server (it either has or doesn't this repo already), what are the actions needed?Do I need a web-server to interact with remote repos?
How do I push/pull from/to a server that I have SSH access to?
Hopefully the reply would be short and to the point - man pages are great by they don't always convey what they need and sometimes have info that I don't need. So I hope you'll forgive me and my question even if it was asked/answer many times before.
Before anything else, understand how to configure ssh access (in
general, not just for git
) to your server, such that you can run
something like:
ssh myserver uptime
And have it run the remote command without prompting you for a
password. This will make your life with git
much more pleasant.
If I want to create a new repository on my local machine, and than push (?) it to the server (it either has or doesn't this repo already), what are the actions needed?
On the remote server:
Create the target repository:
$ mkdir -p path/to/repo.git $ cd path/to/repo.git $ git init --bare
On your local system:
Create your repository...
$ mkdir myrepo
$ cd myrepo
$ git init
...and commit some changes.
$ git add a-file-i-editied
$ git commit -m 'this is a change'
Add a remote -- i.e., a reference to a remote repository:
$ git remote add origin you@yourserver:path/to/repo.git
Where you is your userid on the remote server and yourserver is the hostname (or ip address) of the remote server.
Push your changes to the remote repository:
$ git push origin master
Where origin is the name you have your remote in the previous step, and master is the branch that you're pushing.
Do I need a web-server to interact with remote repos?
Note the lack of any web server in the previous example. Git can
operate over http/https, but it is more often used over ssh. Git also
provides a native git protocol that can be used for providing
anonymous read-only access to repositories; the git-daemon
implements this protocol.
How do I push/pull from/to a server that I have SSH access to?
This is pretty much the example I've provided, but let me know if you would like more detail in any of the steps.
What you've asked in the question happen to be the most fundamental aspects of Git and Github. Let me walk you through the exact steps for what you asked.
1) To create a new git repo, navigate to the project folder using Terminal(or another equivalent program for your OS)/ Open Terminal in the folder and type the following command
git init
This initializes your project with an empty git repository.
You can now go ahead and make changes to your project and record those changes in individual commits. Here's how you issue a commit
git commit -m "YOUR COMMIT MESSAGE"
To push your repo to github/ any other git hosting service, you first need to add the remote repository url as follows
$ git remote add origin you@yourserver_url.git
Now it's ready to be pushed. Issue the following command to do so
$ git push origin master
2) Do you need a server to interact with Git
This has been answered very well by Eli above but just to summarize. No you don't. Most people use an existing service like Github, Bitbucket, Gitlab etc to host their repos, in which case you just need to interact with the remote repo using the remote url.
3) How to push/ pull from a remote repo?
First you need access to the repo if it's private (If it's not you're good to go). Next get the URL of the repo, it should look something like this
https://github.com/YOUR_USERNAME/YOUR_PROJECT.git (This URL example is from github)
next you need to clone it to your local machine as follows
git clone https://github.com/YOUR_USERNAME/YOUR_PROJECT.git
Now that you have cloned it, make the necessary changes and just push it back using the command I mentioned below.
There's way more to version control than just the basics. If you want to learn more there's an interactive site try.github.io that you can refer for learning up the basics or check out this article that I have written on the same, it should help you get started.
Here's the link https://www.techlila.com/version-control-git/
精彩评论