Linkedin: Git Essential Training : Q&A

1. Why Do You Want to Use Git?

Q1. Where is Git located on your computer?
A1. It is installed as separate software. 
Note: Although sometimes Git is part of the OS, overall, you will install it from the internet.

Q2. Why would you use Git for your own files?
A2. To keep track of old versions of files.
Note: Git helps make sure you can go back to an old version, without saving multiple copies.

Q3. You and your coworker want to use Git to work on the same code. How do you do that?
A3. You store a version of the files online at a Git provider.
Note: When you store the code at a Git Provider, you and your coworker can both get a local copy and work on that.

Q4. Fred wants to use Git to share a file from his computer with his friend Sally. What is not required to accomplish this?
A4. Sally must have an account with a Git provider.
Note: While Fred might prefer to give Sally direct access, it is not necessary. He could make his code public and share the URL with Sally.

Q5. What problem does branching solve?
A5. When a lot of people work on the same file, Git might not be able to merge the different files.
Note: Branching solves more problems than this, but this is one of them.

Q6. You and your coworker are working on the same file at the same time. What will happen when you push the changes?
A6. Git will try to merge the changes into one file.
Note: Git has some intelligence to merge code, even if it is changed at the same time. If it is not possible, you can fix it manually.

Q7. You have made a branch and are working on a file. What can you expect when you look at the file in the remote repository?
A7. The files will look just as they did when originally accessed.
Note: Unless and until changes are merged into the master repository, you work on your local repository, so those changes remain local.

2. How Does Git Work?

Q1. When you use Git Pull, where can you view the old, new, and changes files?
A1. They are visible in the local folder.
Note: Pull adds the file to the local repository and directly to the local folder.

Q2. When you use Git push, what changes are pushed to the remote repository?
A2. The changes in the local repository.
Note: Only changes in the local repository will be pushed.

Q3. You are working on a Git repo but must do some work without an internet connection. What can you not do?
A3. Push your changes to the remote repository.
Note: To push the changes, you need to connect to it through the internet. All other actions can be performed even without internet.

Q4. What is the effect if a file in the main repository becomes corrupted?
A4. There is no effect on users' local copies.
Note: Because Git is distributed, the file in users' repositories will be unaffected by any changes to the file in the main repository unless users attempt to retrieve the corrupted file.

Q5. Which command do you need to use to add the staged changes to the local repository?
A5. git commit
Note: Git commit will add the staged changes to the local repository, don't forget to add a commit message!

Q6. What do you need to know to start working with Git?
A6. You can get started straight away, which will help you to understand how Git works.
Note: You can get started straight away, which will help you to understand how Git works.

Q7. Where is the repository information stored in your local folder?
A7. In the .git folder.
Note: The .git folder stores all information and configuration.

3. Install and Configure Git

git-scm.com to download git

(Recommended) Use visual studio code for editing (code.visualstudio.com)
(Recommended) Use main (same default as Github)

Also consider GUI clients from git-scm.com (like Visual Studio.)

Q1. Ways to gain access to Git on a Mac?
A1a. Use the pre-installed version on newer Macs.
A1b. Install Git with the HomeBrew command brew install git.
A1c. Install Git with the MacPorts command: sudo port install git from git-scm.com.
Note: The terminal utility on a Mac does not have an install command. HomeBrew commands can be used on the terminal command line instead.

Q2. Which action will successfully install Git on any Linux system?
A2. Use the command specific to the Linux distribution, as listed on git-scm.com.
Note: The Git website lists commands for each distribution of Linux to ensure users can properly install Git on their specific system.

Q3. What is the best way to find out what GUIs are available for your OS?
A3. There is a list on git-scm.com.

Q4. Why would you use a GUI for Git?
A4. To help you keep track of what happens to files and what commands to use
Note: While a GUI isn't needed, it surely does help to visualize what is happening.

Q5. A user is unsure of the version of Git installed. Which command should they use?
A5. git --version
Note: The user might also be able to type simply git version to learn the installed version of Git.

Q6. Which two settings are mandatory to start working with GIT and remote repositories?
A6. user.name and user.email
Note: You need to define the user.name and user.email setting to push to remote repositories.

