Git and Github Flashcards

Learn Git and Github, Colt Steel Udemy course

1
Q

Who created Git and why

A

Linus Torvalds. He wanted a free VCS (Version Control System)

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

What is Git

A

Git is a Version Control System. A Version Control System tracks changes to project files over time.

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

Explain the 3 states a file can be in

A

Working directory/tree
Staging area
Committed to repo

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

What is the command to check git file statusses and what does it report?

A

git status when on a given branch. It displays the status of each file that is not yet committed, ie staged or in working directory.

It also reports the status of the branch, things like if the head is detached and how many commits the local branch is ahead of origin (remotes)

Added, removed, modified in staging area and working tree/directory

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

What is the command to move a file to the staging area?

A

git add example.txt example2.text
git add . - add all files from working directory.

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

What is the command to commit staged files?

A

git commit -m “some message”
git commit - opens vim or default editor in order to enter message.

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

How can you check the commits on a branch?

A

Go to the branch and run git log

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

How can you see commits on a branch, but compressed to a single line per commit?

A

git log –oneline - currently checked out branch

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

How can you see commits on a branch, but as a graph

A

git log –graph : currently checked out branch

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

How do you create a git repo

A

git init command

OR

create on Github/Gitlab/Bitbucket and clone

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

What is a git repo

A

A project that git should track history of file changes for over time.

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

What is an untracked file

A

A file not yet committed or staged, in working directory

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

What should commit messages look like

A

Git itself suggests present tense. Some people prefer past tense.

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

What does the HEAD refer to

A

By default it refers to the tip of the currently checked out branch. The tip is the latest commit.

If the head is detached it refers to a specific commit on the checked out branch.

When the head is detached, all the files in your local project will be what it was at the point where that commit was done. Inclusive of changes in the commit.

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

Explain commit relationships

A

Each commit has a sha1 hash as an id. The next commit has a reference to the parent’s hash.

In the context of a merge commit, the commit has 2 parent commits

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

What are atomic commits

A

Files that do a single thing grouped together as a single commit, that is, staged and then committed together.

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

What are the benefits of atomic commits

A

If you have to revert for example, then a single thing could be reverted, and not things unrelated to the revert.

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

How do you tell git to ignore files and folders

A

Add a .gitignore file
Files: example.txt
Folders : some-folder/

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

Explain amending commits

A

You can amend the most recent commit by adding the –amend flag to a commit (git commit)

You can stage and unstage files

You can write a new git commit message - either with -m flag or set default editor.

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

How can you see all branches

A

git branch - local branches
git branch -r - remote branches

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

How do you switch to other branch

A

git switch example-branch
git checkout example-branch

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

How to delete git branch

A

Must be on other branch

git branch -d the-branch
git branch -d the-branch –force OR git branch -D the branch if the branch not yet merged into a different branch.

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

Flags shortcuts vs regular

A

For the full flag use –, for example –delete
For shortcut use -, for example -d

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

How can you create a branch and switch to it immediately

A

git switch -c new-branch
git switch –create new-branch
git checkout -b new-branch

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

When a new branch is created, what is it created from?

A

By default it is created from HEAD. That means it’s created from the checked out branch OR the commit that HEAD refers to if it’s detached.

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

How do you rename a branch

A

When on the relevant branch run

git branch -m new-name OR
git branch –move new-name

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

What is a fast-forward merge

A

A merge where the base/parent branch being merged into has not been modified yet, when the feature branch merge is done. Commits are added to this branch, but no merge commit is required.

28
Q

What is a merge-commit

A

This is required if the base branch has been modified when the feature branch is merged in. A merge commit is a combination of the changes on both branches.

The merge commit will have 2 parents, which are, the previous commit on the base branch and the last commit on the feature branch.

29
Q

When not using –oneline, what info is in git log?

A

Author
Date
Hash

30
Q

Explain merge conflicts

A

It happens when a file changed on the 2 branches, and git doesn’t know what to use.

