Git Flashcards

1
Q

how to remove a file from git staging area?

A

To remove a file from the Git staging area (i.e., to unstage a file), you can use:

git reset HEAD <filename>

Or in newer versions of Git (2.23.0+), you can use the more intuitive:

git restore --staged <filename>

Both commands will remove the file from the staging area while keeping your changes in the working directory. If you want to remove multiple files, you can list them all or use patterns:

git restore --staged file1.txt file2.txt
# or
git restore --staged *.txt
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What command shows all remote branches in a Git repository?

A

git branch -r

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

How do you create and switch to a new branch in one command?

A

git checkout -b branch-name

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

What command shows both local and remote branches?

A

git branch -a

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

How do you push a new branch to remote and set up tracking?

A

git push -u origin branch-name

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

What command fetches the latest information from remote without merging?

A

git fetch

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

How do you see detailed information about remote branches including latest commits?

A

git remote show origin

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

What prefix typically indicates a remote branch in Git?

A

origin/ (e.g., origin/main, origin/develop)

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

How do you delete a remote branch?

A

git push origin –delete branch-name
or
git push origin :branch-name

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

How do you update your local list of remote branches?

A

“git remote update origin –prune”
or
“git fetch –prune”

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

How do you rename a local branch?

A

git branch -m old-name new-name

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

How do you set up a local branch to track a remote branch?

A

git branch –set-upstream-to=origin/remote-branch local-branch

Let me break this down step by step:

  1. What it does:
    • This command creates a tracking relationship between a local branch and a remote branch
    • It’s useful when you want to connect an existing local branch to an existing remote branch
  2. Command breakdown:
    bash
    git branch --set-upstream-to=origin/remote-branch local-branch

    • --set-upstream-to (can be shortened to -u)
    • origin/remote-branch is the remote branch you want to track
    • local-branch is your local branch name
  3. Real-world example:
    ```bash
    # Let’s say you have:
    # - A local branch called ‘feature’
    # - A remote branch called ‘feature’ on origingit branch –set-upstream-to=origin/feature feature# Shorter version:git branch -u origin/feature feature
    ```
  4. When you need this:
    • When you created a local branch without tracking
    • When you created a branch before the remote branch existed
    • When you want to change which remote branch you’re tracking
  5. After setting this up:
    • You can use git pull without arguments
    • You can use git push without arguments
    • git status will show how many commits you’re ahead/behind

This is different from git push -u because you use it when the remote branch already exists and you just want to connect your local branch to it.

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

How do you fetch a specific branch from remote?

A

git fetch origin remote-branch-name:local-branch-name

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

How do you compare a local branch with its remote counterpart?

A

git diff local-branch origin/remote-branch

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

How do you merge changes from remote main branch into your feature branch?”

A

git checkout feature-branch” then “git merge origin/main”

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

How do you check if there are any remote changes before pushing?

A

git fetch” then “git status” to see if you’re behind/ahead of remote