Q7. How can you check if Git has been correctly installed?
A7. Open a terminal or PowerShell window and type either Git or Git --Version.
Note: This is the best way to check if Git installation has succeeded if you use --Version.

Q8. What does "Add 'open with code action' to Windows Explorer File Context menu" do?
A8. It adds an option to the right-button-menu in the file explorer to open a file directly with Visual Studio Code.
Note: With this option you can open Visual Studio Code directly from a file.

Q9. What settings are necessary to change in the install wizard to use Git?
A9. none
Note: There are a lot of settings, but you can use the defaults on all of them if you want.

Q10. Why would you use Visual studio code for Git?
A10. It gives a gui to help with basic Git operations.
Note: With Visual studio code, you can use buttons instead of commands. This can be very helpful when you are new to it.

Q11. What scope does the git config --list command use if no scope is defined?
A11. global
Note: By default, Global is used. If you want to use another scope, you need to define it.

Q12. How do you change the username in the global configuration to "John Doe"?
A12. git config --global user.name "John Doe"
Note: This will change the global username in the config.

4. Push Your Code with GIT

Q1. What is the purpose of the .git folder?
A1. It stores all configuration and versions of the repository.
Note: The .git folder is what makes the folder a Git repository. All data and configuration is stored there.

Q2. Who can view and commit to a private repository?
A2. Only you and people you have invited.
Note: You can control who can commit and view to your repository by inviting their account.

Q3. You have committed new changes on your local device. You sign into the repository in the webinterface of the Git provider. What do you see?
A3. The old versions of the files
Note: All your changes are only local, so the files in the Git provider will be the old unchanged versions.

Q4. Examples of remote Git providers:
A4. GitLab, GitHub, Bitbucket

Q5. What will git init write to a directory?
A5. A .git directory.
Note: Git init initializes the directory with a .git directory containing project-specific files.

Q6. Say your repository is stored at https://git.com/user/repo.git. How do you make a local copy?
A6. Git Clone https://git.com/user/repo.git
Note: This will clone the repository as a new folder in the current one.

Q7. How can you recover data from a deleted .git directory?
A7. A deleted .git directory's contents cannot be recovered.
Note: Git keeps all project tracking data in the .git directory, and when it is deleted, all the tracking data goes with it.

Q8. How do we get the URL we need to clone a local copy?
A8. It will be available at the Git provider's web interface of the repository.
Note: Depending on the provider you use, you can find some sort of " clone" button on the web interface of your repository.

Q9. You have made changes to two files: example01.txt and example02.txt. You use git add example01.txt. What will happen when you commit the changes?
A9. Only the changes in example01.txt will be committed.
Note: Since you have added example01.txt to the staging area, only this file will be committed.

Q10. Which command do you use to push new code to the remote repository?
A10. git push
Note: Git Push takes the changes that have been committed and syncs them to the remote repository. No options are needed.

Q11. You have made changes to two files: example01.txt and example02.txt. You use git add example01.txt. You use 'git commit' What will happen?
A11. Your text editor will be open.
Note: Git will not directly commit anything as there is no commit message yet.

Q12. How do you connect your local repository to a remote repository?
A12. git remote add
Note: You add the repository by using Git Remote add and then defining the remote repository.

Q13. How do you stage every file you have changed?
A13. git add .
Note: The dot in this syntax stands for all files, so it will stage all of them.

5. Making Changes to Files

Q1. Which command can you use to rename a file in Git?
A1. git mv
Note: Git mv with the right options will rename a file.

Q2. You change the name of a file in the file explorer. What happens when you run Git status?
A2. Git will say a new file has been added and a file has been removed.
Note: At first glance, Git will think there are two separate files. Once you add them to the staging area, Git will understand that the file has been renamed.

Q3. What is the purpose of a .gitkeep file?
A3. It serves as a placeholder so a folderstructure can be committed.
Note: While you can enter any placeholder file you want, this file is known for this purpose.

Q4. You create a new folder in a repository. You add it with git add . You commit the changes. What will happen?
A4. Nothing, as Git doesn't keep track of empty folders.
Note: Git keeps track of files, not folders. The folder will only be saved if a file is in there.

Q5. How do you view the changes in all previous commits?
A5. Git log -p
Note: By adding the option -p, all changes are shown in the history.

