Learning Git Flashcards
What is a repository?
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.
What is a commit?
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.
What is Git?
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.
What are hidden files and hidden directories?
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 (.)
What are Git Configurations?
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.
What are the two types of Git repositories?
- local repository - a repository that is stored on a computer
- 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.
What is the hidden directory .git for?
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 do you initialize a Git repository?
git init
Your current directory must be the project directory you want to turn into a repository when you execute this command
What is the following command:
git init -b <branch_name>
Initialize a Git repository and set the name for the initial branch to be branch_name
What are the 4 areas to be aware of in Git?
- Working directory
- Staging area
- Commit history
- Local repository
What is the Working Directory?
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.
What is the staging area?
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.
What is the commit history?
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.
What is a tracked file and an untracked file?
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.
Why is committing files/projects important?
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.
Making a commit is a two-step process. What is that?
- Add all the files you want to include in the next commit to the staging area
- Make a commit with a commit message
What is the following command:
git status
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
What is the following command:
git add -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.
What is the general rule when making commits?
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.
Does git add command move or copy a file from the working directory to the staging area?
It copies the file from the working directory into the staging area.
Explain how commit is both a verb and a noun?
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.
Explain the following command:
git log
To see a list of commits in the commit history, use
git log.
The
git logcommand 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
What happens if your
git logoutput goes beyond the size of the command line window?
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.
What are the two main reasons to use branches?
- To work on the same project in different ways
- To help multiple people work on the same project at the same time
What is a good way to think about branches?
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.
What exactly are branches in Git?
Branches in Git are movable pointers to commits. When you list the commits in a local repository using the
git logcommand, you can see information about which branches point to which commits.
What is the branch?
In the
git logoutput, 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 logoutput 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
What is a common pattern of working with branches?
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.
Tracked files in the working directory can be in one of two states. What are they?
- Unmodified files
- Modified files
What are unmodified and modified files?
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.
How does the
git statuscommand help with modified and unmodified files?
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.
Are commits related to each other?
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”.
Explain the command:
git cat-file -p <commit_hash>
To check which commit is the parent of a given commit, you can use the
git cat-filecommand with the
-poption and pass in a commit hash.
Most Git users won’t use this command often.
Explain the command:
git branch
List local branches
Explain the command:
git branch <new_branch_name>
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
What is HEAD?
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.
When you type
git branch, how can you tell which branch you are currently on?
There will be an asterisk next to the branch you are currently on.
When you type
git log, how can you tell which branch you are currently on?
In the
git logoutput, HEAD will point to the branch you are on inside the parentheses.
What does it mean to “check out” a branch?
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.
What two commands are used to switch to another branch?
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 switchcommand is to switch branches, while the
git checkoutcan do more things.
git switchwill be used as this is the specialized command included in the latest versions of Git for this purpose.
What 3 things does
git switch(or
git checkout) command do when used to switch branches?
- It changes the HEAD pointer to point to the branch you are switching onto
- It populates the staging area with a snapshot of the commit you are switching onto
- 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.
Finish the sentence:
When you make a commit, it is the branch …
you’re currently on that updates to point to the new commit.
What are the following terms:
1. source branch
2. target branch
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.
What are the two types of merges?
- Fast-forward merges
- 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.
How can you track development history of a branch?
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).
What is a fast-forward merge?
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.
What is a three-way merge?
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.
What is a merge commit?
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.
What is a merge conflict?
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.
What are the two steps involved in doing a merge?
- Switch onto the branch that you want to merge into (the target branch).
- 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
How does Git project you from losing uncommitted changes?
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.
Complete the sentence…
Switching branches changes…
files in the working directory
What does this command do:
git log --all
git logcommand 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
What does this command do:
git checkout <commit_hash>
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.
Why is detached HEAD state bad?
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.
Explain the command:
git switch -c <new_branch_name>
Create a new branch and switch into it.
This command lets you createa a branch and switch into it in one go. The
-coption stands for “create”
Alternatively, the this command also does the exact same thing:
git checkout -b <new_branch_name>