1. What are the four different phases of git that we’ve learned? What commands do you know to move changes from one phase to the next? Is there ever more than one way to move a change from one phase to another?

    Solution

    The four git phases are the working directory, the staging area, the local repository, and the remote repository. git add/git stage moves changes from the working directory to the staging area (note both commands do the same thing). git commit moves changes from the staging area to the local repository.git push` moves changes from the local repository to the remote repository.

  2. What is the difference between git status and git log?

    Solution

    git status displays changes in the working directory and staging area, whereas git log displays the history of changes in the local repository.

  3. What is the command to show the changes between our working directory and our staging area? How about between our staging area and the local repository?

    Solution

    git diff shows the changes between the working directory and the staging area. git diff --staged shows the changes between the staging area and the local repository.

  4. Suppose you’re in a random directory on your computer and you want to check if it’s a git repository. How might you check this, without using any git commands?

    Solution

    A git repository is managed by the .git directory. So, to see if a given directory is a git repository, you could run ls -a and see if a .git directory exists.

  5. As you grow more experienced with git you’ll find yourself typing a lot of long commands which can be cumbersome. Luckily for us, git allows to alias many of the commands we run so we can rename them. So, for example, let’s say we want to alias status to simply st. If we wanted to do this for all git repositories on our computer, we could type

    git config --global alias.st status
    
    Or, if we want to alias this for just the git repository we’re currently using:
    git config alias.st status
    
    Write your own global alias for commit that makes sense for you!

    Solution

    This is an open ended question. Next week we’ll learn about git checkout, but I think this command is really long and cumbersome. Therefore, I’ve aliased it globally to git ch with the command git config --global alias.ch checkout.

  6. Caution

    The command we’re about to discuss can be very dangerous. Use this at your own risk, but it can be very useful if used correctly!

    The git reset command allows us to move changes out of a commit and back into the staging area. However, this command can be very dangerous if you’ve already pushed your changes to remote. Can you think of why this would cause problems? What might be a good rule of thumb for when to use something like git reset?

    Solution

    To understand the issue here we need to have a basic understanding of commits. Commits are uniquely identified by their hash, which is just a 40 character long string. If you run git log, the long string you’ll see after commit: is that commit’s hash. When you push, the remote repository tries to build the history by matching the hashes between the commit history that you pushed and the commit history it has saved.

    Consider the following scenario, you have a local commit history which looks like this (the left number is the hash for that commit)

    Local History 
    
    abc123   Add file 2
    dnekd2   Add file 1 
    fkdiek   Initial commit
    
    Now, you push this to the remote repo. The history on remote now looks the same, i.e.
    Remote History 
    
    abc123   Add file 2
    dnekd2   Add file 1 
    fkdiek   Initial commit
    
    Now imagine we run git reset, removing our most recent commit from the local history. We now make some more changes and add and commit them with the message Add file 3. Now, our local history looks like this:
    Local History 
    
    lmnopq   Add file 3
    dnekd2   Add file 1 
    fkdiek   Initial commit
    
    Note that the hash of the new commit has changed, since hashes uniquely idetify each commit. Now, if we try to push this change our changes will be rejected, since the remote repository cannot determine which commit should follow dnekd2.

    Generally we want to avoid changing our commit history because it can cause problems like this. A good rule of thumb is that you should never modify any commits that have already been pushed to the remote repository.