COURSE 3 – INTRODUCTION TO GIT AND GITHUB

Module 2: Using Git Locally

GOOGLE IT AUTOMATION WITH PYTHON PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

Last updated:

INTRODUCTION – Using Git Locally

In this module, you’ll dive into advanced Git interactions by skipping the staging area for small code changes, and understand how Git uses the HEAD alias to represent checked-out snapshots. Next, you’ll explore how to move and remove files before finally getting a study guide to help you in your Git explorations! The next step of your learning will include how to undo changes before committing and how to amend commits once they’re submitted.

Finally, you’ll be able to identify errors in commits that were submitted a while back. In the final section of this module, you’ll explore the concept of branching and merging. You’ll learn what a branch is, how to create one, and how they work in harmony with you and your code. Once you’re comfortable with branching, you’ll dive into merging, how it works with branched data, and how to deal with merge conflicts.

Learning Objectives

  • Skip the staging area to delete and move files within Git
  • Delete and rename files in a repository
  • Amend and roll back commits
  • Explain the concept of branching and merging
  • Create new branches
  • Use merging to combine branched data
  • Manage and handle merge conflicts

PRACTICE QUIZ: ADVANCED GIT INTERACTION

1. Which of the following commands is NOT an example of a method for comparing or reviewing the changes made to a file?

  • git log -p
  • git diff –staged
  • git add -p
  • git mv (CORRECT)

Nice job! git mv won’t give you any information on changes. Instead, it is used to move or rename a file or directory in Git.

2. What is the gitignore file?

  • A file containing a list of commands that Git will ignore.
  • A file the user is intended to ignore.
  • A file listing uncommitted changes.
  • A file containing a list of files or filename patterns for Git to skip for the current repo. (CORRECT)

Awesome! The gitignore file is a text file that tells Git which files or folders to ignore in a project.

3. What kind of file will the command git commit -a not commit?

  • Tracked files
  • New files (CORRECT)
  • Old files
  • Staged files

Right on! Files that are new and untracked will not be committed before being added.

4. What does HEAD represent in Git?

  • The subject line of a commit message
  • The top portion of a commit
  • The currently checked-out snapshot of your project (CORRECT)
  • The first commit of your project

Great work! In all cases, HEAD is used to indicate what the currently checked-out snapshot is.

5. If we want to show some stats about the changes in a commit, like which files were changed and how many lines were added or removed, what flag should we add to git log?

  • –stat (CORRECT)
  • –patch
  • -2
  • –pretty

Excellent! This will cause git log to show some stats about the changes in the commit, like which files were changed and how many lines were added or removed.

6. If we’re making a small change and want to skip the staging step, which two flags do we need to add to the git commit command? Check all that apply.

  • -m (CORRECT)
  • -t
  • -l
  • -a (CORRECT)

Right on! The -m flag allows us to directly add the commit message to the command.

Awesome! The -a flag lets us add and commit in the same step

7. If we want to see a specific commit, which command would we use along with the commit ID?

  • git log –stat
  • git show (CORRECT)
  • git log -p
  • git commit -am

You nailed it! Taking the commit ID, git show will show information about the commit and its associated patch.

8. If we need to delete a file from our repository, we’ll need to run a command to delete the file and then stage and commit the change. Which command would we use to delete the file?

  • git rm (CORRECT)
  • git mv
  • git diff
  • git del

Woohoo! This command removes files from the working tree and from the index.

Practice Quiz: Undoing Things

1. Let’s say we’ve made a mistake in our latest commit to a public branch. Which of the following commands is the best option for fixing our mistake?

  • git revert (CORRECT)
  • git commit –amend
  • git reset
  • git checkout — <file>

Nice job! git revert will create a new commit to reverse the previous one, and is the best option for undoing commits on public branches.

2. If we want to rollback a commit on a public branch that wasn’t the most recent one using the revert command, what must we do?

  • Use the git reset HEAD~2 command instead of revert
  • Use the revert command repeatedly until we’ve reached the one we want
  • use the commit ID at the end of the git revert command (CORRECT)
  • Use the git commit –amend command instead

Nice work! The commit ID is a 40-character hash that identifies each commit.

3. What does Git use cryptographic hash keys for?

  • To secure project backups
  • To guarantee the consistency of our repository (CORRECT)
  • To encrypt passwords
  • To identify commits

