What is Git and why would you use it?
Git is a version control system that helps us track code changes easily. One of its biggest advantages is the ability to develop a branching strategy and smoothly integrate new features without messing with the stable version of the project.
What is a repository?
A repository is essentially what allows Git to track your project. It contains a directory named
.git, where Git keeps all of its metadata for the repository. There are two ways to start working with a Git repository: clone an existing repository (usual step when joining an existing project) and initialize a new Git repository in your existing local project.
How do you commit code changes?
When committing new code, you first need to stage all your changes with
git add. You can stage all of the changes at once by using
git add . or do it explicitly for the files/directories you wish to commit. Then, you add your commit message with
git commit -m.
If the files are modified and not created for the first time, you could also use the shortcut
git commit -am. However, this is a bad practice as it becomes a routine and you often automatically stage files you didn't intend to.
What is the purpose of
As the name implies,
git status checks the current status of Git and shows which files are staged for commit and which are not. It's a good practice to use it regularly before committing your code, to make sure you don't have any unintended changes (like a forgotten debugger ?).
git diff do?
git diff shows the difference between your last commit and the current stage of your branch. Using it without parameters will show the differences in all files, while using it like
git diff filename will only show the differences for the given file name.
git stash do?
git stash takes the current state of the working directory, puts it on the stack for later and gives you a clean working directory. You would use
git stash when you want to save your current changes and switch to another issue without committing them. When you want to apply those changes back, you would simply use
git stash apply
What is the function of
git config command is a convenient way to set configuration options for your Git installation. The behavior of a repository, user info, preferences etc. can be defined through this command.
What is the purpose of branches in Git?
Branches provide a way to separate new features from the stable code (master branch). A usual workflow used in software development is:
- Branching off a
dev branchto create a new branch for the given feature (or bug)
- Commiting changes to the
- Doing a pull request when done (submitting the code for review)
- Merging the code into
masteronce properly tested.
How does a conflict happen in Git and how should it be resolved?
A conflict in Git occurs when the commit that is about to be merged has some change in one place, and the current commit also has a change at that same place. Git is not able to predict which change should take precedence, so it shows it as a conflict that you should manually solve.
To resolve the conflict in Git, you should manually edit the files and fix the conflicting changes. After that, you should commit those changes to let Git know the conflict has been resolved.
git checkout used for?
git checkout command is used to update directories or specific files in your working tree with those from another branch without merging in the whole branch. When no branch is specified, the directories/files are changed with the last committed version of the current branch.
Setting up and Scaling Remote and Distributed TeamsLearn more
How do you revert to the previous commit in Git?
git reset — hard HEAD will destroy all changes and revert to the previous commit.
Cherry picking in git allows you to choose a commit from one branch and apply it onto another one. You'd use this when you want to pick only certain changes from a given branch, instead of merging all commits.
What is the difference between
git pull and
git fetch only downloads new data from a remote repository, without integrating any of the downloaded data into your working files. All it does is providing a view of this data.
git pull downloads as well as merges the data from a remote repository into your local working files.
Join 5K+ tech leaders
Stay up to date with Software Engineering, Distributed Teams, Agile Talent, and Future of Work content
No spam. Just great articles & insights
You have successfully subscribed.
Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Adeva