Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ravidas/question1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Q1.What is cherry-pick?Explain the use case of it and also write steps to do it.


Cherry-Pick: Definition
Cherry-picking in Git refers to the process of selecting a specific commit from one branch and applying it to another branch. This allows developers to selectively apply changes without merging entire branches, giving them finer control over what changes are included.

Use Case
cherry-picking is useful in scenarios where:
- You need to apply a specific bug fix from one branch to another (e.g., from a feature branch to the main branch).
- You want to backport a specific commit to a previous version of the code.
- You need to selectively apply changes without bringing in all commits from another branch.

Steps to Cherry-Pick a Commit

1.Identify the Commit to Cherry-Pick:
- Use `git log` to find the commit hash that you want to cherry-pick.
git log
Note the commit hash (SHA) of the commit you want to cherry-pick.
2.Checkout the Target Branch:
- Switch to the branch where you want to apply the commit.
git checkout target-branch
3. Cherry-Pick the Commit:
- Use the `git cherry-pick` command followed by the commit hash.
git cherry-pick <commit-hash>
- If there are multiple commits to cherry-pick, list them all:
git cherry-pick <commit-hash1> <commit-hash2> ...
4.Resolve Conflicts (if any):
- If there are conflicts, Git will pause the cherry-pick process and allow you to resolve them manually.
- Resolve conflicts in your code, mark them as resolved:
git add <file-with-conflict>
- Continue the cherry-pick process:
git cherry-pick --continue
5.Finalize the Cherry-Pick:
- If the cherry-pick is successful, Git will create a new commit with the changes from the cherry-picked commit.
6.Push the Changes:
- After cherry-picking, push the changes to the remote repository.
git push origin target-branch
46 changes: 46 additions & 0 deletions ravidas/question2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Q2.How to set multiple remote repositories for the same project? Explain the use case. you should have a working demo of it.

Setting Multiple Remote Repositories for the Same Project
In Git, you can configure multiple remote repositories for a single project. This is useful in scenarios such as:
- Collaborating with different teams who use separate repositories.
- Pushing to both a personal and a company repository.
- Keeping a backup of your repository in another location.

Use Case
Consider a scenario where you are working on an open-source project that is hosted on GitHub, but you also want to maintain a backup on GitLab. You can set up multiple remotes to push changes to both platforms.

Steps to Set Multiple Remote Repositories
1.Clone or Initialize Your Repository:
- If you don't already have a repository, create one:
git init my-project
cd my-project
2.Add the First Remote:
- Add the main remote (e.g.GitHub).
git remote add origin https://github.com/username/repo.git

3.Add the Second Remote:
- Add an additional remote (e.g.GitLab) with a different name.
git remote add backup https://gitlab.com/username/repo.git
4.Verify the Remotes:
- Check that both remotes have been added.
git remote -v
- The output will list both `origin` and `backup` remotes:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
backup https://gitlab.com/username/repo.git (fetch)
backup https://gitlab.com/username/repo.git (push)
5.Push to Both Remotes:
- Push changes to the `origin` remote (GitHub):
git push origin main
- Push changes to the `backup` remote (GitLab):
git push backup main

