git Flashcards
What is a git repository?
A git workspace with its own history. It’s like a clade. What you do to one repo does not affect the others.
Command to get info on the repo we’re currently in.
git status
Command to initialize a new empty repo in the current directory.
git init. Use mkdir and cd into it, then use git init inside that directory.
What is a commit?
Basically a snapshot of code in a repo.
Difference between saving a file and committing a file.
You save a file(s) first, then group them together into a commit.
How does a commit work?
You group some or all altered files together and commit them. So if you alter 10 files, you can choose a specific group of 3, for example, to commit.
Difference between working directory & repository.
Working directory - the dir where the files you’ve created reside.
Repo - the .git folder that’s created in the dir with your files when you use the git init command.
What is the staging area?
This is what we add our changes to before making a commit.
Command to add files to the staging area.
git add
Command to create and add two files to the staging area.
touch file1.txt
touch file2.txt
git add file1 file2
Command to add all changes in the current dir to the staging area.
git add .
What is a git message?
A message you write when you commit files which explains what has been changed in those files.
Command to write commit message with a commit.
got commit -m “message”
What info does git status give you?
If there are any modified files that haven’t been added or committed.
Command that gives you a log of the commits for a given repo.
git log
What info does git log give you?
It gives you a log of the commits for a given repo, who made the commits and at what time they were made, and the message associated with each commit. Also a list of all branches & where HEAD is currently pointing.
What does it mean to keep commits atomic?
When you make a commit, try to focus on one thing. If you alter a particular GUI element and also alter some other thing like button functionality, make separate commits with messages for each of these changes, don’t commit both simultaneously.
What command lets you amend a commit?
git commit –amend
(only works on previous commit)
When should you use git commit –amend?
If you do a commit but forget to add one of the files you wanted to commit to the stage beforehand.
How does git commit –amend work?
It opens up the previous commit message and allows you to edit it, so you could basically re-do the previous commit command with the file you forgot.
Command to use if there are files you don’t want to make public or be tracked by git.
git ignore
How do you make git ignore a list of files?
Create a file called .gitignore in the repo and add a list of file types & paths to it that you don’t want git to commit (private files you don’t want public).
What does each commit reference?
The commits that come before and after it (except the first and last commits).
What is the master (or main) branch?
The branch that everything else branches off of.
What is HEAD?
A pointer that points to master initially but when you start a new branch, HEAD points to the most recent commit on that branch.
Command to create a branch.
git branch
What does the git branch command do without a branch name given to it?
Shows you what branch you’re on and where HEAD is pointing.
Command to switch branches.
git switch
Where is a branch created when the git branch command is used?
The location of HEAD is where a branch is made.
How do you create a new branch and switch to it in a single command?
git switch -c
Command to commit all changes at once.
git commit -a -m
What does the checkout command do?
Lets you switch branches but also does a lot of others stuff. More functionality than git switch.
Command to delete a branch.
git branch -d
(-D for force delete)
What is merging and how does it work?
You work on various features and then merge them back to the master branch. You merge entire branches, not commits.
Which branch do you need to be on to merge?
You move to the branch which is receiving the merge. You always merge from the current HEAD branch.
Command to merge branchName to master.
Move to the master branch and type get merge
What is a merge conflict?
When you alter the same line in different branches and try to merge them.
Command to show you differences between your working directory & the staging area.
git diff
Command to show staged & unstaged changes since last commit.
git diff HEAD
Command to show only stage changes.
git diff –staged
Command to show differences between specific files.
git diff file1 file2 …
Command to show differences between branches
git diff branch1 branch2 …
What is stashing?
This basically saves your changes when you want to switch branches without actually committing them.
How to remove the most recently stashed changes & re-apply them to your working copy.
git stash pop
In what order are stashes stored?
FILO
Command to view the stash stack
git stash list
Command to drop a particular stash
git stash drop stash@{index#}
Command to clear entire stash
git stash clear
What is detached HEAD?
HEAD usually point to the most recent commit on a branch. Detached head is when HEAD is not pointing to a terminal node (traveling back in time to make a new branch at that point)
Command to reference commit x-amount before HEAD.
git checkout HEAD~x
Command to discard changes you made to a file.
git checkout HEAD
Command to remove a file from stage.
git restore –staged fileName.txt
Say you made a couple commits on the master branch but you actually meant to make them on a separate branch instead. What is the command to undo those commits?
git reset will reset the repo back to the specific commit that the hash is from.
If you want to reverse some commits that other people already have on their machines, use (revert/reset). If you want to reverse commits that you haven’t shared with others, use (revert/reset) and nobody else will be affected by it.
Revert, reset