![]() If someone else has pushed content to a remote repo, you need to fetch those changes first, and merge them into your local branch. We’ll do that in a moment, first let’s see why we need to run fetch before push one more time. We need to merge those changes into our local master branch after running fetch. So our master branch keeps outdated if we get new changes from our remote repo when we run fetch. If a remote branch already exists in our local repo, it is updated.īut a fetch command doens’t touch our local branches ( master, etc), only our remote ones ( origin/master, etc). In that case, since that branch didn’t exist in that remote repo yet, there’s nothing to fetch from.Ī fetch command synchronizes our local repo with a remote repo:īy synchronize we understand a copy of all branches in the remote repo to our local repo as remote branches. The only exception is when we’re pushing a new branch to our remote repo, effectively creating that new branch in our remote repo. But It’s a good practice to always run a fetch command before push, as we’ve seen here. We know that to send our local changes to a remote repository we run a push command. Next, let’s see how to update them when someone else pushes code to a remote repo we’re working on. Nice, isn’t it?Ĭool, now I feel you have a better understanding about what are remote branches. What’s coolest is that it’s a local operation, because you have a copy of the contents of your remote repo’s master branch exactly as it is. Note that you can omit remotes/ prefix when referring to remote branches. ![]() You should see some output, including something like Note: checking out ‘origin/master’. You can really switch to remote branches if you want. At this time they are the same, but when you work on a project with other members, they will also update the project’s remote repo, so your remotes/origin/master remote branch will become outdated at some point. Your remotes/origin/master remote branch is a copy of your remote repo’s master branch, a branch living in your remote repo. ![]() In that case, you should see something like: You can also run $ git branch -a to see all branches, local and remote. If you run $ git branch -r now ( -r option means remote), you should see something like: By default, when we push to a remote repo, and there’s no remote branch setup for that local branch we’re pushing, Git creates one for us automatically, and names it as remotes/remote-name/branch-name, so in this case remotes/origin/master. That reference is our remote branch (our remote master branch in this case), created in our local repo. Git should have created a master branch in your remote repo, and created a reference to it in your local repo. That means the operation was successfully completed. Now we can push to our remote repo, so let’s do just that: Note that Git lists two references to it, one for reading from your remote repo (fetch), and another for writing to your remote repo (push). In that case you should see something like: If you want more info about your remote repo you can append -v option to that command: If you run $ git remote now you should see something like: Note that we’re giving that remote an alias of origin, but it could be anything we wanted. Adding a Remote Reference to Your Local RepoĪfter you have setup your remote repo, let’s add a reference to it in your local repo. You can use GitHub, Bitbucket, or any other service you like. Now, take the time to set up a remote repo. Nice, now we have a local repo with some files committed to it. , and then $ git commit -m "adding some files.". Now if you run $ git status you should see your files listed as Untracked files. So now please create a new directory, add a couple files in it, and run $ git init. ![]() Starting a New Local Repoįor the sake of learning, we’ll create a very simple Git project/repo and play with it to better understand branching and remote branches. A bit confusing? I know, it really is at a first look. It’s also ready-only, so you cannot change it, it’s supposed to be updated when we run a fetch command. So a remote branch is a local branch (created in your local repo) that points to and contains a copy of a branch in some remote repo. They’re local branches that you can’t move they’re moved automatically whenever you do any network communication.” (Chacon 50) “Remote references are references to the state of branches on your remote repositories. That’s just an indicator that you should run the command that follows it in your command line tool. Do not type the $ sign you see in the command examples. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |