GIT - Global Information Tracker - Fun Learning !!
1. What is Git?
Think of Git as a time machine for your code. It allows you to travel back to previous versions, create alternate timelines (branches), and merge them back together.
2. Repositories
A repository (repo) is like a project folder. It contains all your project files and the entire history of changes.
git init
: Initializes a new Git repository.git clone <repository-url>
: Clones an existing repository.
3. Commits
A commit is like taking a snapshot of your project at a specific point in time. Each commit has a unique ID and a message describing the changes.
git add <file>
: Stages changes for the next commit.git commit -m "message"
: Commits the staged changes with a message.
4. Branches
Branches are like parallel universes. You can create a branch to work on a new feature without affecting the main project.
git branch <branch-name>
: Creates a new branch.git checkout <branch-name>
: Switches to the specified branch.git merge <branch-name>
: Merges the specified branch into the current branch.
5. Merging and Conflicts
Merging is like combining two timelines. Sometimes, changes conflict, and you need to resolve these conflicts manually.
git merge <branch-name>
: Merges changes from one branch into another.git status
: Shows the current status of your working directory and staging area.
6. Remote Repositories
Remote repositories are like cloud storage for your code. You can push your changes to a remote repo and pull changes from it.
git remote add origin <repository-url>
: Adds a remote repository.git push origin <branch-name>
: Pushes changes to the remote repository.git pull origin <branch-name>
: Pulls changes from the remote repository.
7. Stashing
Stashing is like putting your work on hold. You can stash your changes and come back to them later.
git stash
: Stashes your changes.git stash apply
: Applies the stashed changes.
8. Rebasing
Rebasing is like rewriting history. It allows you to move or combine commits.
git rebase <branch-name>
: Reapplies commits on top of another base tip.
Best Practices for Using Git
- Commit Often: Make small, frequent commits with clear messages to track changes effectively.
- Use Branches: Create branches for new features, bug fixes, and experiments to keep the main branch stable.
- Write Descriptive Commit Messages: Clearly describe what changes were made and why.
- Pull Before Pushing: Always pull the latest changes from the remote repository before pushing your changes to avoid conflicts.
- Review Changes: Use
git diff
to review changes before committing. - Use Tags: Tag important commits, such as releases, for easy reference.
- Collaborate with Pull Requests: Use pull requests for code reviews and discussions before merging changes.
- Resolve Conflicts Early: Address merge conflicts as soon as they arise to avoid complications later.
- Backup Regularly: Push your changes to a remote repository frequently to ensure your work is backed up.
- Automate Testing: Integrate automated testing to ensure code quality before merging changes.
Interview Questions and Answers
What is Git, and why is it used?
- Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to work on a project simultaneously, manage code versions, and collaborate efficiently.
Explain the difference between git fetch
and git pull
.
git fetch
downloads changes from the remote repository but does not merge them into the local branch. git pull
downloads and automatically merges changes from the remote repository into the current branch.
How do you resolve merge conflicts in Git?
- To resolve merge conflicts, open the conflicting files, manually edit the sections marked by Git, and remove conflict markers. After resolving conflicts, stage the changes using
git add
and commit them with git commit
.
What is the purpose of git stash
?
git stash
temporarily saves changes that are not ready to be committed, allowing you to switch branches or work on something else without losing your progress. You can apply the stashed changes later using git stash apply
.
How do you create a new branch and switch to it?
- To create a new branch, use
git branch <branch-name>
. To switch to the new branch, use git checkout <branch-name>
. Alternatively, you can create and switch to a new branch in one step using git checkout -b <branch-name>
.
Reverting a commit in Git can be done in a few different ways, depending on your needs. Here are the most common methods:
What is Git, and why is it used?
- Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to work on a project simultaneously, manage code versions, and collaborate efficiently.
Explain the difference between git fetch
and git pull
.
git fetch
downloads changes from the remote repository but does not merge them into the local branch.git pull
downloads and automatically merges changes from the remote repository into the current branch.
How do you resolve merge conflicts in Git?
- To resolve merge conflicts, open the conflicting files, manually edit the sections marked by Git, and remove conflict markers. After resolving conflicts, stage the changes using
git add
and commit them withgit commit
.
What is the purpose of git stash
?
git stash
temporarily saves changes that are not ready to be committed, allowing you to switch branches or work on something else without losing your progress. You can apply the stashed changes later usinggit stash apply
.
How do you create a new branch and switch to it?
- To create a new branch, use
git branch <branch-name>
. To switch to the new branch, usegit checkout <branch-name>
. Alternatively, you can create and switch to a new branch in one step usinggit checkout -b <branch-name>
.
Reverting a commit in Git can be done in a few different ways, depending on your needs. Here are the most common methods:
1. Revert a Commit
This method creates a new commit that undoes the changes from a previous commit. It's useful for undoing changes in a shared repository.
git revert <commit-hash>
This method creates a new commit that undoes the changes from a previous commit. It's useful for undoing changes in a shared repository.
git revert <commit-hash>
2. Reset to a Previous Commit
This method moves the current branch to a previous commit, effectively removing all commits after that point. Be cautious with this method, especially in shared repositories, as it rewrites history.
Soft Reset: Keeps changes in the working directory and staging area.
git reset --soft <commit-hash>
Mixed Reset: Keeps changes in the working directory but not in the staging area (default).
git reset <commit-hash>
Hard Reset: Discards all changes in the working directory and staging area.
git reset --hard <commit-hash>
This method moves the current branch to a previous commit, effectively removing all commits after that point. Be cautious with this method, especially in shared repositories, as it rewrites history.
Soft Reset: Keeps changes in the working directory and staging area.
git reset --soft <commit-hash>
Mixed Reset: Keeps changes in the working directory but not in the staging area (default).
git reset <commit-hash>
Hard Reset: Discards all changes in the working directory and staging area.
git reset --hard <commit-hash>
3. Checkout a Previous Commit
This method allows you to view a previous commit without changing the current branch. It's useful for inspecting past states without altering the current branch.
git checkout <commit-hash>
This method allows you to view a previous commit without changing the current branch. It's useful for inspecting past states without altering the current branch.
git checkout <commit-hash>
4. Cherry-Pick a Commit
This method applies changes from a specific commit to the current branch. It's useful for selectively applying changes.
git cherry-pick <commit-hash>
This method applies changes from a specific commit to the current branch. It's useful for selectively applying changes.
git cherry-pick <commit-hash>
Example Scenario
Let's say you want to revert the last commit:
Revert the Last Commit:
git revert HEAD
Reset to the Commit Before the Last:
git reset --hard HEAD~1
Let's say you want to revert the last commit:
Revert the Last Commit:
git revert HEAD
Reset to the Commit Before the Last:
git reset --hard HEAD~1
Important Note
Always be cautious when using git reset --hard
as it can permanently delete changes. For shared repositories, prefer using git revert
to avoid rewriting history.
Always be cautious when using git reset --hard
as it can permanently delete changes. For shared repositories, prefer using git revert
to avoid rewriting history.
Comments
Post a Comment