Learning Git Flashcards

1
Q

What is a repository?

A

A repository is a copy of a Git project.
A repository (also called repo) is how we refer to a project version controlled by Git.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a commit?

A

A commit in Git is basically one version of a project. You can think of it as a snapshot of a project, or a standalone version of a project that contains references to all the files that are part of that commit.

Every commit has a commit hash (also called commit ID).

Every time you want to save a new version of a project, you can make a commit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Git?

A

Git is a technology used to track changes to a project and to help multiple people to collaborate on a project. At a basic level, a project version controlled by Git consists of a folder with files in it, and Git tracks the changes that are made to the files in the project. This allows you to save different versions of the work you are doing, which is why we call Git a version control system.
Git is a version control system that lets you track the history of a project and collaborate with other people.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are hidden files and hidden directories?

A

Hidden files and directories are files or directories that store information that we, as users, don’t need to access, such as application configurations and various system settings.

The names of many hidden files and directories start with a dot (.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Git Configurations?

A

Git configurations are settings that allow you to customize how Git works. They consist of variables and their values, and they are stored in a couple of different files.

git config --global --list

List the variables in the global Git configuration file and their values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two types of Git repositories?

A
  1. local repository - a repository that is stored on a computer
  2. remote repository - a repository that is hosted on a hosting device

A hosting service is a company that provides hosting for projects using Git. Github is a hosting service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the hidden directory .git for?

A

A local repository is represented by a hidden directory called .git that exists within a project directory. It contains all the data on the changes that have been made to the files in a project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you initialize a Git repository?

A
git init

Your current directory must be the project directory you want to turn into a repository when you execute this command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the following command:

git init -b <branch_name>
A

Initialize a Git repository and set the name for the initial branch to be branch_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 4 areas to be aware of in Git?

A
  1. Working directory
  2. Staging area
  3. Commit history
  4. Local repository
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Working Directory?

A

The working directory contains the files and directories in the project directory that represent one version of the project.

It is sort of like a workbench. It is where you add, edit, and delete files and directories. The working directory is where you make all the modifications to the contents of a project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the staging area?

A

The staging area is similar to a rough draft space. It is where you can add and remove files, when you are preparing what you want to include in the next saved version of your project (your next commit).
The staging area allows you to choose which updated files (or changes) will be included in your next commit.

The staging area is represented by a file in the .git directory called index.

The index file is created only if you have added at least one file to the stating area in your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the commit history?

A

The commit history is where you can think of your commits existing. It is represented by the objects directory inside the .git directory.

Every time you make a commit, it is saved in the commit history.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a tracked file and an untracked file?

A

An untracked file is a file in the working directory that Git is not version controlling. It has never been added to the staging area and it has never been included in a commit; therefore, it is not part of the repository.

Once you add a file to the staging area and include it in a commit, the file becomes a tracked file. This is a file that is version controlled (in other words, a file that Git tracks).

Every new file in a project version controlled by Git needs to be explicitly added to the staging area and then included in a commit in order to become a tracked file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is committing files/projects important?

A

Committing is important because it allows you to back up your work and avoid losing unsaved work. Once you’ve madea commit, that work is saved, and you’ll be ableto go back and look at that commit to see what your project looked like at that point in time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Making a commit is a two-step process. What is that?

A
  1. Add all the files you want to include in the next commit to the staging area
  2. Make a commit with a commit message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the following command:

git status
A

This tells you the state of the working directory and the staging area. This is useful because in a project w/many files it is easy to lose track of what state the files in your working directory are in (i.e. which ones you have edited) and which files you’ve added to the staging area

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the following command:

git add -A
A

Add all the files in the working directory that have been edited or changed to the staging area.

The “-A” part is an option that tells you to add all files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the general rule when making commits?

A

The general rule is to group related changes together (in the staging area) when you make a commit. This allows you to keep your commits more organized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Does git add command move or copy a file from the working directory to the staging area?

A

It copies the file from the working directory into the staging area.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Explain how commit is both a verb and a noun?

A

In Git, the verb to commit means to save something, and the nount (a commit) means a version of our project. So, to make a commit means to save a version of a project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Explain the following command:

git log
A

To see a list of commits in the commit history, use

git log
.

The

git log
command lists the commits in a local repository in reverse chronological order. It didsplayes 4 pieces of info about each commit:
1. Commit hash
2. Author name and email address
3. Date and time commit was made
4. Commit message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens if your

git log
output goes beyond the size of the command line window?
A

To view the rest of the commits you must press Enter (Return) or use the down arrow.
To exit the command, you must enter

Q
.
24
Q

What are the two main reasons to use branches?

A
  1. To work on the same project in different ways
  2. To help multiple people work on the same project at the same time
25
Q

What is a good way to think about branches?

A

You can think of branches like a line of development. A Git project can have multiple branches (or lines of development). Each of these branches is a stand-a-lone version of the project.

26
Q

What exactly are branches in Git?

A

Branches in Git are movable pointers to commits. When you list the commits in a local repository using the

git log
command, you can see information about which branches point to which commits.
27
Q

What is the branch?

A

In the

git log
output, next to the commit hash inside the parentheses you see
HEAD -> main
.

The branch or branches that appear inside the parentheses next to a particular commit hash in the

git log
output are the branches that point to that commit.

You could say that the ‘main’ branch points to the ‘red’ commit.

Note: HEAD is not a branch

28
Q

What is a common pattern of working with branches?

A

One common pattern for working with branches is to have one official primary line of development - the main or primary branch - and off of that to create secondary branches, called topic branches or feature branches, that are used to work on just a specific part of the project.

These branches are short-lived; they are ultimately combined or incorporated back into the primary branch and then deleted.

29
Q

Tracked files in the working directory can be in one of two states. What are they?

A
  1. Unmodified files
  2. Modified files
30
Q

What are unmodified and modified files?

A

Unmodified files are files in the working directory that have not been edited since the last commit.Once a file in the working directory has been edited (and saved in the text editor), it becomes a modified file.

For Git to know that a file has been edited, the file must have been saved in the text editor. If you made edits to a file but you have not saved those changes in your text editor, then Git will view it as an unmodified file.

31
Q

How does the

git status
command help with modified and unmodified files?
A

This command shows a list of all the modified files and tells you whether or not they have been added to the staging area. It does not, however, list unmodified files.

32
Q

Are commits related to each other?

A

Yes, commits can be related to one another through a parent link. Every commit, other than the very first one in a repository, has a parent commit (some commits can have more than one parent).

It is a parent-child relationship, where the older commit is the “parent” and the newer commit is the “child”.

33
Q

Explain the command:

git cat-file -p <commit_hash>
A

To check which commit is the parent of a given commit, you can use the

git cat-file
command with the
-p
option and pass in a commit hash.

Most Git users won’t use this command often.

34
Q

Explain the command:

git branch
A

List local branches

35
Q

Explain the command:

git branch <new_branch_name>
A

Create a branch

A new branch will initially point to the commit that you werre on when you made the branch.

Note: the branch name can not have any spaces

36
Q

What is HEAD?

A

At any given point in time, you are looking at a particular version of your project. Therefore, you are on a particular branch which is pointing to a commit.

HEAD is simply a pointer that tells you which branch you are on.

The name HEAD is always in capital letters, but this is simply a convention; it is not an acronym.

37
Q

When you type

git branch
, how can you tell which branch you are currently on?
A

There will be an asterisk next to the branch you are currently on.

38
Q

When you type

git log
, how can you tell which branch you are currently on?
A

In the

git log
output, HEAD will point to the branch you are on inside the parentheses.
39
Q

What does it mean to “check out” a branch?

A

To work on another branch (or line of development) in a Git project, you have to switch into that branch. Another way of saying this in Git terminology is that you have to “check out” another branch.

40
Q

What two commands are used to switch to another branch?

A

You must explicitly instruct Git that you want to switch onto a branch. The two commands are:
1.

git switch <branch_name>

2.
git checkout <branch_name>

and pass in the name of the branch that you want to switch onto.

The only purpose of

git switch
command is to switch branches, while the
git checkout
can do more things.
git switch
will be used as this is the specialized command included in the latest versions of Git for this purpose.
41
Q

What 3 things does

git switch
(or
git checkout
) command do when used to switch branches?
A
  1. It changes the HEAD pointer to point to the branch you are switching onto
  2. It populates the staging area with a snapshot of the commit you are switching onto
  3. It copies the contents of the staging area into the working directory

In short, when you change branches you end up changing the commit that you’re looking at, provided that the two branches point to two different commits.
When you switch to a new branch, that is where you will be doing your work now.

42
Q

Finish the sentence:

When you make a commit, it is the branch …

A

you’re currently on that updates to point to the new commit.

43
Q

What are the following terms:
1. source branch
2. target branch

A

Merging in Git is one way you can integrate the changes made in one branch into another branch. In any merge, there is one branch that you are merging, called the source branch, and one branch that you’re merging into, called the target branch.

The source branch is the branch that contains the changes that will be integrated into the target branch. The target branch is the branch that receives the changes and is therefore the one one that is altered in this operation.

44
Q

What are the two types of merges?

A
  1. Fast-forward merges
  2. Three-way merges

The factor that determines which of these types of merges will take place when you merge the source branch into the target branch is whether the development histories of the two branches have diverged. A branch’s development history can be traced by following the parent link of commits.

45
Q

How can you track development history of a branch?

A

The development history of a branch begins with the commit it points to, and extends backward through the chain of commits (the arrows going from child to parent).

46
Q

What is a fast-forward merge?

A

A fast-forward merge is a type of merge that occurs when the development histories of the branches involved in the merge have not diverged - in other words, when it is possible to reach the target branch by following the parent links that make up the commit history of the source branch.

During a fast-forward merge, Git takes the pointer of the target branch and moves it to the commit of the source branch.

47
Q

What is a three-way merge?

A

A type of merge that occurs when the development histories of the branches involved in the merge have diverged.

Development histories have diverged when it is not possible to reach the target branch by following the commit history of the source branch.

In this case when you merge the source branch into the target branch, Git performs a three-way merge, creating a merge commit to tie the two development histories together; it then moves the pointer of the target branch to the merge commit.

48
Q

What is a merge commit?

A

When a fast-forward merge can not be done because there is not way to just move the branch pointer forward in a single-track to combine two development histories, then you need a merge commit.

A merge commit will be created to tie the two development histories (which diverged) together. A merge commit is a commit that has more than one parent.

49
Q

What is a merge conflict?

A

Three-way merges are a more complex type of merge where you may experience merge conflicts. These arise when you merge two branches where different changes have been made to the same parts of the same file(s), or if in one branch a file was deleted that was edited in the other branch.

50
Q

What are the two steps involved in doing a merge?

A
  1. Switch onto the branch that you want to merge into (the target branch).
  2. Use the
    git merge
    command and pass in the name of the branch you’re merging (the source branch).
git merge <branch_name>

Integrate changes from one branch into another branch

51
Q

How does Git project you from losing uncommitted changes?

A

Switching branches changes the contents of your working directory.

But what if you have modified files in your working directory (in other words, files that you have edited) that you have not committed? Will you lose all the work you’ve done in those files if you switch branches? No! Git protects you from losing uncommitted changes.

If Git detects that switching branches will cause you to lose uncommitted changes in your working directory, then it will stop you from switching branches and present you with an error message. However, this happens only if the files that contain uncommitted changes have conflicting changes in the branch you are switching onto.

52
Q

Complete the sentence…

Switching branches changes…

A

files in the working directory

53
Q

What does this command do:

git log --all
A
git log
command shows a list of commits that are reachable by following the parent links from the commit you are on when you execute the command.

To see a list of commits for all the branches in your local repository, use

git log --all

This command shows a list of commits in reverse chronological order for all branches in a local repository

54
Q

What does this command do:

git checkout <commit_hash>
A

Check out a commit.

This allows you to look at other versions (other commits) of your project.

When you run this command, the following things occur:
1. It changes the HEAD pointer to point to the commit you are switching onto.
2. It populates the staging area with all the files and directories that are part of the commit you are switching onto.
3. It copies the contents of the staging area into the working directory

Here, the HEAD pointer will point directly to a commit instead of pointing to a branch. This means that you will be in something that Git (scarily) calls detached HEAD state.

This allows you to look at any commit - or, in other words, any version of your project - in your entire repository.

55
Q

Why is detached HEAD state bad?

A

It is not recommended to make any changes to a repository while in detached HEAD state (that is, while not on a branch).

You will usually want to make commits on branches because branches are easier to remember and refer to than commit hashes.

Therefore, if you checkout a commit, it is common to make and swtich onto a new branch that will point to that commit, so that you are no longer in detached HEAD state.

56
Q

Explain the command:

git switch -c <new_branch_name>
A

Create a new branch and switch into it.

This command lets you createa a branch and switch into it in one go. The

-c
option stands for “create”

Alternatively, the this command also does the exact same thing:
~~~
git checkout -b <new_branch_name>
~~~</new_branch_name>