Remote Branches 1
(these posts are part of the material I cover in my devops course)
Create Repositories
- Go to your github account anc create a public repository.
(I have created one called demo) - This is just a training session, so you will remove this repository when you're done.
- Locally, create a working directory (called demowork), and 2 directories under that:
1mkdir demowork
2cd demowork/
3mkdir a
4mkdir b
- Clone your demo repository into a and b:
1cd a/
2clone <your repository url>
3cd ../b/
4clone <your repository url>
Branch feature1
- Goto your demo repository in a
- Configure your user:
1$ git config user.name "Yuval Shaul"
2$ git config user.email "yuval.shaul@gmail.com"
3$
- Check git status:
1cd ../a/demo
2git status
- As you can see, you are in the master branch.
- In fact, there are no branches created at all.
If you try git branch you'll see no branches. The master you see is just a default name that will be use only if you commit anything. - Go ahead and create another branch and switch to it.
Then create a file, add, commit and try to push:
1git checkout -b feature1
2echo hello > feature1.txt
3git add feature1.txt
4git commit -m "adding file to feature1 in a"
5git push
- The last push command fails with something like this:
1fatal: The current branch feature1 has no upstream branch.
2To push the current branch and set the remote as upstream, use
3
4 git push --set-upstream origin feature1
so we are not tracking a remote branch.
Tracking a remote branch
- The push command has many options, but if these are not configured then the current branch is pushed to the corresponding upstream branch.
The problem is that we don't have a current upstream branch, so we have to create one.
We'll push using the suggested upstream configuration:
1git push --set-upstream origin feature1
- Now we can see how are local feature1 branch tracks the remote one. Type:
1git branch -vv
- Let's repeat everything with another branch called feature2:
1git checkout -b feature2
2echo hello > feature2.txt
3git add feature2.txt
4git commit -m "first commit for feature2"
5git push --set-upstream origin feature2
Remote Branches
- Use the follwing command to inspect all remotes:
1git remote -v show
- Try the following to see ALL branches (including remote branches):
1git branch -a
- Try this to see only remote branches:
1git branch -r
- Add -v to see more details:
1git branch -r -v
Updating info
- Go to b/demo and look for branches:
1cd ../../b/demo/
2git branch -a
You should see no brances at all.
- You can update by using the git fetch command:
1$> git fetch
2remote: Enumerating objects: 5, done.
3remote: Counting objects: 100% (5/5), done.
4remote: Compressing objects: 100% (3/3), done.
5remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
6Unpacking objects: 100% (5/5), 438 bytes | 438.00 KiB/s, done.
7From github.com:YuvalShaul/demo
8 * [new branch] feature1 -> origin/feature1
9 * [new branch] feature2 -> origin/feature2
- Let's look that the branches that were updated:
1$> git branch -a
2 remotes/origin/feature1
3 remotes/origin/feature2
- Try looking at the log of these remote branches:
1git log remotes/origin/feature1
2git log remotes/origin/feature2
- Create a local branch to track the remote one.
See the results:
1git checkout -b feature1 origin/feature1
2git status
3ls
Note that you have switched to the new local branch and that everything is updated from the remote branch.
- Now, let's do the same thing differently:
1git branch newbr origin/feature2
2git branch -a
3git checkout newbr
4ls
Note:
We have created a new branch, set it to track a remote one (where names do not match).
Checking out to the new local branch shows the created files.