Basic Git Commands Flashcards

1
Q

Create a new git repository

A

git init

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

Add a file to the git repo staging area

A

git add <filename></filename>

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

Commit the changes from the staging area to the git repo

A

git commit -m “Commit Message”

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

Show the current status of staged and committed files for the git repo

A

git status

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

Configure a global default username for git

A

git config –add –global user.name “SuperCoolGuy”

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

Configure a global default email address for git

A

git config –add –global user.email “coolguy@gmail.com”

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

Configure a local default username for git

A

git config –add user.name “SuperCoolGuy”

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

View git settings (user, email, etc)

A

git config –list –local

git config –list –global

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

View a single key from the git configuration settings

A

git config –get <key></key>

example: git config –get user.email

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

Edit the git config file directly

A

nano .git/config

(from the root of the project folder)

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

Remove a key from the git configuration

A

git config –unset <key></key>

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

Remove all instances of keys with a given value

A

git config –unset-all <key></key>

example: git config –unset-all user.name
(assuming user.name has been set more than once)

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

Remove an entire section of keys from the config

A

git config –remove-section <section name>

example: git config –remove-section user

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

Where are the git config files stored for the system, global, local, and worktree levels?

A

System: /etc/gitconfig
Global: ~/.gitconfig (hidden file)
Local: .git/config (in the project folder)
Worktree: .git/config.worktree (in the project folder)

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

View the past commits and hashes

A

git log

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

Show the git log, but only with the last commit and turn off paging

A

git –no-pager log -n 1

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

Show the git log with full reference IDs and branches

A

git log –decorate=full

18
Q

Show a compact view of the git log (one line for each commit)

A

git log –oneline

19
Q

Using plumbing commands, view the contents of a blob file.

A

1.) git log (to see the commit hashes)
2.) git cat-file -p <hash-of-commit> (to see the tree hash)
3.) git cat-file -p <hash-of-tree> (to get blob hash)
4.) git cat-file -p <hash-of-blob> (to view blob file contents)</hash-of-blob></hash-of-tree></hash-of-commit>

20
Q

Check the active branch of the git project

A

git branch

21
Q

Change the name of a branch from “main” to “default”

A

git branch -m main default

22
Q

Change the global default branch from “master” to “main”

A

git config –global init.defaultbranch main

23
Q

Remove the branch named “main”

A

git branch -d main

OR

git branch -D main (force delete)

24
Q

Create a new branch named “feature_one”

A

git branch feature_one

25
Create a branch called feature_two and switch to it
git switch -c feature_two
26
Switch to the main branch (old way or new way)
git switch main (new) OR git checkout main (legacy)
27
Show the git log including a graph of the branch history
git log --graph --all
28
Merge a branch called 'feature_two' into 'main'. (Two commands)
1.) git switch main 2.) git merge feature_two
29
Rebase a git branch named "feature_three" to "main" (2 commands)
1.) git switch feature_three 2.) git rebase main
30
Reset the last commit, changes in the index, or the worktree - but keep the changes.
git reset --soft
31
Reset the last commit, changes in the index, or the worktree; removing all staged files and deleting anything from subsequent commits
git reset --hard NOTE: this command should be used with caution
32
Add a remote branch to a git project
git remote add Example: git remote add origin https://github.com/userguy/project1 NOTE: URI could also be a local file path to another git repo on your local machine
33
Download any remote git repos metadata that have been added.
git fetch
34
Show git log for the remote repo for a branch called feature_two
git log origin/feature_two
35
Upload your local git repo to the remote git repo
git push Example: **git push origin main**
36
Configure git to use rebase automatically in place of merge
git config --global pull.rebase true
37
In a .gitignore file, ignore the venv folder
nano .gitignore venv/*
38
In a .gitignore file, ignore all .txt files
nano .gitignore *.txt
39
Exclude a specific file from being ignored
nano .gitignore !important.txt
40
Add a comment to a .gitignore file
nano .gitignore #Some Comment Here
41
Initialize a new local git repository by pulling down a remote github repo.
git clone https://github.com/user-name/repo-name