Woohoo! Git doesn’t really use these hashes for security. Instead, they’re used to guarantee the consistency of our repository.

4. What does the command git commit –amend do?

  • Start a new branch
  • Create a copy of the previous commit
  • Delete the previous commit
  • Overwrite the previous commit (CORRECT)

Awesome! The command git commit –amend will overwrite the previous commit with what is already in the staging area.

5. How can we easily view the log message and diff output the last commit if we don’t know the commit ID?

  • git show (CORRECT)
  • git identify
  • git log
  • git revert

Right on! The git show command without an object parameter specified  will default to show us information about the commit pointed to by the HEAD.

6. What is the purpose of the git checkout command?

  • It finalizes staged changes.
  • It reverts changes to modified files before they are staged. (CORRECT)
  • It skips staging and directly commits.
  • It displays the current status of the commit.

Right on! git checkout restores files to the latest stored snapshot, reverting any changes before staging.

7. What does the git commit –amend do?

  • Add an error log to the commit.
  • Remove files from the staging area.
  • Change the commit message.
  • Overwrite the previous commit. (CORRECT)

Awesome! git commit –amend allows us to modify and add changes to the most recent commit.

8. Which of the following is true about the git revert command?

  • It undoes a commit as though it never happened.
  • It creates a new commit with inverse changes. (CORRECT)
  • The output of git revert is not the same as a regular commit.
  • It does not include the ID of the commit that was reverted.

Awesome! With git revert, a new commit is created with inverse changes. This cancels previous changes instead of making it as though the original commit never happened.

9. Which of the following is NOT true about the SHA1 hash numbers that Git uses to identify commits?

  • They provide the consistency that is critical for distributed systems such as Git.
  • They are created using the commit message, date, author, and the snapshot taken of the working tree.
  • Git requires the entire hash ID to identify a commit. (CORRECT)
  • They are composed of 40 characters.

You nailed it! Git can identify a commit using the first few hash numbers as long as there is only one matching possibility.

PRACTICE QUIZ: BRANCHING & MERGING

1. When we merge two branches, one of two algorithms is used. If the branches have diverged, which algorithm is used?

  • three-way merge (CORRECT)
  • fast-forward merge
  • merge conflict
  • orphan-creating merge

Excellent! A three-way-mergeoccurs when the two commits have diverged previously, and a new commit is created.

2. The following code snippet represents the result of a merge conflict. Edit the code to fix the conflict and keep the version represented by the current branch.

Screenshot 2024 01 18 000612

Answer:

print("Keep me!")

You got it! No more conflicts here!

3. What command would we use to throw away a merge, and start over? 

  • git checkout -b <branch>
  • git merge –abort
  • git log –graph –oneline (CORRECT)
  • git branch -D <name>

Right on! If there are merge conflicts, the –abort flag can be used to abort the merge action.

4. How do we display a summarized view of the commit history for a repo, showing one line per commit? 

  • git log –format=short 
  • git branch -D <name>
  • git log –graph –oneline (CORRECT) 
  • git checkout -b <branch>

Awesome! The commandgit log –graph –oneline shows a summarized view of the commit history for a repo.

5. The following script contains the result of a merge conflict. Edit the code to fix the conflict, so that both versions are included.

Screenshot 2024 01 18 000818

Answer:

def main():


    print("Start of program>>>>>>>")


    print("End of program!")


main()

Great work! Now the code has both versions without any conflicts!

6. What is the purpose of organizing repositories into branches?

  • To enable changes to be worked on without disrupting the most current working state. (CORRECT)
  • To make it easier to undo commits.
  • To enable changes to the repository to permanently replace previous commits.
  • To give users a place to keep notes.

Awesome! By creating a new branch, we can experiment without breaking what already works.

7. What does the git checkout -b new branch command do?

  • Switches to another branch and immediately commits.
  • Checks if the new branch is ahead of the main branch.
  • Merges two branches.
  • Creates a new branch and switches to it. (CORRECT)

Nice job! It’s very handy to be able to start a new branch, and switch to it in a single command.

8. How does git checkout switch branches?

  • By creating a new commit on a new branch.
  • By updating the working tree to match the selected branch. (CORRECT)
  • By moving the HEAD to the previous commit.
  • By amending the commit with the provided ID.

Right on! The HEAD is moved to the relevant commit on the specified branch.