Q6. What key closes the commit log view?
A6. Q
Note: When you press Q, you will get to the prompt. The alternative is to keep pressing space, but that could take a long time!

Q7. What happens when you delete a file from a Git repository and commit your changes?
A7. It is deleted from the current code, but still visible in the history.
Note: While the code is no longer visible in the current files, it is saved in the history.

Q8. How do you find all the commits with the phrase 'example' in them?
A8. git log --grep='example'
Note: You use grep to search strings. You can use Regex as well!

Q9. How do you discard all changes you made in the working directory?
A9. git restore .
Note: This will remove all changes you have made.

Q10. Git status tells you "Your branch is ahead of 'origin/main' by 1 commit. What does this mean?
A10. You have committed changes that are not in the remote repository.
Note: You can fix this situation by using Git Push.

Q11. You have staged some changes in the file example.txt, but you decide you don't want to commit them. Which command do you use?
A11. git restore --staged example.txt
Note: This will bring the file back to the working directory.

Q12. What is the most likely cause if git status results in the output "Not a Git repository"?
A12. There is no .git directory in the current directory or in parent directories.
Note: Git will check parent directories to confirm the user is not in a subdirectory of a repository.

Q13. How can you view the changes in the staging area?
A13. git diff --cached
Note: Using --cached will show you the changes in the staging area specifically.

Q14. Considering the following output, if you decide you don't like the change shown in this diff, where should you go to find the output line(s)?

--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@ 

-This is a new line
+This is a new line
+
+And another one

A14. line 1
Note: The information between the "@@" symbol pair is the starting point (preceded by the dash) and the ending point (preceded by the plus), in this case, line 1.

Q15. You have four commits in your project, and the HEAD is the most recent. What do the Git logs show after reverting the third commit?
A15. The logs show a fifth new commit indicating which commit was reverted.
Note: History will not be rewritten in Git; the log will show that a commit was reverted.

Q16. Which part of Visual Studio Code do we use for Git?
A16. Source Control
Note: Git is Source Control, so you can manage your Git files from this section.

Q17. Which extensions do you need to install to work with Git in Visual studio code?
A17. none
Note: While there are some extensions that can help with using Git, it is not needed to install them.

Q18. How do you go back to a specific commit?
A18. git checkout
Note: You use git checkout to look back at a specific commit.

Q19. Which command do you use to view the changes in the files you are going to commit?
A19. git diff
Note: Git diff will show you the lines in the code that have been changed.

Q20. In what way does the commit history change when git revert is executed?
A20. HEAD points to a new revert commit that is a descendent of the previous HEAD.
Note: Because Git prioritizes integrity of data, the ancestry of HEAD will not change -- a new commit that reverts the prior commit's changes is added.

Q21. What does a detached state mean?
A21. The files you see are not connected to the current version of your code.
Note: You are in some part of the history of your git repository, so you cannot commit anything in the current branch.

Q22. You have a file in the staging area. What changes will Git Status make to your repository?
A22. Nothing. Git Status shows what your current state is, it will not change that state.

6. Important Concepts in Git

Q1. What file do you need to add to make Git ignore certain files?
A1. .gitignore

Q2. Which line in a .gitignore file would ignore all files ending in ".md" in a directory named logfiles, and nothing else?
A2. logfiles/*.md

Q3. You make some changes to your repository and push them to the remote repo. You get an error: "failed to push some refs to [url]". What went wrong?
A3. You first need to run git pull.
Note: This error means there is a difference between the remote repository and the local repository. When you run git pull, it will collect the changes from the remote repository.

Q4. How many branches does a repository need to work?
A4. 1
Note: Every repository has 1 branch, as the main branch is also a branch!

Q5. Why do you create a pull request?
A5. To get the code from a branch added to the main branch
Note: A pull request is used to get code to a main branch and enable automatic testing and reviews.

Q6. Which command helps to create a multiline git commit message?
A6. git commit
Note: When you use git commit without the m option, it will open a text editor for you to create a message.

Q7. Which single line commit message uses best practices?
A7. Adds dashboard feature to display client usage log summary as pie chart
Note: This message is written in present tense, gives an overview of the change made in the commit, stays within character limits, and does not include unnecessary information.

Comments