GIT Flashcards

1
Q

What are the three states of GIT?

A

Working directory
Staging area
Commit - Repository (.git file) - history of changes

—–You could consider a fourth state
Remote repository

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

How do you configure you user name?

A

git config –global user.name “user name”

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

How do you configure your email address?

A

git config –global user.email “myemail@foobar.com”

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

How do you get the settings for your git user?

A

git config –global –list

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

What is the default branch?

A

master

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

How do you clone a repository?

A

git clone [repository link]

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

What is an untracked file?

A

Simply a file that hasn’t been added to a repository as yet.

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

How do you add an untracked file to the staging area?

A

git add [pathtofile]

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

How do you see what the current status of your repository is?

A

git status

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

If you commit a file using commit -m, what will the state of the working directory and the staging area be?

A

The working directory will be clean, and the staging area will have nothing to commit. By using git commit - you have committed the files to the repository. The third git state.

NOTE: Until you push the files to the remote repository - they are still local to your machine.

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

The remote name of the repository usually defaults to ‘origin’ - when is this created?

A

When you clone the repository.

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

How do you push a file to the remote repository (assuming you are using the default remote repository name, and master branch)?

A

git push origin master

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

How do you create a new GIT project locally (no source)?

A

git init [name of project]

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

When you do a commit - if you see the status:
[master (root-commit)]
What does this mean?

A

It’s the very first commit.

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

How do you create a new git repository based on existing source?

A

git init

From within the folder containing the source

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

How do you add all the files within a directory so that git is tracking them, in one command?

A

git add .

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

How do you stop a folder being tracked by git?

A

rm -rf .git

The .git file is the one inside the folder

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

The unique identifier (used for commits) is what kind of hash?

A

sha1

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

If you fork a repository - your git repository (on github) is upstream or downstream?

A

The fork is downstream - the original is upstream.

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

Is commit a local or a remote command?

A

local

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

Before doing a push - what should we do as a best practice?

A

git pull origin master

Make sure our local repository is up to date with the head.

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

When you do a push - you may get a response back that looks like this:

4beb7f0..cec9347 master - master

What does it mean?

A

The 4beb7f0..cec9347 are sha1 hashes that refer to the commit id’s. The left hand number is the local - the right hand number is the remote.

master - master, refers to the branch names, local master to remote master.

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

How do you add and commit a file at the same time?

A

git commit -am “Adding more text”

NOTE: This can only work with files that are being tracked.

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

What constitutes a tracked file?

A

Pretty much any state - with the exception of new files. So in staging, in the repository, editing existing file - are all tracked files.

New files have yet to be tracked, unless you use git add - filename.

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

How do you get a list of all files that are being tracked?

A

git ls-files

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

If you use git add filename - and then you edit that file name again and call git status - what will you see?

A

The same file will be listed twice, once as a new set of changes, and again as an existing file ready to commit. Despite the fact it’s one file - it will apear like two. By calling git add on the file, you are basically merging both changes into the staging area.

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

How do you add files recursively?

A

Adding files using “git add . “ will recursively add files (up through a folder structure).

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

Can you issue commands from anywhere within your git repository?

A

Yes - for most commands that’s fine.

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

How do you unstage a file?

A

So after using git add filename - you’ve decided it’s not ready for a commit, you want to do more work. You can back out of the staging area by calling

git reset HEAD filename

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

Does unstaging a file remove the changes?

A

No - it simply removes the file from the staging area, and returns it to the working directory.

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

How do you revert any changes made to a file in your working directory (i.e. trash the changes)?

A

git checkout – filename

NOTE: This discards and loses all changes

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

How do you rename a file?

A

You can rename using the git mv comand

git mv oldname newname

Then you simply commit the change. NOTE: Until you commit, you can easily backout the rename, like you would normally do (git reset HEAD filename)

OR

You can mv on the command line. BUT - git will see htis as a deletion and an addition. To fix this, you use

git add -A

This will update any files that have been renamed, moved or deleted on the filesystem. Once you have done this - git status will report it as a renamed file, and no longer as a deleted and added file.

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

Why should you rename your files before you make any changes?

A

Makes it easier for git to track the changes.

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

How do you back out of a staged file rename?

A

it may be tempting to follow the instructions and do a git reset HEAD filename… but that will probably break in weird ays.

Simply use git mv filename filename again to revert to the old filename.

Git will detect there has been no net changes and git status will show no changes to be staged.

35
Q

What happens if we rename the file through finder?

A

Like doing it through bash - it will look like a deletion and an addition. But you cannot simply use git add -A to fix this. Because the filesystem will add a DS_Store file as well.

You need to:

git add filename
git add -u

To update the index.

36
Q

What happens if you try to delete a file using git rm filename when the file isn’t tracked yet?

A

Git will complain - you need to use bash.

37
Q

What happens if you rm a file that wasn’t being tracked?

A

Nothing - it just gets deleted.

38
Q

What command do you use to delete a file?

A

git rm filename

This will automatically remove the file from the workspace and also stage the deletion.

39
Q

How do you unstage a deleted file (i..e revert that change)?

A

Two steps - first:
git reset HEAD filename
then
git checkout – filename

After the reset head - the file will not be in the directory still. It will appear as though it didn’t work. You need to checkout the file again to clean everything up.

40
Q

What do you do if you delete a tracked file from your working directory using bash?

A

git status will show the file deleted, but unstaged. Simply run:

git add -A

And it will detect the deletion, and stage the change for you.

41
Q

If you use rm -rf on a tree structure, how do you get git to synch with that change?

A

You can simply issue a

git add -A

command to get it up todate, and stage the change. Then you can simply commit the change.

42
Q

How do you show help for a command (say log)?

A

git help log

