Join Kevin Skoglund for an in-depth discussion in this video Adding a remote repository, part of Git Essential Training.
In the last movie we created a GitHub account and created an initial GitHub repository, and that's on the remote server that we can then connect to. We now need to tell our local repository information about where it can find the remote. When we set up the remote repository, GitHub came up and offered us some helpful information. Go ahead and leave that page up, because we're going to want to come back to that. But first I want to switch over to your command line and make sure that you're inside the root of your project already. If not, you'll want to navigate into it, and from there let's type our first command which is git remote.
So git remote will come up and give us a list of all the remotes that it knows about. It doesn't know about any right now about. So it doesn't return anything. The git remote works a lot like git branch does. We use git branch to see all the branches, git remote shows us all the remotes that we know about. Now the next command we want to do is git remote add. So git remote add will add a remote and then what we want to put in. Don't type this part. Just wait for second. Git remote add and then the alias for what we want to name our remote followed by the URL of where it can find it.
Let's go back and look at that and GitHub page again, because it tells you the name, the URL, where you can find it. That's what this is right here. So you can just copy and paste that, and it suggests the default name of origin. Now this is an alias. You can call it whatever you want. Let's say origin and then that whole long string pasted in. This will create a new remote called origin that points to that remote server at that URL. Now we don't have to call it origin. By convention, typically, you call your primary one origin, that's what most people do.
But if you want you can call it GitHub let's say. I'm going to go head and call mine origin just so we can stick with that default convention. Now I do want to make sure that I call attention to the fact that I'm using a URL which has my name in it, because that's for my GitHub account. Of course, for you, you will want to make sure that you are using the one that has your GitHub account, and from now on if you're using the exercise files where we have a remote defined, you will want to make sure that you take a second to change that remote to point to your GitHub account, not to mine.
So that's very important. You want to make sure that you customize it so that it's looking at your remote repository, and not trying to find the one that I created. Now it's worth noting that you can have more than one remote for your project. We don't have to just have this one remote server out there that we are connecting to. You can have several different ones and in that case you would definitely want to give them each a different name. So now lets try out git remote command again. Git remote, and this says ah, here is origin. Let's do git remote with the -v option after it, that will give us a little more information.
Now it shows us the URL that it's going to use for fetching and the one that it's going to use for pushing. Now typically those are going to be the same ones, but they don't have to be. It could be different ones. We could have a read-only remote that we're fetching from, but we're pushing to one that lets us write. And if you're wondering how Git now knows about these remotes, it stores those in the .git folder of course, in config. Let's take a look that file, and you can see that now it says, okay, I have a remote called origin and here's the URL for it, and these are instructions that Git will use to tell it what information it's going to pull down from the remote repository when it does a fetch.
The last thing I want to show you is just that if you want to remove a remote is just simply git remote rm followed by the alias. So that would remove the origin remote that we just created. So I've removed that. If we now take a look, you will see it's not in our config file, and of course, I can add it again just like that, and now git remote shows me it in the list again. So now our local repository knows about the GitHub repository that we've created. We haven't shared any data yet. None of our branches or commits or anything like that are on the remote repository yet.
At this point, we've just simply created the mechanism by which they can establish a connection so that we can begin pushing our code up to the remote repository, and that's what we will start doing in the next movie.
- Exploring the history of version control
- Installing Git on Mac, Windows, and Linux
- Initializing a repository
- Writing useful commit messages
- Understanding the Git three-tree architecture
- Tracking when files are added, edited, deleted, or moved
- Viewing change sets and comparing versions
- Undoing changes and rolling back to previous versions
- Ignoring changes to select files
- Creating and working with code branches
- Merging branches and resolving merge conflicts
- Stashing changes for later
- Working with hosted repositories and remote branches
- Developing an effective collaboration workflow
Skill Level Beginner
Q: In the Chapter 10 movie "Configuring the command prompt to show the branch," when I type the function "__git_ps1," I do not get the expected result.
A: The function "__git_ps1" was recently moved to a new file, .git-prompt.sh, as described here: https://github.com/git/git/commit/af31a456b4cd38f2630ed8e556e23954f806a3cc.
We will update the video. In the meantime, you may do the same steps you do for .git-completion.bash, but a second time using ".git-prompt.sh" as shown here: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh.
Q: When I use the code the instructor advises in the above video ("git config --global user.name "Nelda Street"), I still get an "Illegal Instruction" error. I have OS 10.6.8. Am I doing something wrong?
A: The current installer version of git isn't compatible with older Mac OS versions.
1. To add "-mmacosx-version-min=10.6" as described here:https://stackoverflow.com/questions/14268887/what-is-the-illegal-instruction-4-error-and-why-does-mmacosx-version-min-10https://stackoverflow.com/questions/10177038/illegal-instruction-4-shows-up-in-os-x-lion
2. Or to use the version of git that comes with Xcode, or to use homebrew to install git instead.http://superuser.com/questions/697144/installed-git-not-sure-how-to-get-it-working