Module 2: Using Git Locally

Spread the love

INTRODUCTION – Using Git Locally

Go into advanced Git interactions skipping staging area for smaller changes in code and know how Git uses an alias to head, which represents a point to the snapshots now that it is on record. You can then proceed to explore how you may move or remove files before you do have a survey, which has been made as a sort of study guide to help you venture into Git.

Then at a stage, it will show you how to undo the things you changed and amend those changes after you have committed them. By the time you get through all this, you will know how to tell at a glance what changes have been made to earlier submitted commits. The last part of the module is about branching and merging. You will appreciate what branch actually is, how to create one, and how it matters in assisting you in managing code to tide you through learning. This way, once you have mastered branching, you can dive right into merging and explore what would make it tick within branched data, leading to how you handle those merge conflicts.

Learning Objectives:

  • Leave without staging touch delete and move within git.Config repository to allow deletion and renaming of files.Rollback and change git commits.
  • Define branch and merge, with a key objective being how one creates branches and what their utility is when working with you and your code.
  • Create new branches, and effect merging or combining of branched data. Sorting of the resulting 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! Running git mv will not show file edit information, but is used to move or rename a file or directory in a specific directory within the Git repository.

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! It’s a text file that tells Git which files or folders to ignore within 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 which are new in addition to being untracked are not yet going to be committed as long as they are not staged.

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! A HEAD is to identify the snapshot that is currently checked out in Git in all cases.

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! Using git log –stat option, one can see the stats of the committed changes including changed files, the number of lines added, and 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! It is possible to examine the commit along with the relevant information like the 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! The creation of a new commit to counteract the changes brought about by the previous commit is what git revert does. You get a copy of a commit reversion, with the best solution being to commit git’s public branch commits.’

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

Good job on the work! Each commit made in Git is distinctly identifiable through its 40-character SHA-1 names – commit ID.

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 does not really rely on these hashes for security but to keep the integrity and consistency of its 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 git commit –amend command overwrites the previous commit, replacing it with the current contents of 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?

  • 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! When you use git checkout for files, it will recover a right version of the most recent commit. This provides a way to undo any changes that have been created as compared to those in the staging area line. This should undo all changes from the user directory.

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

  • Stop intrusive activity
  • Monitor system and network activity (CORRECT)
  • Collect and analyze system information for abnormal activity (CORRECT)
  • Alert on possible intrusions (CORRECT)

IDS is an application that keeps track of system and network activities and generates alerts for suspected intrusions. It also retrieves and analyzes system data to detect any abnormal or unusual behavior.

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 lets you simply alter the latest commit if you need to add anything or fix that commit message. Essentially, this updates the previous 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! When an attempt is made to revert changes on a comparative basis, git revert is a technique rather than deletion: it instead triggers the introduction of a new commit that actually does the change in reverse and then negates the effects of the prior commit.

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! The Git can identify a commit by addressing the first few characters of its hash as long as there is a unique computation.

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 merge occurs when changes have been applied to two branches, and thus they differ. Such a commit usually caters for a merge of diverging branches.

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! In situations where any error takes place, the command –abort can be used along with git merge. It will enable about the conduction of merging task while also reverting the repo as far as possible when encountering merge errors.

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! We do this by creating a new branch so that experimenting with changes and new features will have no impact on the main code base and whatever is working already will remain undamaged.

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! Moving between branches means the branch is altered so that any changes are then relevant to the specified branch. Commit at that place is the repository’s state at the time.

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! While defining conflicting differences between the two lines git is unsure of when you merge or rebase conflicting differences, it is more advisable to resolve the conflict by hand instead of losing a good portion of the job. The respective areas of the conflict are clearly marked by git allowing us to evaluate and determine the correctness of the changes.

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

Well done! This module has absolutely provided you with a good understanding of advanced Git concepts and tools. Now, you are all set for complex version control systems, where-learning about bypassing the staging area, using the HEAD alias, managing files, and moving between commits-also knowing good details on branching and merging has actually developed your competence to work with differences, collaborate, and sort out conflicts perfectly.

These skills are becoming formed in codebase management, specifically in faster and more confident development as you continue to work on different projects. Keep practicing: It is very clear that your Git skills are getting better.

Leave a Comment