Conflicts are marked with Conflict Markers

This CANNOT be a fast-forward merge, so it will result in a merge-commit.

31
Q

How to interpret git diff

A

One file is marked with + and the other with -

changes are marked with + for the one branch and - for the other

red means removed and green means added

@@ -4,12 - excerpt +1,12 @@
The + and - references file
The first number is start line, and the second is the number of lines.

32
Q

Explain purpose of git stash

A

Two things can happen when switching branches with uncommitted changes (in working tree or staging area). This is that the files are taken with to the new branch if there are no conflicts. Otherwise a warning will be given and switching will be prevented from happening.

In either case, stashing would probably be more preferable. Stashing the changes means saving uncommitted changes temporarily in order to prevent unnecessary commits. It reverts uncommitted changes.

33
Q

How do you stash changes

A

git stash, will add a new stash

Multiple git stash commands will create one after the other.

34
Q

How do you retrieve a stash

A

git stash pop - will pop latest stash off of stashes list and apply it to working directory. Will remove the stash.

git stash apply - will apply latest stashed changes to working directory, but will not delete the stash.

If there are multiple stashes, the command will refer to the last one in unless stash id is provided, then that specific stash will be popped/applied

35
Q

When can merge conflicts occur

A

When popping or applying stashes, if changes have been committed on branch after stash has been created.

When merging one branch into a different branch (merge or pull), where it is NOT a fast-forward merge, ie, the base branch has new commits after feature branch’s creation.

Git revert

git rebase

36
Q

How do I see all stashes

A

git stash list

37
Q

How do I remove all stashes

A

git stash clear

38
Q

How do I delete a specific stash without popping it

A

git stash drop stash-id

39
Q

How do you detach HEAD

A

You use git checkout commit-hash.
You can also checkout relative to HEAD for example: git checkout HEAD~1 (one before HEAD) git checkout HEAD~2 (two commits before HEAD) and so on

You can then view the files at that moment in time, and branch off from that branch at that point in time.

40
Q

How do I re-attach HEAD

A

switch to a branch, checked out one or new one

41
Q

How do you create a new branch

A

git branch some-new-branch
git switch -c (–create) some-new-branch
git checkout -b some-new-branc

42
Q

Explain git reset

A

git reset commit-hash OR HEAD~2 etc : This will reset the branch to the given commit, and add the removed files and changes to the working directory. HEAD will be set to this commit and the following commits will have been removed from the git log for the branch.

git reset –hard will remove all applicable files for good.

git reset with –soft will add the applicable files to the staging area

43
Q

What does git revert do

A

git revert commit-hash / HEAD~2

This will create a new commit that undos this specific commit

44
Q

When to use revert, instead of reset.

A

If you work on a team and other developers might have the commits you are resetting on their machines, reset could be problematic seeing as it removes commits. revert is better, because it just creates a new commit. However, changes can just be pulled, in which case reset should be okay.

45
Q

Explain git restore

A

Remove file from staged:
git restore –staged filename

Restore file to the last committed state:
git restore filename (because it defaults to HEAD)

Restore file to hash etch
git restore –source hash/HEAD~1 filename

46
Q

Explain remote tracking branch

A

The remote branch that is linked to this local branch. After pulling from remote or pushing to it, the tip should be the same for local and remote. If new work is done locally, then new commits are added and the remote tracking branch commit stays where it is.

Therefor local can be ahead of remote.

47
Q

Explain fetch

A

get fetch fetches changes from remote repo, but does not integrate them into working directory or local branches.

It fetches new commits and branches, but does not add them into the local branches.

48
Q

Explain git pull

A

git pull = git fetch (fetch all remote branches and their commits) + git merge (merge those commits into currently checked out local branch)

example - git pull origin whatever - commits from origin/whatever are merged into the currently checked out local branch

git pull only will pull changes for the checked out branch from it’s corresponding remote branch.

git pull creates a merge commit, if not fast-forward merge

