Basic Git Workflow Flashcards

1
Q

Explain the following:

git init
A

The word init means initialize. The command sets up all the tools Git needs to begin tracking changes made to the project

creates a new Git repository

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

How can you think of a Git project?

A

A Git project can be thought of as having three parts:

  1. A Working Directory: where you’ll be doing all the work: creating, editing, deleting and organizing files
  2. A Staging Area: where you’ll list changes you make to the working directory
  3. A Repository: where Git permanently stores those changes as different versions of the project

The Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a Git repository. In Git, we save changes with a commit

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

Explain following:

git status
A

When you do work, you will be changing contents of the working directory. You can check the status of these changes with

git status

inspects the contents of the working directory and staging area

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

What does the

untracked files
stand for?
A

n the output, notice the file in red under

untracked files
. Untracked means that Git sees the file but has not started tracking changes yet.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the following

git add filename
A

In order to Git to start tracking a file, the file needs to be added to the staging area.

The file shows up as green colored when it is added to staging area.

adds files from the working directory to the staging area

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

Explain the following:

git diff filename
A

When you make changes in a file after you added it, since the file is tracked, we can check the differences between the working directory and the staging area with

git diff filename

shows the difference between the working directory and the staging area

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

Explain the following

git commit
A

A commit is the last step in our Git workflow. A commit permanently stores changes from the staging area inside the repository.

You need the

-m
option followed by a message, as so:
git commit -m "Complete first line of dialogue"

Standard convention for commit messages:
1. Must be in quotation marks
2. written in present tense
3. Should be brief (50 characters or less) when using

-m

permanently stores file changes from the staging area in the repository

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

Explain the following

git log
A

Often with Git, you’ll need to refer back to an earlier version of a project. Commits are stored chronologically in the repository and can be viewed with:

git log

shows a list of all previous commits

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

Explain the following

What is the purpose of Git’s staging area?

A

To stage file changes for a commit

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

The output below is typical of which command?

commit bda95786432d142bbff996ad32045fa4f32ec619
Author: codecademy <ccuser@codecademy.com>
Date: on Nov 16 13:13:33 2015 -0500
First commit
A
git log
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The command “git status” shows

A

Untracked files and file changes staged for commit

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

Explain the following:

git show HEAD
A

In Git, the commit you are currently on is known as the HEAD commit. In many cases, the most recently made commit is the HEAD commit.

The output of this command will display everything the git log command displays for the HEAD commit, plus all the file changes that were committed.

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

What does ‘git checkout’ do and how do you use it?

A
git checkout HEAD filename

will restore the file in your working directory to look exactly as it did when you last made a commit.

Here, filename again is the actual name of the file. If the file is named changes.txt, the command would be

git checkout HEAD changes.txt
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you add multiple files to staging area?

A
git add filename_1 filename_2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does this command do:

git reset HEAD filename
A

This command lets you unstage that file from the staging area. This command resets the file in the staging area to be the same as the HEAD commit. It does not discard file changes from the working directory, it just removes them from the staging area.

If you staged a file, but then you realize you do not want to commit that file to repository, then this command restores that file as it previously was in the HEAD commit.

Notice in the output, “Unstaged changes after reset.”

M
is short for “modification”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain following command:

git reset commit_SHA
A

You can “rewind” your changes to a past previous commit of your choosing.

This command works by using the first 7 characters of the SHA of a previous commit. For example, if the SHA of the previous commit is 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:

git reset 5d69206

HEAD is now set to that previous commit.

17
Q

Explain “git reset” diagram:

A

To better understand

git reset commit_SHA
, each circle in the diagram represents a commit.

Before reset:
* HEAD is at the most recent commit

After resetting:
* HEAD goes to a previously made commit of your choice
* The gray commits are no longer part of your project
* You have in essence rewound the project’s history

18
Q

What does the command do:

git checkout -- filename
A

It does the same exact thing that git checkout HEAD filename does.

19
Q

Explain the command:

git stash
A

Let’s say you’re working on experimental code on a fresh branch and realize that you forgot to add something to a previous commit in order to continue your work. In order to go to a different branch, one must always be at a clean commit point. In this case you don’t want to commit your experimental code since it’s not ready but you also don’t want to lose all the code you’ve been working on.

A good way to handle this is by using git stash, which allows you to get back to a clean commit point with a synchronized working tree, and avoid losing your local changes in the process. You’re “stashing” your local work temporarily in order to update a previous commit and later on retrieve your work.

The flow when using git stash might look something like this:

20
Q

Expalin a use case of
~~~
git stash
~~~

A

While working on a file, you find a small bug in a separate file from a previous commit that needs to be fixed before you continue.

$ git stash

Running the command above will store your work temporarily for later use in a hidden directory.

At this point, you can switch branches and do work elsewhere.

Once the bug is fixed, you want to retrieve the code you were working on previously, you can “pop” the work that was stored when you used git stash.

$ git stash pop

From here, you can continue your work and commit it when ready.

21
Q

Explain the following:

git log --oneline
A

shows the list of commits in one line format

22
Q

Explain

git log --oneline --graph - --graph
A

Displays a visual representation of how the branches and commits were created in order to help you make sense of your repository history. When used alone, the description can be very lengthy, so you can combine the command with –oneline in order to shorten the description.

23
Q

Explain:

git commit --amend
A

Git’s –amend flag is extremely useful when updating a commit, it allows you to correct mistakes and edit commits easily instead of creating a completely new one.

Let’s say you finish working on a lengthy feature and everything seems to be working fine so you commit your work. Shortly after, you realize you missed a few semicolons in one of your functions. You could technically create a new commit, but ideally, you want to keep all commits specific, clean, and succinct. To avoid creating a new one, you could create your changes, stage them with git add and then type the command git commit –amend to update your previous commit.

It’s important to note that although it seems like –amend is simply updating the commit, what Git actually does is replace the whole previous commit. For this reason, when you execute the command git commit –amend, your terminal editor asks you to update your commit message:

However, if you want to keep the same commit message, you can simply add the flag –no-edit:

git commit --amend --no-edit
24
Q

Explain the usefulness of Git alias?

A

When grouping commands together, you can end up writing very long lines of Git commands in the terminal such as:
~~~
$ git log –pretty=format:”%h %s” –graph
~~~

Fortunately, Git offers a helpful feature that can make your Git experience simpler, easier, and more familiar: aliases.

If you have a set of commands that you use regularly and want to save some time from typing them, you can easily set up an alias for each command using Git config.

Below are a couple of examples:
~~~
$ git config –global alias.co “checkout”
$ git config –global alias.br “branch”
$ git config –global alias.glop “log –pretty=format:”%h %s” –graph”
~~~

Once the aliases are configured, next time you want to check out to another branch you could type the command:
~~~
$ git co example_branch
~~~

Instead of:
~~~
$ git checkout example_branch
~~~

Using Git aliases can create a much more fluid and efficient workflow experience when using Git. By getting creative with your aliases, you’re able to wrap a sequence of Git commands into one in order to save time and effort.