6.Working with Multiple Remotes:
- You can fetch from any remote:
git fetch backup
- Pull from a specific remote:
git pull origin main
- Push changes to all remotes simultaneously using a loop or by configuring a default behavior (though this requires scripting and isn't native to Git).

Conclusion
Setting multiple remotes allows you to manage and collaborate across different platforms efficiently. It's a powerful feature for those who need to maintain codebases in multiple locations or repositories.
44 changes: 44 additions & 0 deletions ravidas/question3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Q3. How to combine two commits into one commit? For Example, you have 4 different commits C1, C2, C3, and C4, and you to combine C2 and C3 into one single commit.
Combining Two Commits into One (Squashing Commits)

To combine (squash) two commits into one, you can use an interactive rebase in Git. This process is commonly used to clean up the commit history by merging multiple related commits into a single, more meaningful commit.
Use Case

For example, if you have four commits (`C1`, `C2`, `C3`, `C4`) and you want to combine `C2` and `C3` into one commit, you can do this to simplify the commit history, making it easier to understand and review.

Steps to Combine `C2` and `C3` into One Commit
1.Start an Interactive Rebase:
- Begin an interactive rebase for the last 4 commits:
git rebase -i HEAD~4
2.Modify the Rebase Todo List:
- This will open a text editor showing a list of the last 4 commits:
pick <hash1> C1
pick <hash2> C2
pick <hash3> C3
pick <hash4> C4
3.Squash Commits:
- To combine `C2` and `C3`, change the word `pick` for `C3` to `squash` (or just `s`):
pick <hash1> C1
pick <hash2> C2
squash <hash3> C3
pick <hash4> C4
4.Edit Commit Message:
- After closing the editor, Git will pause to allow you to combine the commit messages. It will open another editor showing the commit messages of `C2` and `C3`.
- You can edit the message to something more concise or relevant for the combined commit:
# This is a combination of 2 commits.
# The first commit's message:
C2's commit message
# The second commit's message:
C3's commit message
- Modify the commit message as needed, save, and close the editor.
5.Finalize the Rebase:
- Git will apply the rebase and squash `C2` and `C3` into a single commit. If there are conflicts, resolve them manually, then continue the rebase:
git rebase --continue
6.Push Changes (if necessary):
- If you have already pushed the commits to a remote repository, you will need to force-push the changes:
git push origin branch-name --force
Notes
-Backup: It's good practice to back up your branch before rebasing, especially if working on a shared branch.
-History Rewriting: Rebasing rewrites commit history, so it's advised not to rebase commits that have already been pushed to a shared repository unless all collaborators are aware and agree.

Using `git rebase -i` is a powerful way to manage commit history, making it cleaner and more understandable.
31 changes: 31 additions & 0 deletions ravidas/question4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Q4. How to delete one commit or multiple commits permanently Write a command for it.
Deleting One or Multiple Commits Permanently in Git
Deleting commits from your Git history can be done using different approaches depending on whether you want to delete the most recent commit(s) or specific commits in the middle of the history. It’s important to note that once you delete commits, they are removed from the history, so be cautious when performing these actions, especially if the commits have been pushed to a shared repository.

Scenarios and Commands
1.Deleting the Most Recent Commit
To delete the most recent commit permanently:
-Command:
git reset --hard HEAD~1
- This command moves the branch pointer back by one commit and resets the working directory and index to match the new HEAD. The last commit is deleted from the history.
2.Deleting Multiple Recent Commits
To delete the last `n` commits:
-Command:
git reset --hard HEAD~n
- Replace `n` with the number of commits you want to remove.

3.Deleting a Specific Commit (Interactive Rebase)
To delete a specific commit that is not the most recent, use an interactive rebase:
-Start an Interactive Rebase:
git rebase -i HEAD~n
- Replace `n` with the number of commits you want to look back.

3.Force Pushing After Deletion
If you have pushed the commits to a remote repository, you will need to force push the changes to overwrite the remote history:
-Command:
git push origin branch-name --force
Caution
-Collaboration: Deleting commits that have been pushed to a shared repository can cause issues for other collaborators. Ensure everyone is aware and has agreed to the changes.
-Backup: Before performing destructive operations like `git reset --hard` or `git rebase`, consider creating a backup branch or tag for safety.

By using these methods, you can remove unwanted commits from your Git history effectively, but always handle such operations with care to avoid disrupting your workflow or your team's.
53 changes: 53 additions & 0 deletions ravidas/question5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Q5. Difference between --force push and --force-with-lease, You have to show the use of both, also you have to show in what scenario --force-with-lease is used

Difference Between `--force` and `--force-with-lease` in Git
Both `--force` and `--force-with-lease` are options used when pushing changes to a remote repository, and they are primarily used when rewriting history, such as after a rebase or a commit deletion. However, they have distinct behaviors and use cases.

1.`--force` Push
Definition
-`--force`: This option forces the remote repository to accept the changes, regardless of the state of the remote branch. It overwrites the remote branch with the local branch forcefully.

Use Case
- Scenario: You have rewritten commit history (e.g., via `git rebase` or `git reset`) and want to push these changes to a remote branch.

Example
-Command:
git push origin branch-name --force
- This will overwrite the remote branch `branch-name` with your local branch, even if there are changes on the remote that you don’t have locally.

Risk
-Overwrites: It can overwrite commits on the remote branch that others might have pushed after your last pull, potentially leading to lost changes.

2.`--force-with-lease` Push
Definition
- `--force-with-lease`: This option is a safer alternative to `--force`. It checks if the remote branch has been updated since you last fetched from it. If the remote branch has been updated, the push is rejected, preventing potential overwrites of someone else’s changes.

Use Case
-Scenario: You want to rewrite the history on a remote branch, but you also want to ensure that you do not overwrite any new commits that others might have pushed since your last fetch.

Example
- Command:
git push origin branch-name --force-with-lease
- This checks if the remote branch is in the same state as your last fetch. If the remote has changed, the push will fail, prompting you to resolve any discrepancies.

Safety
-Prevents Overwrites: It acts as a safeguard by ensuring that your push doesn’t overwrite changes pushed by others that you haven’t seen yet.

Practical Demonstration of Use
Scenario 1: Using `--force`
1.Rebase your branch:
git rebase -i HEAD~3
2.Force Push the Changes:
git push origin branch-name --force
- This will overwrite the remote branch even if others have pushed new commits.
Scenario 2: Using `--force-with-lease`
1.Rebase your branch:
git rebase -i HEAD~3
2.Push with Safety:
git push origin branch-name --force-with-lease
- If someone else has pushed new commits to the remote branch since your last fetch, the push will fail. You will need to pull those changes and resolve any conflicts before pushing again.
Conclusion
- `--force`: Use when you are sure that overwriting the remote branch won’t cause issues or when working on a private branch.
- `--force-with-lease`: Use in collaborative environments to prevent accidentally overwriting others’ changes, ensuring you don’t disrupt the team’s workflow.

In collaborative projects, `--force-with-lease` is generally the safer and more recommended option to avoid inadvertently losing work.
26 changes: 26 additions & 0 deletions ravidas/question6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Q5. How to un-stage changes from the staged area. write command for it.
Unstaging Changes from the Staged Area
If you have staged changes (added to the index) and want to move them back to the working directory, you can unstage them using the `git restore` or `git reset` commands.

Commands to Unstage Changes
1. Unstaging a Specific File**
- Command:
git restore --staged <file-path>
- This command removes the specified file from the staging area without changing the working directory.
- Example:
git restore --staged src/app.js
2.Unstaging All Files
-Command:
git restore --staged .
- This command removes all staged changes from the index.
3.Using `git reset` to Unstage
-Command (Specific File):
git reset <file-path>
- This moves the file from the staging area back to the working directory.
-Example:
git reset src/app.js
- Command (All Files):
git reset
- This unstages all changes but keeps the modifications in the working directory.
Note
- Unstaging does not discard the changes; it simply moves them from the staged area back to the working directory, so they are no longer included in the next commit.