9. What happens when we merge two branches?

  • The HEAD points at the master branch.
  • Both branches are pointed at the same commit. (CORRECT)
  • One of the former branches disappears.
  • Two independent snapshots will now share the same name.

Awesome! Merging combines branched data and history together.

10. What’s the advantage of Git throwing a merge conflict error in cases of overlap?

  • It prevents loss of work if two lines overlap. (CORRECT)
  • It helps us understand which changes to keep.
  • It warns us of all potential problems.
  • It tells us whether the commit is a merge

Nice job! If two lines have differences Git is unsure about, it’s best we decide than risk losing work forever.

MERGING BRANCHES IN GIT

1. What is the purpose of the “git commit” command, and how does it contribute to the version control process?

  • The “git commit” command is used to push local changes to a remote repository, ensuring that the latest code is always available to collaborators.
  • The “git commit” command is used to save the current state of changes in the local repository, creating a snapshot of the code at that point in time and enabling a detailed history of revisions. (CORRECT)
  • The “git commit” command is used to revert code changes to a previous state in the Git repository, undoing any recent modifications.
  • The “git commit” command is used to create a new branch in the Git repository, allowing developers to work on isolated features independently.

Correct

2. In the Git repository “food-scripts,” what command can be used to check the current status of the working tree, including paths with differences between the index file and the current HEAD commit?

  • git status (CORRECT)
  • git log
  • git diff
  • git branch

Correct

3. What Git command is used to revert a specific commit, effectively undoing the changes introduced by that commit, while still keeping a record of the undo operation in the Git history?

  • git reset
  • git revert (CORRECT)
  • git checkout
  • git commit –amend

Correct

4. You and your colleagues are working on a software development project, and you’ve decided to use Git for version control and collaboration. What command do team members use to upload their local commits to the remote repository in the Git workflow described in the scenario?

  • git push (CORRECT)
  • git fetch
  • git clone
  • git commit

Correct

5. In the lab, after merging the “improve-output” branch into the “master” branch, what type of merge is indicated by the message “Fast-forward”?

  • It’s a merge strategy that requires manual conflict resolution.
  • It’s a type of merge that combines the changes from both branches automatically. (CORRECT)
  • It’s a merge that creates a new branch to preserve the history of both branches.
  • It’s a merge where the master branch moves forward to include the changes from the feature branch.

Correct

6. Which Git command is used to retrieve changes from a remote repository and merge them into your current branch?

  • git push
  • git branch
  • git checkout
  • git pull (CORRECT)

Correct

7. In a Git repository, what is the purpose of creating and using branches?

  • To create a backup of the repository
  • To clone the repository
  • To permanently delete files and directories from the repository
  • To isolate and work on new features, fixes, or experiments separately from the main codebase (CORRECT)

Correct

8. What does it mean when Git says there is a ‘merge conflict’? 

  • The branch you are trying to merge does not exist.
  • You have forgotten to pull the latest changes before merging.
  • There is a syntax error in your code.
  • Git can’t automatically merge the changes and needs your help to resolve the conflict. (CORRECT)

Correct

9. When would you typically create a new branch in a Git repository?

  • To permanently delete code that is no longer needed
  • To automatically synchronize with remote repositories
  • To merge all changes into the main branch
  • To work on a new feature or bug fix without disrupting the main code (CORRECT)

Correct

10. In a Git repository, what is the purpose of the “git pull” command?

  • To create a new branch
  • To merge changes from a remote repository into the current branch (CORRECT)
  • To view the commit history
  • To push local changes to a remote repository

Correct

11. In the given Git repository named “food-scripts,” what command can be used to view the commit history, including the author’s name, email, date, and commit message?

  • git status
  • git diff
  • git branch
  • git log (CORRECT)

Correct

12. In a Git repository, what command would you use to create a new branch based on the current branch, and what command would you use to switch to that newly created branch?

  • To create a new branch, use git checkout new-branch-name, and to switch to it, use git branch new-branch-name
  • To create a new branch, use git push new-branch-name, and to switch to it, use git pull new-branch-name
  • To create a new branch, use git branch new-branch-name, and to switch to it, use git checkout new-branch-name (CORRECT)
  • To create a new branch, use git merge new-branch-name, and to switch to it, use git commit new-branch-name

Correct

