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 -aor run a bash script like the one in this StackOverflow thread.
- Create a new branch answersand switch to it
- Make sure you are on branch answers, notmaster. Usegit statusandgit branchto see where you are
- git branchcan accept many arguments that we didn't look at in the lecture. You can use- git branch --no-mergeto see branches that have not been merged into the current branch or- git branch --mergeto 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.txtand 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 packagethat don't belong to the branchmaster?
 
- Switch back to the branch master.
- Create a new file 7.txtand commit it to the repository
- Look at the commit history with branches. See how the branch answersbranches out frommaster
- Merge the branch answersintomaster
- Look at the commit history with branches. See how the branch answersmerges back intomaster, but the pointer ofanswersis 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 --allto push information of all branches.
- Push all changes to the remote using git push --all
- Use git branch --mergeto 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