43
Q

When you use:

git log

It gives you a full SHA1 identifier - how can you make that shorter?

A

git log –abbrev-commit

This gives you an id that is probably the same as the one used in the GIT web client. And is all you really need to ID a commit.

44
Q

How can you make a git log that shows everything on one line - with a graph (ascii graph) and any additional info that may make things clearer?

A

git log –oneline –graph –decorate

45
Q

How can you specify a range of commits to show in a log?

A

git log d65d556…f5aa5b9

NOTE the … three dots. The SHA1 identifiers are just the short versions.

46
Q

How could you get git commits for the last couple of days?

A

git log –since=”3 days ago”

47
Q

How do you get the history of a specific file?

A

git log – test.txt

48
Q

How do you get a history of changes to a file that has been renamed?

A

git log –follow – level1/level1-file.txt

49
Q

How do you show information about a specific commit?

A

git show 0df2bed40106d95849a830f148c68901ef67f2cc

This will show all the info about a specific commit (based on the SHA1 id).

it shows the id, date, the author, the date and the commit message plus the diff information.

50
Q

How do you make an alias to a long or complicated command?

A

git config –global alias.hist “log –all –graph –oneline –decorate”

NOTE the –global means it’s available over all repositories.

The alias.hist bit means that the alias is called ‘hist’.

51
Q

Where do aliases get written to?

A

~/.gitconfig

52
Q

How do you create a .gitignore file?

A

Simply create it in the root of the repository. Format is just one entry per line. And can be any simple match to a file name

*.log
MyFile.txt
MyFolder/

etc.

53
Q

Do you add .gitignore files to the version control?

A

yes.

54
Q

How do you create a global .gitignore?

A

In your root directory, create the following file

~/.gitignore_global

55
Q

How do you do a basic diff, for a file that is in working directory and with a non-commited change in staging?

A

git diff

56
Q

How do we do a diff, using the visual tool

A

Assuming we have setup the aliases correctly,

git difftool

57
Q

How do you compare the working directory with the last commited files?

A

git diff HEAD

git difftool HEAD

58
Q

How do you compare whats last been staged, and the last commit?

A

git diff –staged HEAD

59
Q

What are the three main ways to do diffs? (i.e. wd vs staging etc…)

A

wd vs staging: git diff
wd vs last commit: git diff HEAD
staging vs last commit: git diff –staged HEAD

60
Q

If there are multiple files - git diff will catch them all, how do you specify a single file?

A

git diff – README.md

this works for all scenarios

61
Q

How would you compare two commits - using diff? Say an arbitrary ID and the HEAD?

A

git diff 48997c6 HEAD

62
Q

How would you (using abbreviated syntax) compare the HEAD with HEAD - 1?

A

git diff HEAD HEAD^

63
Q

If you are doing a diff, and one side of the file is referenced by — /dev/null, what does that mean?

A

The entire file was new at that point.

64
Q

How do you cycle through the files in p4Merge?

A

command - q

in p4merge

65
Q

How do you show the differences between the local master branch, and remote master branch?

A

git diff master origin/master

NOTE: This works exactly the same for branches too. Left hand side is local, right hand side is remote.

66
Q

How do you list all of the branches (including remote branches)?

A

git branch -a

67
Q

How do you create a new branch?

A

git branch newbranchname

68
Q

How do you switch to the new branch?

A

git checkout newbranchname

69
Q

When you create a new branch, and you do a log:

git log –oneline –decorate

The output indicates that the HEAD and ORIGIN/HEAD and the new branch all point to the same place, why is that?

A

Because, until you actually commit something in the new branch - they do point to the same place.

Branches are just labels.

70
Q

How do you rename a branch?

A

git branch -m testbranch newbranch

-m for move

71
Q

How do you delete a branch?

A

git branch -d mybranchname

72
Q

What happens if you try to delete a branch that you are currently working on?

A

You can’t - you have to be in a different branch.

73
Q

How can you checkout and create a branch at the same time?

A

git checkout -b newbranchname

74
Q

How do I compare the differences between a change in my branch, and the master branch?

A

git difftool master title-change

75
Q

When I am ready to merge my branch - what branch do I need to be in, the master, or the branch I am merging from?

A

The master branch

76
Q

How do you merge a branch?

A

git merge mybranch

This is done from the master branch

77
Q

What is a fast-forward commit?

A

It’s when the branch is merged directly into the main branch - and there were no issues, because the main branch was only up to the point that the new branch was branched.

This can only really occur, if the master branch doesn’t have any file additions between when the branch is created and merged.
—–—–—–
/
—–*——————–

becomes:

—–-—–—–—–*

78
Q

Once you have merged a feature (from a branch) - do you need the branch anymore?

A

No - you can delete it.

79
Q

How do you merge a branch, while preserving the graph line (i.e. not fast forwarding)?

A

git merge add-copyright –no-ff

   *-----*-----*-----*
   / -----*--------------------

becomes:

   *-----*-----*-----*
   /                     \ -----*--------------------*-----
80
Q

If you are not fast forwarding a merge - can you still delete the branch afterwards?

A

Yes

81
Q

If you try to merge two branches that are in conflict, what happens?

A

If git can’t automerge, the status of the branch becomes ‘merging’ - it will report unmerged paths. And you have to fix this before you can proceed.

82
Q

How do you run a graphical merge?

A

git mergetool

(assuming that’s setup) - otherwise you have to manually fix the files.

83
Q

What is a .orig file?

A

When you have a merge conflict, a .orig file is saved, so that you have a chance to back out if there are any issues arising from the merge. You can setup your .gitignore file to safely ignore these files so they aren’t added to your repository.

84
Q

How do you delete any unstaged, untracked files?

A

git clean -f