13. How are merge conflicts resolved?

  • By abandoning the merge operation altogether and starting over
  • By manually editing the conflicting files to choose the desired changes (CORRECT)
  • By using the “git force-merge” command
  • By deleting one of the conflicting branches and reentering it later on

Correct

14. What is the primary purpose of merging a branch back into the main branch in a Git repository?

  • To create a backup of the branch and merge it back into the Git repository
  • To combine the changes from the branch into the main codebase. (CORRECT)
  • To delete the branch permanently
  • To rename the branch

Correct

15. What should you consider when deciding whether to create a new branch in Git? 

  • The number of commits in the repository
  • The size of the repository
  • The number of collaborators on the project
  • Whether the changes are significant or experimental. (CORRECT)

Correct

16. What is the purpose of a Git repository, and how does it facilitate version control?

  • A Git repository is a version control system that tracks changes to source code, allowing multiple developers to collaborate, review changes, and maintain a complete history of project revisions. (CORRECT)
  • A Git repository is a secure backup of a software project and serves as a redundant storage solution for critical files.
  • A Git repository is a storage location for software documentation, such as user manuals and guides, and it helps in organizing project documentation.
  • A Git repository is used to store the final, production-ready version of a software project and ensures that it is readily available for deployment.

Correct

17. What command would you use to create a patch file that represents the changes between the current working directory and the latest commit? 

  • git patch > changes.patch
  • git checkout > changes.patch
  • git commit > changes.patch
  • git diff > changes.patch (CORRECT)

Correct

18. In a scenario where you are collaborating with a team on a project using Git, what would be the role of a remote repository?

  • To store individual team member’s versions of the project
  • To store backup copies of the project
  • To store the final version of the project after all changes have been incorporated
  • To act as a central repository where all team members can push and pull changes (CORRECT)

Correct

19. When working with Git, which commands are used to save changes to the repository and stage them for a commit? 

  • git add
  • git pull
  • git commit (CORRECT)
  • git branch

Correct

20. What is the purpose of branching in Git? 

  • To isolate changes for a specific feature or issue (CORRECT)
  • To create a copy of a repository
  • To merge two or more commits
  • To delete a repository

Correct

21. What command is used to change a commit to a previous state in the Git repository?

  • git add
  • git revert (CORRECT)
  • git merge
  • git checkout

Correct

22. In the lab’s Git fast-forward merge, what happened to the branch being merged into?

  • It remains unchanged, and the changes from the other branch are discarded.
  • It deleted the branch being merged into.
  • It created a new branch with the merged changes.
  • It updated to include the changes from the other branch, moving its pointer forward. (CORRECT)

Correct

23. In a Git repository, what command is typically used to combine changes from a shared feature branch into another when you want to integrate new features or fixes?

  • git merge (CORRECT)
  • git log
  • git add
  • git checkout

Correct

24. When working with Git, which command is used to add changes to the staging area when preparing for a commit? 

  • git branch
  • git commit
  • git pull
  • git add (CORRECT)

Correct

25. In the lab’s Git operation, what does the “git merge improve-output” command achieve?

  • It reverts all changes made in the improve-output branch.
  • It combines the changes from the improve-output branch into the master branch. (CORRECT)
  • It creates a new branch named “improve-output” based on the master branch.
  • It deletes the improve-output branch.

Correct

26. Which Git command is used to create a new branch in a Git repository?

  • git commit
  • git merge
  • git branch (CORRECT)
  • git checkout

Correct

CONCLUSION – Using Git Locally


In conclusion, this module has provided a comprehensive exploration of advanced Git interactions, offering valuable insights into efficient code management. By delving into techniques such as bypassing the staging area for minor code changes and understanding the utilization of the HEAD alias in Git, you have acquired a deeper understanding of version control. The module further equipped you with knowledge on moving and removing files, complemented by a study guide to enhance your Git proficiency.

As you progressed through the learning journey, you gained valuable skills in reverting changes before committing, amending commits post-submission, and identifying errors in commits made in the past. The module’s concluding section focused on branching and merging, elucidating the concept of branches, their creation process, and their collaborative function with you and your code. As you attain proficiency in branching, the exploration extends to merging, shedding light on its interaction with branched data and offering effective strategies for managing merge conflicts. Armed with these advanced Git techniques, you are well-prepared to navigate the complexities of version control in your coding endeavors.