Git Flashcards
how to remove a file from git staging area?
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
What command shows all remote branches in a Git repository?
git branch -r
How do you create and switch to a new branch in one command?
git checkout -b branch-name
What command shows both local and remote branches?
git branch -a
How do you push a new branch to remote and set up tracking?
git push -u origin branch-name
What command fetches the latest information from remote without merging?
git fetch
How do you see detailed information about remote branches including latest commits?
git remote show origin
What prefix typically indicates a remote branch in Git?
origin/ (e.g., origin/main, origin/develop)
How do you delete a remote branch?
git push origin –delete branch-name
or
git push origin :branch-name
How do you update your local list of remote branches?
“git remote update origin –prune”
or
“git fetch –prune”
How do you rename a local branch?
git branch -m old-name new-name
How do you set up a local branch to track a remote branch?
git branch –set-upstream-to=origin/remote-branch local-branch
Let me break this down step by step:
-
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
-
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
-
-
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
``` -
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
-
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
- You can use
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 do you fetch a specific branch from remote?
git fetch origin remote-branch-name:local-branch-name
How do you compare a local branch with its remote counterpart?
git diff local-branch origin/remote-branch
How do you merge changes from remote main branch into your feature branch?”
git checkout feature-branch” then “git merge origin/main”
How do you check if there are any remote changes before pushing?
git fetch” then “git status” to see if you’re behind/ahead of remote