How do you know if a certain directory is a git repository?
You can check if there is a ".git" directory.
What is the difference between git pull
and git fetch
?
Shortly, git pull = git fetch + git merge.
When you run git pull, it gets all the changes from the remote repository and attaches it to your corresponding branch in your local repository.
git fetch gets all the changes from the remote repository, stores the changes in a separate branch in your local repository
Explain what the file gitignore
is used for?
The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached.
How to check if a file is tracked and if not, then track it?
There are different ways to check whether a file is tracked or not:
git ls-files <file>
-> exit code of 0 means it's trackedHow can you see which changes have done before committing them?
git diff
What git status
does?
git status
helps you to understand the tracking status of files in your repository. Focusing on working directory and staging area - you can learn which changes were made in the working directory, which changes are in the staging area and in general, whether files are being tracked or not.You've created new files in your repository. How to make sure Git tracks them?
git add FILES
What is the difference between git reset
and git revert
?
git reset
:- Moves the
HEAD
pointer to a specific commit and optionally modifies the staging/index and working directory.
- Alters history (used cautiously in shared branches).
- Types:
-soft
: Keeps changes staged.-mixed
(default): Keeps changes in the working directory.-hard
: Discards all changes.
git revert
:- Creates a new commit that undoes the changes made by a previous commit.
- Does not alter history, making it safer for shared repositories.
How can you view the commit history in Git?
You use the
git log
command.Common flags include:
git log
: Displays commits with the author, date, and commit message.
git log --oneline
: A condensed one-line summary per commit.
git log --graph
: Visualizes branch and merge history.
git log -p
: Shows diffs introduced in each commit.
Scenarios
What's is the branch strategy (flow) you know?
- Git flow
- GitHub flow
- GitLab flow
- Trunk based development
How to resolve git merge conflicts?
First, you open the files which are in conflict and identify what are the conflicts. Next, based on what is accepted in your team, you either discuss with your colleagues on the conflicts or resolve them by yourself. After resolving the conflicts, you add the files with
git add
Finally, you run git rebase --continue
How do you discard local file changes? (before commit)
git checkout -- <file_name>
What git diff does?
git diff
can compare between two commits, two files, a tree and the staging area, etc.Explain GitLab Flow branching strategies?
GitLab Flow simplifies branching by aligning development with deployment environments.
- Main Branch: Always contains production-ready code. Deployments come from here.
- Feature Branches: Used for developing new features or fixes. Developers create merge requests to integrate them into the
main
branch after reviews and testing.
- Environment Branches:
production
: Reflects the live environment.staging
: Used for testing production-ready features.
- Tags for Releases: Tags like
v1.0.0
mark specific commits as releases for easy tracking and rollback.
- Hotfix Branches: Urgent production fixes are done on a
hotfix
branch and merged intomain
and other relevant branches.
Explain is git squash
?
Git squash is the process of combining multiple commits into a single commit.
It Combines multiple commits into one, making the history easier to read.