Exercises Git Advanced#

1 Explaining options#

  1. What do the --oneline and -n options for git log do?

  2. What other options does git log have that you would find useful?

2) Modifying prompt#

Modify your shell prompt so that it shows the branch you are on when you are in a repository.

3) Ignoring files#

GitHub maintains [a collection of .gitignore files][github-gitignore] for projects of various kinds. Look at the sample .gitignore file for Python: how many of the ignored files do you recognize? Where could you look for more information about them?

4) Creating the same file twice#

Create a branch called same. In it, create a file called same.txt that contains your name and the date.

Switch back to main. Check that same.txt does not exist, then create the same file with exactly the same contents.

  1. What will git diff main..same show? (Try to answer the question before running the command.)

  2. What will git merge same main do? (Try to answer the question before running the command.)

5) Deleting a branch without merging#

Create a branch called experiment. In it, create a file called experiment.txt that contains your name and the date, then switch back to master.

  1. What happens when you try to delete the experiment branch using git branch -d experiment? Why?

  2. What option can you give Git to delete the experiment branch? Why should you be very careful using it?

  3. What do you think will happen if you try to delete the branch you are currently on using this flag?

6) Tracing changes#

Chartreuse and Fuchsia are collaborating on a project. Describe what is in each of the four repositories involved after each of the steps below.

  1. Chartreuse creates a repository containing a README.md file on GitHub and clones it to their desktop.

  2. Fuchsia forks that repository on GitHub and clones their copy to their desktop.

  3. Fuchsia adds a file fuchsia.txt to the master branch of their desktop repository and pushes that change to their repository on GitHub.

  4. Fuchsia creates a pull request from the main branch of their repository on GitHub to the main branch of Chartreuse’s repository on GitHub.

  5. Chartreuse does not merge Fuchsia’s PR. Instead, they add a file chartreuse.txt to the main branch of their desktop repository and push that change to their repository on GitHub.

  6. Fuchsia adds a remote to their desktop repository called upstream that points at Chartreuse’s repository on GitHub and runs git pull upstream main, then merges any changes or conflicts.

  7. Fuchsia pushes from the main branch of their desktop repository to the main branch of their GitHub repository.

  8. Chartreuse merges Fuchsia’s pull request.

  9. Chartreuse runs git pull origin main on the desktop.