Git Flashcards
(18 cards)
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? Abd remove obsolete ones
“git remote update origin –prune”
or
“git fetch –prune”
Both achieve the same essential goal: they synchronize your local references to match what’s actually on the remote repository, cleaning up any stale references to branches that have been deleted on the remote.
You can also use git fetch origin --prune
if you want to only fetch from a specific remote but still prune obsolete references.
How do you rename a local branch?
git branch -m old-name new-name
You’ve already created a local branch
2. You have a remote branch on the server (like origin)
3. You want to connect them so your local branch tracks changes from the remote
git branch -u origin/remote-branch-name local-branch-name
How do you fetch a specific branch from remote?
git fetch origin remote-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
Assume that you started to merge from remote to local. And you get this message:
[amirmhd@cedar2 multi_modal_CSI]$ git pull origin
» hint: Diverging branches can’t be fast-forwarded, you need to either: git merge –no-ff or git rebase
» fatal: Not possible to fast-forward, aborting.
How you see difference between local and the remote branch.
git diff —name-only HEAD..origin/main
Assume that you started to merge from remote to local. And you get this message:
[amirmhd@cedar2 multi_modal_CSI]$ git pull origin
» hint: Diverging branches can’t be fast-forwarded, you need to either: git merge –no-ff or git rebase
» fatal: Not possible to fast-forward, aborting.
How to see what would happen if we merge remote files to local without committing the changes to local?
Git merge —no-commit —no-ff origin/main
After this, if it went well the files will be staged and you need to commit them in local repository.
Explanation:
–no-commit - Performs the merge but stops before creating a commit, allowing you to inspect the result and make changes if needed before committing
–no-ff - “No Fast Forward” - Forces Git to always create a merge commit, even if the merge could be performed with a fast-forward