Basic Git Workflow Flashcards
Explain the following:
git init
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 can you think of a Git project?
A Git project can be thought of as having three parts:
- A Working Directory: where you’ll be doing all the work: creating, editing, deleting and organizing files
- A Staging Area: where you’ll list changes you make to the working directory
- 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
Explain following:
git status
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
What does the
untracked filesstand for?
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.
Explain the following
git add filename
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
Explain the following:
git diff filename
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
Explain the following
git commit
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
-moption 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
Explain the following
git log
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
Explain the following
What is the purpose of Git’s staging area?
To stage file changes for a commit
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
git log
The command “git status” shows
Untracked files and file changes staged for commit
Explain the following:
git show HEAD
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.
What does ‘git checkout’ do and how do you use it?
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 do you add multiple files to staging area?
git add filename_1 filename_2
What does this command do:
git reset HEAD filename
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.”
Mis short for “modification”
Explain following command:
git reset commit_SHA
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.
Explain “git reset” diagram:
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
What does the command do:
git checkout -- filename
It does the same exact thing that git checkout HEAD
filename does.
Explain the command:
git stash
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:
Expalin a use case of
git stash
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.
Explain the following:
git log --oneline
shows the list of commits in one line format
Explain
git log --oneline --graph - --graph
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.
Explain:
git commit --amend
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
Explain the usefulness of Git alias?
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.