git pull –rebase, does a git fetch and then instead of merge, it does a rebase.

49
Q

What are github pages

A

A service where you can create a static website for your repo.

Create a branch, with the source code and under settings set the github pages setting

50
Q

Explain Gitflow workflow

A

The work flow is as follows:
main branch, contains working copy of repo

develop branch is what feature branches are created from and merged back into via PRs

release branches are created from develop branch and any release-related work is done on it.

When the release branch is ready, merge it back into main and into develop.

Tag main with the release version and delete the version branch.

If hotfixes are required, create a hotfix branch from main, and do the fix.

Merge it back into develop and into main.

51
Q

How do you resolve Pull Request conflicts?

A

Go to branch PR was created for.

Pull from the branch PR is meant to merge into.

Resolve conflicts locally

Commit and push

52
Q

Branch protection rules

A

Under github repo settings you can set settings for your branches, for example, a branch can only be merged into via Pull Request

53
Q

What can Github actions be used for

A

To run automated tests for example, when PRs are created to merge into given branch

This is a CI (continuous integration) workflow

54
Q

What is trunk-based git workflow

A

All feature branches are PRd into main. Pull requests run automated tests, for continuous integration purposes. Use Github actions for this. Tag main branch when release is done.

55
Q

What is a remote

A

A remote refers to a connection to a repo hosted on Github/Gitlab/BitBucket etc.

The default is “origin”.

If you want to collaborate on a project repo, you can fork it, and create a new remote called “upstream” (by convention) in order to pull from the project repo.

56
Q

Explain forking and forking workflow

A

When you want to help work on a repo such as React, you can fork their repo (which makes a clone of it on your own Github profile). This is now your own repo with a copy of the original repo’s history, files etc.

You can then create a new remote called “upstream” by convention in order to pull in new commits from the original repo.

You can only merge into the original repo via PR.

57
Q

How do you create a new remote

A

git remote add name git-repo-url

58
Q

How do you merge one branch into another

A

1) run git merge branch-name when checked out the one you want to merge into

2) rebase the checked out branch with a different branch

3) pull from remote branch (regular or rebase)

59
Q

What is the point of rebasing a branch?

A

Rebasing is an alternative to merging and cleans up history preventing unnecesarry merge commits

It basically tells git to use the branch after ‘rebase’ as the new base for your feature branch and it recreates all your current commits on your feature branch and adds it after the tip of the new base.

This changes the history on the feature branch only by adding the base and then creating new commits after the tip that are identical to the commits there were on the branch.

60
Q

How to rebase

A

git switch feature-branch-name
git rebase other-branch

OR when pulling from remote

git pull origin whatever --rebase

61
Q

Handling rebase conflicts

A

After resolving conflicts, instead of using git commit, rather use git rebase –continue after staging the resolved file with git add.

62
Q

When not to use rebase

A

Better not to if collaborators already have the branch with it’s commits on their machines, seeing as history is re-written OR when already stored remotely

However, if they pull with –rebase, it should be fine seeing as if they added commits it will also be added to the tip of this branch.

63
Q

Explain cherry-picking

A

Allows you to pull in only the changes of a specific commit(s) into your checked out branch by creating new commit that contains these changes.

64
Q

What is the point of interactive rebasing?

A

It allows you to do the following to the checked out branch , which basically changes the history:
- change messages
- combine multiple commits
- delete a commit

Any commits that are newer than the edited commit(s) are recreated, and therefor have new commit hash values.

65
Q

How to do an interactive rebase

A

git rebase -i (–interactive) HEAD~9 will check out the last 9 commits in order to edit them. It opens VIM or your default editor. Follow the instructions in the text you are presented.

Starts at HEAD~1 for some reason.

66
Q

What does rebase onto mean

A

The new base (after rebase command) is rebased onto the checked out branch.

67
Q

What is git diff used for

A

To see changes in files, branches, commits. Use relevant flags and file names in order to filter down the output.