GIT Flashcards

Working with Git hub and Cheat Sheets

1
Q

How do you set up global credentials for all repositories?

A

git config –global user.name “Your Name”
git config –global user.email “you@example. com”

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

What do you do if you want to start a new git repository?

A

Navigate to your project directory and run:
git init

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

Downloads a project with the entire history from the remote repository.

A

git clone URL

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

Displays the status of your working directory. Options include new, staged, and modified files.

A

git status

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

Add a file to the staging area. Use. in place of the full file path to add all changed files from the current directory down into the directory tree.

A

git add [file]
or
git add .

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

git commit -m “Message”

A

Create a new commit from changes added to the staging area. The commit must have a message!

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

Add a file to the staging area. Use . in place of the full file path to add all changed files from the current directory down into the directory tree.

A

git add [file]
or
git add .

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

Show changes between working directory and staging area.

A

git diff [file]

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

Shows any changes between the staging area and the repository.

A

git diff –staged [file]

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

Discard changes in working directory. This operation is unrecoverable.

A

git checkout – [file]

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

Revert some paths in the index (or the whole index) to their state in HEAD.

A

git reset {<path>...]</path>

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

Remove file from working directory and staging area.

A

git rm [file]

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

How can you verify that the .git folder was created?

A

ls -a

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

How do you check if a folder is a git repository, if there are untracked files, staged files, or the working directory is clean?

A

git status

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

How do you remove git tracking from a project?

A

rm -rf .git

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

Navigate to the home directory

A

cd ~

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

navigate to the root directory

A

cd /

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

Move one level up in a directory tree

A

cd ..

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

Show the current directory path

A

pwd

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

List all files and directories in the current location

A

ls

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

List all files including hidden files like .git

A

ls -a

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

Clear the terminal screen

A

clear

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

What are untracked files?

A

Files not yet added to git

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

What are files that have changes but are not yet staged?

A

modified files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What do you need to do before committing files?
Stage files
26
How do you stage a file for the next commit
git add or git add . to stage all modified and new files in working directory
27
What saves changes to the repository history?
commit git commit -m "added first test"
28
what is -m
let's you add a commit message
29
How do you check your saved user details?
git config --list
30
What are the steps to pushing to a remote repository?
First commit changes Connect to repository git remote add origin https://github.com/username/repository.git Push your changes git push origin main Pull latest changes git pull origin main
31
How do you connect to a remote repo
git remote add origin URL
32
How do you push changes to a repo?
git push origin main
33
Show commit history
git log
34
List all branches
git branch
35
Create and switch to a new branch
git checkout -b new-branch
36
Merge a branch into the current one
git merge branch-name
37
Undo the last commit (irreversable)
git reset --hard HEAD~1
38
Clone a repository from remote repo
git clone URL
39
What is the commit id?
The SHA hash
40
I want to achieve this output. What do I type commit e6d1f80e3b68df1b68f56ad8b02df6712a9c6392 (HEAD -> main) Author: Your Name Date: Tue Mar 5 14:30:00 2024
git log
41
What are the steps to creating and committing a new file?
Create file touch file.txt stage the file git add file.txt check the status git status Commit the changes commit -m "added new file.txt"
42
How do you switch to an older version of a project
git checkout
43
What is the commit id or SHA?
It is a unique ID assigned to every commit. It allows you to switch to older versions.
44
What is the detached head state?
Can be accessed with git checkout While in detached HEAD state, you can view past commit, but changes won't be tracked unless create a new branch
45
Working directory
The actual files and folders you see in your directory
46
Staging area
Files added with git add, ready for comit
47
Repository
.git folder, the full commit history with tracked changes and branches
48
Branches
Independent lines of development in a Git repo
49
List all branches
git branch
50
Create a new branch
git branch
51
Switch to a branch
git checkout
52
Create a new branch and switch to it shortcut
git checkout -b
53
Confirm branch name
git branch
54
How do you view a compact commit history
git log --oneline
55
What is HEAD
The head is the pointer that always refres to the latest commit in the current branch. - Always points to the **latest commit** of the active branch. - Moves forward automatically with each new commit. - Can be **manually changed** to point to a different commit.
56
Explain this output: Output: ``` e6d1f80 (HEAD -> third-branch) Added new feature a7c3d21 (main) Updated README d2b1234 Initial commit
🔹 Here, **`HEAD -> third-branch`** means that: - `HEAD` is pointing to **`third-branch`**. - The latest commit in `third-branch` is `e6d1f80`.
57
What is a Detached Head?
happens when you manually checkout a specific commit **instead of a branch**. `HEAD` will be detached because it's pointing **directly to a commit**, not a branch. -used for checking out an old commit for debugging -can create commits, but changes will be lost unless a new branch is created
58
How do you enter detached Head state?
git checkout ``` 📌 **Example:** git checkout a7c3d21 📌 **Confirm detached state:** git status Output: HEAD detached at a7c3d21
59
How do you prevent from losing changes in detached head state?
git checkout -b new-branch will create a new branch so can continue working without losing changes
60
Exit detached Head and return to a branch
git checkout main this restores Head to its normal state
61
How do you show the HEAD location?
git log --oneline --decorate
62
T or F: git switch will create a new branch
False. Must use git switch -c new-branch -c will create the new branch.
63
list all tracked files
git ls-files
64
remove files
git rm filename.txt
65
How do you undo unstaged changes
git checkout -- git checkout -- filename.txt or git restore .
66
Reset all unstaged changes in the current branch
git checkout .
67
Remove untracked files, deleting files without actually deleting them
git clean -dn d - removes directories n - no execution (dry-run)
68
Forces deletion without confirmation
git clean -df
69
Undoing staged changes
git reset filename.txt Moves the file out of staging and back to modfied state.
70
What does git reset do?
git reset: moves the branch HEAD backwards to undo commits
71
Undo the last commit but keeps changes staged
git reset --soft HEAD~# (# represents 1,2,3 etc.)
72
Undo last commit and unstage files
git reset HEAD~# (# represents 1,2,3 etc.)
73
Completely remove last commit and file changes
git reset --hard HEAD~# (# represents 1,2,3 etc.)
74
Delete branches
git branch -d branch-name
75
Force delete of a branch
git branch -D branch-name or git branch -D branch1 branch2
76
Steps to making and commiting in detached HEAD state
git checkout git add . git commit -m "Changes made in detached HEAD" git branch branch-name or git switch branch-name git checkout main git merge branch-name
77
What is .gitignore?
- a file that prevents specific files from being tracked by Git. - Useful for **logs, system files, API keys, build files**. - list all of the files to ignore inside the .gitignore
78
Create a .gitignore File
touch .gitignore
79
Common ways to list files in .gitignore
Ignore all log files *.log # Ignore all files in the `node_modules` directory node_modules/ # Ignore all files in `temp/` except `temp/keep.txt` temp/*
80
How do you create a Global .gitignore?
If you want **all repositories** to use the same ignore rules: git config --global core.excludesfile ~/.gitignore_global Then create the file: touch ~/.gitignore_global