Final Github Questions 17.12.2023-Answer Key
Final Github Questions 17.12.2023-Answer Key
(Regulations 2022)
1. Which Git command changes where the HEAD pointer points and modifies the [CO1 [K
contents of the working directory? ] 1]
The `git checkout` command in Git is used to switch branches or restore files in the
working directory. It also changes where the HEAD pointer points depending on how
it's used:
1. **Switching Branches:** When you use `git checkout` followed by a branch name, it
switches the HEAD pointer to that branch, updating the contents of the working
directory to reflect the state of that branch.
Example:
```bash
```
This command switches to the `main` branch, updating the working directory to
match the state of the `main` branch.
2. **Checking Out Files:** You can also use `git checkout` to restore individual files or
directories to their state at a specific commit or branch.
Example:
```bash
```
This command restores the `<file_name>` to the state it has in the latest commit
(`HEAD`), modifying the contents of the working directory for that specific file.
It's important to note that in Git, the `HEAD` pointer usually refers to the currently
active branch or commit, and `git checkout` helps to manipulate what the `HEAD`
points to, thus affecting the working directory contents accordingly.
**Git:**
Git is a distributed version control system. It's a tool that allows developers to manage
and track changes to their codebase. Key points about Git include:
1. **Version Control:** Git tracks changes to files over time, allowing users to revert to
specific versions, compare changes, and collaborate on projects efficiently.
2. **Distributed:** Every Git directory on a user's computer is a full-fledged repository
with complete history and version-tracking capabilities, independent of network access
or a central server.
3. **Command-Line Tool:** Git is primarily a command-line tool, although there are
also graphical user interface (GUI) clients available.
4. **Local Operations:** Most operations in Git are local, meaning they don't require a
network connection. This allows for faster branching, committing, merging, and more.
**GitHub:**
GitHub is a web-based hosting service for Git repositories. It provides a platform for
collaboration, sharing, and hosting Git repositories. Key points about GitHub include:
1. **Hosting Service:** GitHub hosts Git repositories remotely on its servers, making
it easy for developers to collaborate with others by pushing and pulling changes to and
from these repositories.
2. **Social Collaboration:** GitHub offers features for issue tracking, pull requests,
2
code review, and project management. It facilitates collaboration among teams or
communities working on software projects.
3. **Web Interface:** While Git is primarily command-line-based, GitHub provides a
web interface that allows users to navigate repositories, view code, manage issues,
perform pull requests, and more.
4. **Additional Features:** GitHub offers various features beyond basic version
control, including wikis, actions (for CI/CD workflows), insights (such as analytics and
project management tools), and integrations with other services.
Example:
```bash
git reset --soft HEAD~1
```
This command resets the HEAD pointer to the commit before the current HEAD,
keeping the changes staged.
Example:
```bash
git reset HEAD~1
```
This command resets the HEAD pointer to the commit before the current HEAD,
3
unstaging the changes.
Example:
```bash
git reset --hard HEAD~1
```
This command resets the HEAD pointer to the commit before the current HEAD,
discarding all changes made after that commit.
Remember, while these `git reset` modes are powerful for managing commit history,
caution is necessary, especially with `--hard` resets, as they can discard changes
irreversibly. Always ensure you're resetting to the correct commit to avoid losing
valuable work.
3. **Understanding Changes:** Developers can view what changes were made in each
commit. Using flags like `-p` or `--patch` with `git log` displays the actual content changes
introduced in each commit, allowing for a detailed review of code modifications.
5. **Commit Metadata and Context:** `git log` displays metadata associated with each
commit, such as commit messages, author details, timestamps, and commit hashes. This
information provides context for understanding why changes were made and by whom.
6. **Finding Bugs or Issues:** Developers often use `git log` to trace the introduction of
4
bugs or issues. By examining the commit history and changes, they can identify when a
bug was introduced, which commit caused it, and potentially track the changes that might
resolve it.
Overall, `git log` is a fundamental command that provides a comprehensive view of the
history of a Git repository. It serves as a crucial tool for developers to understand the
evolution of the codebase, review changes, investigate issues, and navigate through commit
history efficiently.
5. Why does the C5 commit node point at both C3 and C4 in given Git repo? [CO3 [K
] 2]
6. Which error would you get if you try to push master-branch changes to a remote [CO3 [K
repository, and someone else pushed changes to that same branch while you were ] 2]
making your changes?
If you attempt to push changes to the master branch of a remote repository and
someone else has pushed changes to the same branch before you, you would
typically encounter a "rejected - non-fast-forward" error
7. Write the procedure for creating a remote repository with named ‘Remote 1’along with [CO4 [K
Readme.md file. ] 3]
To create a remote repository named 'Remote 1' along with a `Readme.md` file, follow
these steps:
5
- Look for an option/button to create a new repository.
- Optionally, you may add a description and choose whether to make it public or
private.
- It's often possible to initialize the repository with a `Readme.md` file directly from
the web interface. You can select an option during the repository creation process to
add this file.
Open your terminal or command prompt and navigate to the directory where you
want to create your local repository.
```bash
git init
```
- Link your local repository to the remote repository you just created (replace
`<remote_repository_URL>` with the URL provided by your remote service):
```bash
```
```bash
```
6
- Add and commit the `Readme.md` file:
```bash
```
- Push the `Readme.md` file to the `master` branch of the remote repository:
```bash
```
The `-u` flag sets the upstream branch, linking your local `master` branch to the
`origin/master` branch on the remote repository.
Go back to the remote service website (e.g., GitHub, GitLab) and refresh the
repository page. You should see the `Readme.md` file and the initial commit reflected
in your 'Remote 1' repository.
This process creates a new remote repository named 'Remote 1', initializes a local Git
repository, adds a `Readme.md` file, and pushes it to the remote repository's `master`
branch. Adjust the steps based on the specific remote service you're using and any
variations in their interface or repository creation process.
- **`git fetch`:**
- Fetches changes from the remote repository to your local repository.
- Updates your remote tracking branches (e.g., `origin/master`, `origin/main`) to
7
reflect the state of the remote repository.
- Does not automatically merge or modify your local branches. It only brings the
changes into your local repository, allowing you to inspect and integrate them later.
- Fetching doesn't affect your working directory or local changes.
Example:
```bash
git fetch origin
```
- **`git pull`:**
- Pulls changes from a remote repository and integrates them into the current branch.
- Automatically fetches the changes (like `git fetch`) and then merges the changes into
your current branch, updating your working directory.
- It's a combination of `git fetch` followed by `git merge` or `git rebase` based on your
repository's configuration (default is `git merge`).
- Can potentially cause conflicts if there are changes both locally and remotely on the
same files/lines.
Example:
```bash
git pull origin master
```
**Key Differences:**
2. **Potential Conflicts:** `git pull` can cause conflicts if there are conflicting changes
between your local branch and the remote branch being pulled. `git fetch` doesn't
directly modify your local branch, so it avoids immediate conflicts.
3. **Control and Inspection:** `git fetch` gives you more control and time to review
changes before merging them into your working branch. It allows you to inspect the
changes fetched from the remote repository separately from your local changes.
In summary, `git fetch` is used to download changes from a remote repository to your
local repository, while `git pull` is used to download changes and automatically merge
them into your current branch. `git pull` is a more automatic and immediate way to
update your local branch with the latest changes from a remote repository but may
lead to potential conflicts that need resolution.
9. Show the steps for creating a webpage and hosting it in Github. [CO4 [K
] 3]
10. Identify the features supported by fork command. [CO4 [K
] 2]
Part – B (3 × 10 = 30 Marks)
ANSWER ANY THREE QUESTIONS
11. i) Demonstrate the three-tire architecture of git workflow with a neat sketch. (6) [CO2 [K
As a text-based AI, I can't create visual sketches directly. However, I can ] 2]
describe the three-tier architecture of a typical Git workflow:
8
1. **Local Repository (First Tier):**
- This tier represents your local environment where you perform most of your
work.
- It includes your local Git repository, where you can create branches, make
changes, stage those changes, commit them, and manage your version control.
- All these operations occur on your local machine without needing a network
connection.
Arrows between tiers can illustrate actions like pushing changes from the local
repository to the remote repository and pulling changes from the remote
repository to the local repository.
ii) Illustrate the concept git clone with an example. (4) [CO2 [K
Certainly! The `git clone` command is used to create a copy of a remote ] 3]
repository onto your local machine. Here's an example to illustrate how `git
clone` works:
Let's say there's a remote repository hosted on GitHub containing a project you
want to work on. The repository URL is
`https://github.com/exampleuser/example-repository.git`.
9
To clone this remote repository to your local machine, follow these steps:
2. Navigate to the directory where you want to place the cloned repository. For
example:
```bash
cd /path/to/desired/location
```
3. Use the `git clone` command followed by the URL of the remote repository:
```bash
git clone https://github.com/exampleuser/example-repository.git
```
4. Press Enter to execute the command. Git will clone the entire repository from
GitHub to your local machine.
5. Once the cloning process is complete, you will have a new directory created in
your current location named `example-repository` (assuming that was the name
of the repository). This directory contains a copy of all the files and the entire
commit history of the remote repository.
Now, you have successfully cloned the remote repository onto your local
machine. This cloned repository is linked to the original remote repository,
allowing you to fetch updates, make changes, and interact with the remote
repository by pushing your changes or pulling updates made by others.
12. Create a local repository and execute the following git commands and show the
result
● Unstage ‘f2.txt’
10
● Commit the changes
Sure, here are the steps to execute the Git commands you provided:
```bash
cd "Repo 1"
git init
```
```bash
```
```bash
```
11
4. **Unstage 'f2.txt':**
```bash
```
```bash
```
```bash
```
```bash
git add .
```
```bash
```
12
9. **Update the contents of staged file 'f4.txt':**
```bash
```
```bash
```
```bash
```
```bash
```
These commands will create a local repository named 'Repo 1', perform the
specified operations with different files, commit those changes at each step, and
finally display the commit history using `git log --oneline` command, showing a
concise view of the commits made in the repository.
13. i) Create a local repository ‘Repo 1’ and perform two commits then reflect the (5) [CO4 [K
changes of local repository with remote repository. ] 3]
To perform this task, you'll create a local repository named 'Repo 1', make two
ii)
commits in the local repository, and then synchronize these changes with a
remote repository. (5) [CO4 [K
] 3]
13
Here are the step-by-step commands to accomplish this:
This sequence of commands will create a local repository, make two commits,
and then link and push these changes to a remote repository. Ensure you have
the necessary permissions to push changes to the remote repository. Adjust the
URLs and branch names as needed for your specific remote repository setup.
Demonstrate the synchronization between local and remote repository with the
following.
● Create a new file ‘rf1.txt’ and ‘Readme.md’ in remote repo
● Create a new file ‘1f1.txt’in local repo and commit the changes in local
repo
● Push the changes from local repo to remote repo
Certainly! Here are the steps to demonstrate synchronization between a
local and remote repository:
```bash
git clone <remote_repository_URL>
```
4. **Create a new file '1f1.txt' in the local repository and commit the
changes:**
Create the file and commit the changes in your local repository.
15
```bash
echo "Content for 1f1.txt" > 1f1.txt
git add 1f1.txt
git commit -m "Add 1f1.txt to local repository"
```
Push the committed changes from your local repository to the remote
repository.
```bash
git push origin master
```
This command pushes the changes from the local `master` branch to the
remote repository (`origin`). It will update the 'rf1.txt' file if changes were made
in the local repository and also add the new file '1f1.txt' to the remote
repository.
14. i). Draw the AFTER-execution state of the pictured repository after a Git
commit operation (make up a hash). [CO3 [K
(5) ] 3]
Before:
ii). Draw the AFTER-execution state of the pictured repository after running the
following commands.
$ git checkout master Before:
$ git merge myfix
(5) [CO3 [K
] 3]
16
17