Practice 4: Branching
By the end of this practice, you will know how to
- List, create, delete branches in Git
- Switch branches in Git
- Merge branches in git
Branching
- Fork (see last practice) the repository for this practice: https://github.com/CSE-2020/cse-prac4-branching
- Look at the list of branches of this repository on Github
- Clone your fork of the repository to your computer
- Look at the list of branches on your local copy of the repository. What do you notice?
- When we clone a repository, we don't actually download all the branches, so now you should only have one branch -
master
. Run the commandgit branch -a
. What do you notice? - When you switch to a branch that exists on a remote, git automatically creates this branch locally as well. Git sadly doesn't have a good command to download references to all branches from remote and create local branches for it. We can only see the list of all remote branches with
git branch -a
or run a bash script like the one in this StackOverflow thread. - Create a new branch
answers
and switch to it - Make sure you are on branch
answers
, notmaster
. Usegit status
andgit branch
to see where you are git branch
can accept many arguments that we didn't look at in the lecture. You can usegit branch --no-merge
to see branches that have not been merged into the current branch orgit branch --merge
to see branches that have been merged- Use the commands introduced in the lectures or any third-party software to inspect the repository. Find answers to the following questions; write them to a file
answers.txt
and commit the file to the repository:- What branches exist in this project?
- What branch has been merged into master?
- How many commits (in total) does the repository have?
- What commits belong to branch
package
that don't belong to the branchmaster
?
- Switch back to the branch
master
. - Create a new file
7.txt
and commit it to the repository - Look at the commit history with branches. See how the branch
answers
branches out frommaster
- Merge the branch
answers
intomaster
- Look at the commit history with branches. See how the branch
answers
merges back intomaster
, but the pointer ofanswers
is still on a separate branch. - By default, Git pushes only the current branch. You need to push a branch explicitly, as shown in the lecture. Or you can use
git push --all
to push information of all branches. - Push all changes to the remote using
git push --all
- Use
git branch --merge
to see what branches have been merged into master and delete them. When deleting branches, remember that Git doesn't delete remote branches. usegit push origin --delete <branch>
to delete the required branches also in the remote