Git Flashcards
To restore deleted and staged files
git reset HEAD followed by
git checkout HEAD
to see the remotes associated with your local repository.
git remote -v
git diff compares your working tree
to the staging area
git diff –staged to compare to staging area
git log -p
to see every detail of your commits, such as the actual changes, or diff, of each commit.
The working tree is
the collection of project files that you work with directly.
Git thinks about the files in your working tree as being in three distinct states:
unmodified, modified and staged.
/.html matches
all files with an .html extension, but only in subdirectories of your project.
Graphical views of your repository
git log –graph
git log –oneline –graph
o tell Git to show you the complete history of everything it knows about
git log –oneline –graph –all
Searching Git history commands
git log --author=crispy8888 --oneline git log --grep=ideas --oneline git log --oneline books/book_ideas.md git log --oneline --stat books books - directory To see changes in directory git log -S"Fortran"
Find all of the commits in your code that deal with the term “Fortran”
git log -S”Fortran”
git log -S”Fortran” -p (with details)
to see all local branches.
git branch
brings the commits from the remote repository and merges them with your local commits.
git pull
pulls all of the commits down from the remote repository to your local
one.
git fetch
You can’t push to a remote that has
any commits that you don’t have locally, and that Git can’t fast-forward merge.
to add a remote to your local repository.
git remote add origin
to push the local commits in your repository to your remote, and to start tracking your local branch against the remote branch.
git push–set-upstream origin master or git push -u origin master
How Git reconstruct a branch, based on a single commit hash step by step
You switch to a named branch, which is a label that references a commit hash.
Git finds that commit object by its hash, then it gets the tree hash from the commit object.
Git then recurses down the tree object, uncompressing file objects as it goes.
Your working directory now represents the state of that branch as it is stored in the repo.
Gits the most common objects
Commits: Structures that hold metadata about your commit, as well as the pointers to the parent commit and the files underneath.
Trees: Tree structures of all the files contained in a commit.
Blobs: Compressed collections of files in the tree.
git rev-parse
will translate a short hash into a long hash
git cat-file
will show you the pertinent metadata about an object.
git status -sb
gives a concise view of the state of your working tree.
starts an interactive rebase operation.
git rebase -i
You can move lines around in the rebase script to
reorder commits.
What cloning repository automatically do?
Cloning a repository automatically creates a hidden .git directory, which tracks the activity on your local repository.
What Forking do?
Forking creates a remote copy of a repository under your personal user space.
Git thinks about the files in your working tree as being in three distinct states
Unmodified •
Modified•
Staged
With git add .
to put everything from your working tree into the staging area
How Git views your working tree?
At its core, Git really only knows about files, and nothing about directories. Git thinks about files as string that point to entities Git can track.
How to commit an empty directory?
placeholder file. The usual convention is to create a hidden, zero-byte .keep file inside the directory you want Git to “see.”
A commit is essentially
a snapshot of the particular state of the set of files in the repository at a point in time
git status
shows you the current state of your working tree.
git add
lets you add changes from your working tree to the staging area.
git add .
adds all changes in the current directory and its subdirectories.
git add /*
lets you add all changes in a specified directory.
git diff
shows you the difference between your working tree and the staging area.
git diff –staged
shows you the difference between your staging area and the last commit to the repository.
git commit
commits all changes in the staging area and opens Vim so you can add a commit message.
git commit -m “”
commits your staged changes and includes a message without having to go through Vim.
git log
shows you the basic commit history of your repository.
git log -p
shows the commit history of your repository with the corresponding
diffs.
The general philosophy is that a commit should be
a logical collection of changes that make sense as a unit — not just “the latest collection of things I updated that may or may not be related.”
what is HEAD?
HEAD is simply a label that references the most recent commit
git reset HEAD
use HEAD as a reference point, restore the staging area to that point
staging area
The staging area lets you contruct your next commit in a logical, structure fashion.
git reset HEAD
lets you restore your staging environment to the last commit state.
How I should move/delete files in a repository?
With git commands
git rm
git mv
git mv
moves files around and stages the change, all in one action.
git rm
removes files from your repository and stages the change, again, in one
action.
global .gitignore
You can find where your global .gitignore lives with the command git config – global core.excludesfile ~/.gitignore_global.
.gitignore lets you
configure Git so that it ignores specific files or files that match a certain pattern.
*.html in your .gitignore matches
on all files with an .html extension, in any directory or subdirectory of your project.
! in .gitingnore
negates a matching rule.
You can have multiple .gitignore files inside various directories of your project to
override higher-level matches in your project.
Limiting results git log
git log -3
Git log each commit in one line
git log –oneline
git shortlog
This is a nice way to get a summary of the commits, perhaps for including in the release notes of your app.
A commit in Git includes information about
the state of the files in your repository, along with metadata such as the commit time, the commit creator, and the commit’s parent or parents.
A branch in Git is simply
a reference to a particular commit by way of its hash.
to create a branch
git branch
to switch to a local branch, or to checkout and track a remote branch.
git checkout
to delete a local branch.
git branch -d
to see all local and remote branches.
git branch –all
to create and switch to a local branch in one fell swoop.
git checkout -b
git pull is actually two commands in disguise:
git fetch & git merge
takes your local commits and synchronizes the remote repository with
those commits.
git push
merges the commits from the remote into your local repository.
git merge
Can you have multiple remotes?
Yes
You can pull commits from multiple remotes into your local repository and merge them as you would commits from any other branch or remote.
to set up a Git repository.
git init
to create the first commit on your new repository.
Use git add followed by git commit
create mode
in git
is simply Git telling you what file permissions it’s setting on the files added to the repository.
What commit object stores?
A commit object stores the metadata about a commit, such as the parent, the author, timestamps and references to the file tree of this commit
A tree object is
a collection of references to either child trees or blob objects.
Blob objects are
compressed collections of files; usually, the set of files in a particular directory inside the tree.
git config merge.conflictstyle diff3
provides a three-way view of the conflict, with the common ancestor, “their” change, and “our” change.
Rebasing is a great technique over merging when you want
to keep the repository history linear and as free from merge commits as possible.
To rebase your current branch on top of another one, execute
git rebase .
To resume a rebase operation after resolving conflicts and staging your changes, execute
git rebase –continue.
To skip rebasing a commit on top of the current branch, execute
git rebase –skip.
Should I do rebasing locally or on remote?
Locally
Interactive rebases in Git let you
create a “script” to tell Git how to perform the rebase operation
The pick command means
to keep a commit in the rebase.
The squash command means
to merge this commit with the previous one in the rebase.
The reword command lets you
reword a particular commit message.
Rebasing creates
new commits for each original commit in the rebase script.
Squashing lets you
combine multiple commits into a single commit with a new commit message. This helps keep your commit history clean.
Gitignore After the Fact
Mastering git book