0% found this document useful (0 votes)
28 views28 pages

Git (AutoRecovered)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views28 pages

Git (AutoRecovered)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1. What is git and why it is used?

Git is a distributed version control system (DVCS) designed to track changes in source
code during software development. Git allows multiple developers to collaborate on projects
efficiently, even when they are geographically dispersed.

Git is used:

1. Version Control: Git allows developers to keep track of changes made to the source
code over time. This enables them to revert to previous versions if necessary, compare
changes, and collaborate effectively.
2. Branching and Merging: Git provides powerful branching and merging capabilities,
allowing developers to work on multiple features or bug fixes simultaneously without
interfering with each other's work.
3. Collaboration: Git facilitates collaboration among developers by allowing them to work
on the same codebase concurrently.
4. Distributed Development: Unlike centralized version control systems, Git is distributed,
meaning that each developer has a complete copy of the repository, including its entire
history.
5. Open Source: Git is open source software, meaning that its source code is freely
available for anyone to inspect, modify, and distribute.

2. Explain the difference between git and github?

Git:

1. Git is a distributed version control system (DVCS) that allows developers to track
changes in source code during software development.
2. It provides functionalities such as version control, branching, merging, and
collaboration among developers.
3. Git operates locally on a developer's computer and does not require a central server to
function.
4. It is a command-line tool that can be used independently of any online platform.

GitHub:

1. GitHub is a web-based platform that provides hosting for Git repositories and
additional collaboration tools.
2. It allows developers to store their Git repositories remotely on GitHub's servers,
making it easy to share code with others and collaborate on projects.
3. GitHub provides features such as issue tracking, project management, code review, and
continuous integration.
4. It offers a graphical user interface (GUI) for interacting with Git repositories, in
addition to its web-based interface.
5. GitHub is not the only platform that hosts Git repositories; alternatives include GitLab,
Bitbucket, and others.

3. How do you install git in your machine?

To install git:

1. Visit the official Git website: https://git-scm.com/.


2. Download the latest version of Git for Windows.
3. Run the downloaded installer.
4. Follow the installation wizard's instructions, selecting the default options unless
you have specific preferences.
5. After installation, open a command prompt or Git Bash to verify that Git has been
installed correctly by typing git --version. You should see the installed Git version
displayed.

4. How do you configure your username and email in git?


To configure your username and email address in Git, you can use the git config
command with the --global flag to set these values globally, meaning they will be used for all
Git repositories on your system.
1. Open a terminal or command prompt
2. Type the following command to set your username:
-> git config --global user.name "Your Name"
Replace "Your Name" with your actual name.

3. Next, set your email address using the following command:

-> git config --global user.email "[email protected]"


Replace "[email protected]" with your actual email address.

These commands will configure your username and email address globally in Git. You can
verify that the settings have been applied correctly by using the git config --list
command, which will display all Git configuration settings. Your username and email address
should be listed among them.

5. What is a repository in Git?


In Git, a repository, often abbreviated as "repo," is a collection of files and directories, along
with the version history of those files and directories. It serves as a centralized location for
storing and managing a project's source code and related files. Git repositories facilitate version
control, allowing developers to track changes, collaborate with others, and manage the evolution
of their projects over time

6. How do you create a new git repository?

To create a new Git repository, you can follow these steps:

Initialize a New Repository: Navigate to the directory where you want to create your new
repository using the command line or terminal. Then, use the git init command to initialize
a new Git repository in that directory

-> cd /path/to/your/directory
-> git init

This command creates a new .git directory in your project directory, which
contains all the necessary files and subdirectories for Git to manage your r e
repository.

Add Files: After initializing the repository, you can add your project files to it. You can add all
files using the git add .command, or you can specify individual files by name

-> git add . # Add all files


-> git add filename # Add specific file

Commit Changes: Once you've added the files you want to include in the initial commit, you
need to commit them to the repository using the git commit command. This
creates a snapshot of the current state of your project.

-> git commit -m "Initial commit"

Replace "Initial commit" with a descriptive message that summarizes the


changes you've made in this commit.

Your new Git repository is now set up and contains your project files.
7. How do you clone a repository from GitHub?

To clone a repository from GitHub, follow these steps:

1. Copy Repository URL: Go to the GitHub repository you want to clone in your
web browser. Click on the green "Code" button, which will reveal a URL. Copy
the URL to your clipboard.

2. Open Terminal/Command Prompt: Open a terminal or command prompt


on your computer.

3. Navigate to Desired Directory: Use the cd command to navigate to the


directory where you want to clone the repository. For example:

-> cd /path/to/destination/directory

4. Clone the Repository: Use the git clone command followed by the
repository URL you copied from GitHub. This command will download the
entire repository to your local machine.

-> git clone <repository_URL>

Replace <repository_URL> with the URL you copied earlier.

For example:

git clone https://github.com/username/repository.git

If the repository is private and requires authentication, you may need to use
the SSH URL or provide your GitHub credentials when prompted.

5. Authenticate (if necessary): If the repository is private and requires


authentication, you may be prompted to provide your GitHub username and
password or access token. Enter the required credentials to proceed with
cloning process.

6. Verification: Once the cloning process is complete, you should see a


message indicating that the repository has been cloned successfully. You
can navigate into the newly created directory using cd and start working with
the files in the repository.

Now you have successfully cloned a repository from GitHub to your local machine.

8. What is the purpose of the .gitignore file?

The .gitignore file is used to specify intentionally untracked files that Git should ignore.
These are typically files that are generated during the build process or contain sensitive
information that should not be included in the repository. The purpose of the .gitignore file is
to prevent these files from being accidentally committed to the repository, which helps keep the
repository clean and focused on the source code and other important files.

9. How do you check the status of your working directory in Git?

To check the status of your working directory in Git, you can use the git status
command. Here's how:

1. Open a terminal or command prompt.

2. Navigate to your Git repository directory using the cd command.

3. Once you're in the repository directory, simply type the following command
. and press Enter:

-> git status

This command will display the current status of your working directory, including
information about:

 Modified files (changes that have been made but not yet staged for commit)
 Staged files (changes that have been staged for commit)
 Untracked files (files that are not yet tracked by Git)
 Branch information (the current branch and how it relates to the remote
repository, if any)

10. How do you add files to the staging area in Git?

To add files to the staging area in Git, you can use the git add command followed by the
names of the files you want to stage. Here's how:
1. Add Specific Files: To add specific files to the staging area, you can use the
following command

-> git add filename1 filename2 ...

Replace filename1, filename2, etc., with the names of the files you want to
stage.

For example:

-> git add index.html style.css

2. Add All Files: If you want to add all modified and untracked files in the current
directory and its subdirectories to the staging area, you can use the following
command:

-> git add .

This command stages all changes in the current directory and its subdirectories.

3. Add Changes Interactively: You can use the interactive mode of git add to
selectively stage changes within files. This allows you to review and stage changes
in chunks or lines. To enter interactive mode, use the following command:

-> git add -i

This will open a prompt where you can choose from a list of options for staging
changes interactively.

After adding files to the staging area, you can use git status to see the changes that
have been staged. Once you're satisfied with the changes in the staging area, you can commit
them using git commit.

11. Explain the concept of commits in Git?

concepts related to commits in Git:


1.Committing Changes: When you're working on a project, you make changes to files
in your repository. To save these changes as a commit, you first stage the changes
using git add to add them to the staging area. Then, you use the git commit
command to create a new commit with a commit message that describes the
changes you've made.

2.Commit Hash: Each commit in Git has a unique identifier called a commit hash, which
is a long string of characters generated based on the contents of the commit. This hash is
used to reference the commit and is used in various Git commands and operations.

3.Commit Message: When you create a commit, you provide a commit message that
describes the changes made in the commit. A good commit message is concise and
descriptive, providing enough information for other developers (and your future self)
to understand the purpose and context of the changes.

4.Commit History: Commits form a linear sequence or tree-like structure called the
commit history. Each commit points to its parent commit(s), representing the history
of changes in the project. You can visualize the commit history using tools like git log
or graphical interfaces provided by Git hosting platforms.

5.Branches and Merge Commits: In Git, branches allow you to work on different
features or bug fixes in isolation from each other. When you merge branches
together, Git creates a new commit called a merge commit, which combines the
changes from the merged branches into the main branch's history.

6.Reverting and Rewriting History: Commits can be reverted or amended to modify


the project's history. Reverting a commit creates a new commit that undoes the
changes introduced by the original commit, while amending a commit allows you to
modify its content or commit message.

12. How do you create a new commit in Git?

To create a new commit in Git, you typically follow these steps:

1.Make Changes: First, make changes to the files in your working directory. You can
modify existing files, create new files, or delete files as needed to implement your
changes.
2.Stage Changes: After making your changes, you need to stage them for commit. Use
the git add command to stage the files you want to include in the commit. You can
either stage specific files or directories, or you can stage all changes using git add . to
include all modified and untracked files:

-> git add <filename> # Stage specific file


-> git add . # Stage all changes

3.Review Staged Changes: You can use git status to review the changes that have
been staged for commit. This allows you to verify that you've staged the correct changes
before creating the commit.

-> git status

4.Create Commit: Once you've staged the changes you want to include in the commit,
you can create the commit using the git commit command. When you run this
command, Git will open your default text editor for you to write a commit message.
-> git commit

Alternatively, you can use the -m option to specify a commit message directly on the
command line:

-> git commit -m "Your commit message"

Replace "Your commit message" with a brief, descriptive message that summarizes
the changes made in the commit.

5.Save Commit: After writing the commit message, save and close the text editor. Git
will then create the commit, which includes the changes you staged in the previous step,
along with the commit message you provided.

Your new commit is now created and saved in the repository, capturing the changes you made
to the project.

13. What is the purpose of Git log command?

The git log command in Git is used to view the commit history of a repository. It
displays a list of commits in reverse chronological order, with the most recent
commits shown first. The git log command provides detailed information about
each commit, including the commit hash, author, date, and commit message.

Here are some of the main purposes of the git log command:

1.Viewing Commit History: The primary purpose of git log is to view the commit
history of a Git repository. It allows you to see a chronological list of all commits,
providing insight into how the project has evolved over time.
2.Commit Details: git log displays detailed information about each commit, including
the commit hash (a unique identifier for the commit), the author of the commit, the date
and time the commit was made, and the commit message (a brief description of the
changes made in the commit).
3.Filtering Commits: You can use various options and filters with git log to
customize the output. For example, you can limit the number of commits displayed,
specify a range of commits to show, or filter commits by author, date, or commit
message.
4.Viewing Branch History: When run without any additional parameters, git log
displays the commit history of the current branch. This can be useful for understanding
the development history of a specific branch.
5.Visualizing Commit Relationships: git log can display commit history in a linear
or graphical format, showing the relationships between commits and branches. This can
help you understand how different branches diverge and merge over time.

14. How do you view the history of commits in a repository?

To view the history of commits in a Git repository, you can use the git log
command. Here's how:

1.Open a terminal or command prompt.


2.Navigate to your Git repository directory using the cd command.
3.Once you're in the repository directory, simply type the following command and press
Enter:

-> git log

This command displays a list of commits in reverse chronological order, with the most
recent commit appearing first. Each commit is displayed with the following information:
 Commit hash: A unique identifier for the commit.
 Author: The name and email address of the person who made the commit.
 Date: The date and time when the commit was made.
 Commit message: A brief description of the changes made in the commit.

15. How do you view the changes made in commit?

To view the changes made in a specific commit in Git, you can use the git show
command followed by the commit hash or a reference to the commit.

1.Find the Commit Hash: First, you need to identify the commit hash of the commit you want
to view the changes for. You can use git log to find the commit hash. For example:

-> git log

This command will display a list of commits along with their commit hashes. Note
down the commit hash of the commit you're interested in.

2.View Changes: Once you have the commit hash, use the git show command followed by the
commit hash to view the changes made in that commit. For example:

-> git show <commit_hash>

Replace <commit_hash> with the actual commit hash you obtained from git log.

For example:

-> git show abc1234567890123456789012345678901234567

This command will display detailed information about the specified commit, i
ncluding the commit message, author, date, and the changes made to each file in the
commit. Git will show the content added, removed, or modified in each file, along with
line-by-line differences.

If you want to view the changes for the most recent commit, you can simply use:

-> git show HEAD


HEAD is a reference to the latest commit in the current branch.
16. What is branching in Git and why is it used?
ranching in Git refers to the practice of creating separate lines of development that diverge from
the main line (often called the "master" branch) to work on new features, bug fixes, or
experiments without affecting the main codebase. Each branch represents an independent line of
development, allowing developers to isolate changes and collaborate on different tasks
simultaneously.
branching is used:
1.Isolation of Changes: Branching allows developers to work on new features or bug fixes
in isolation from each other and from the main codebase.
2. Parallel Development: By creating branches, multiple developers can work on
different features or fixes concurrently without stepping on each other's toes.
3. Experimentation and Prototyping: Branches can be used for experimentation and
prototyping new ideas without affecting the stability of the main codebase.
4. Feature Development: Branches are commonly used to develop new features in a
project. Developers can create feature branches to implement and test new functionality
independently, making it easier to track progress and collaborate on feature
development.
5. Bug Fixing and Maintenance: Branches are also used for bug fixing and
maintenance tasks. Developers can create bug fix branches to address specific issues or
apply patches to older versions of the codebase while keeping the main branch stable and
unaffected.
6. Code Review and Collaboration: Branches facilitate code review and
collaboration by providing a controlled environment for reviewing and discussing changes
before they are merged into the main branch. Pull requests or merge requests are
typically used to propose and review changes between branches.

17. How do you create a new branch in Git?

Creating a new branch in Git is a straightforward process. You can create a new branch based
on the current state of your repository or from a specific commit. Here's how to create a new
branch:

Create a Branch Based on the Current State:

-> git branch <branch_name>

Replace <branch_name> with the name you want to give to your new branch.
For example, to create a new branch named "feature/new-feature":

-> git branch feature/new-feature

18. How do you switch between branches in Git?

Switch to the New Branch:

After creating the branch, you may want to switch to it to start working on it
immediately. You can use the git checkout command or its shorthand git switch if you
have Git version 2.23 or later:

-> git checkout <branch_name>


or
-> git switch <branch_name>

For example:

-> git checkout feature/new-feature


or
-> git switch feature/new-feature

This command switches your working directory to the new branch, so any changes you make
will be committed to this branch.

19. What is the difference between git merge and git rebase?
 Merge: Use merge for integrating feature branches into the main branch or for
incorporating changes from multiple contributors into a shared branch.
 Rebase: Use rebase for keeping a clean and linear commit history, simplifying the review
process, or when working on long-lived feature branches that need to be kept up-to-date
with changes in the main branch.

Git Merge:

1.Merge Commit: When you merge one branch into another using git
merge, Git creates a new merge commit that combines the changes from both branches.
2.Preserves Branch History: Merge commits preserve the history of each branch, showing
where and when the branches diverged and how they were brought back together.
3. Simple Workflow: Git merge is straightforward and suitable for most use cases. It's
the default strategy for combining branches in Git and is recommended for merging
feature branches into the main branch (e.g., master or main).

Git Rebase:

1.Linear History: When you rebase one branch onto another using git rebase, Git
rewrites the commit history of the branch being rebased, moving all of its commits to the
tip of the target branch. This results in a linear sequence of commits, with no merge
commits.
2.Cleaner History: Rebasing creates a cleaner and more linear commit history, as it
eliminates unnecessary merge commits and keeps the commit graph simple.
3. Interactive Rebase: Git rebase offers an interactive mode (git rebase -i) that
allows you to modify, reorder, squash, or edit commits before applying them to the target
branch. This gives you more control over the commit history and enables you to clean up
or organize your commits as needed.

Key diffrences:

 Commit History: Merge preserves the existing commit history and creates a new merge
commit, while rebase rewrites the commit history and creates a linear sequence of
commits.
 Merge Commits: Merge creates merge commits, while rebase does not.
 Complexity: Merge is simpler and easier to understand, especially for teams or projects
with multiple contributors. Rebase is more complex and requires careful handling,
particularly when rewriting history or resolving conflicts.

20. How do you resolve merge conflictin Git?

1. Identify the Conflict:

When you attempt to merge two branches using git merge, Git will notify you if
there are any conflicts. You'll see a message like "Automatic merge failed; fix
conflicts and then commit the result." Additionally, Git will mark the conflicted files
with conflict markers (<<<<<<<, =======, and >>>>>>>) to indicate the conflicting
sections.

2. Open the Conflicted Files:


Open the conflicted files in your text editor or IDE. Inside the conflicted files, you'll see
the conflicting sections marked by the conflict markers.
3. Resolve the Conflict:

Manually edit the conflicted sections in the file to resolve the conflict. You'll need to
decide which changes to keep, modify, or discard. Remove the conflict markers and make
the necessary adjustments to reconcile the conflicting changes.
4. Save the Changes:

After resolving the conflict, save the changes to the file.


5. Add the Resolved Files:
Once you've resolved the conflicts in all the conflicted files, use git add to stage the
resolved files for the next commit.
-> git add <resolved_file>

Replace <resolved_file> with the name of the resolved file.


6. Commit the Changes:

After adding the resolved files, commit the changes to finalize the merge resolution.

-> git commit

This will create a new commit with the resolved changes and complete the merge
process.
7. Verify the Resolution:

After committing the changes, use git log or other Git commands to verify that the
merge conflict has been resolved successfully.
8. Continue the Merge:

If there are multiple conflicting files, repeat the above steps for each conflicted file until
all conflicts are resolved. Once all conflicts are resolved, you can continue the merge
process by completing the merge commit.

By following these steps, you can effectively resolve merge conflicts in Git and ensure that
conflicting changes are reconciled in your codebase.

21. What is the purpose of the git stash command?


The git stash command is used to temporarily stash changes in your working directory that
are not yet ready to be committed or discarded. It allows you to store modifications, including
untracked files and staged changes, in a "stash" stack so that you can work on something else
without committing incomplete changes or losing work.

Here's the purpose of the git stash command:

1.Temporary Storage: When you're working on a set of changes but need to switch to a
different task or branch, you can use git stash to store your current modifications in a
stash. This allows you to clean up your working directory and switch to another task
without committing incomplete or experimental changes.
2.Switching Branches: If you need to switch branches but have uncommitted changes in
your working directory that are not yet ready to be committed, you can use git stash
to stash those changes before switching branches. After switching branches, you can
apply the stashed changes to the new branch if needed.
3.Resolving Merge Conflicts: During a merge operation, if you encounter conflicts that
you're not ready to resolve immediately, you can stash your changes to temporarily set
them aside. After resolving the conflicts, you can apply the stashed changes to
incorporate them back into your working directory.
4.Experimenting with Code: If you want to experiment with changes or try out different
approaches without committing them to your repository, you can use git stash to
stash your current modifications. This allows you to work on experimental changes
without affecting the main codebase.
5.Backing Up Work: git stash can also serve as a backup mechanism for your work-
in-progress changes. If you're concerned about losing your local modifications or
accidentally discarding changes, stashing them provides a safety net until you're ready to
commit them.

22. How do you apply stashed changes in Git?

To apply stashed changes in Git, you can use the git stash apply command. This command
applies the most recent stash to your working directory without removing it from the stash stack.
Alternatively, you can specify a specific stash to apply by providing its reference.

Here's how to apply stashed changes:

1.Apply the Most Recent Stash:

To apply the most recent stash to your working directory, use the following
command:
-> git stash apply

This command applies the changes from the topmost stash in the stash stack.

2.Apply a Specific Stash:

If you have multiple stashes and want to apply a specific one, you can specify its
reference using stash@{n}, where n is the index of the stash you want to apply (e.g.,
stash@{0}, stash@{1}, etc.).

-> git stash apply stash@{n}

Replace n with the index of the stash you want to apply. For example, to apply the
second most recent stash, you would use:

-> git stash apply stash@{1}

23. What is the purpose of the Git tag command?

The git tag command in Git is used to create, list, delete, and manage tags in a repository. A tag
is a reference pointer to a specific commit in the repository's history. Tags are often used to mark
important points in history, such as release points (e.g., v1.0, v2.0) or significant milestones.

Here's a breakdown of some common uses of the git tag command:

1.Creating tags: You can create a tag using the git tag command followed by the tag
name and optionally the commit hash you want to tag. If no commit hash is specified, it
defaults to the current HEAD.

-> git tag <tag_name> [<commit_hash>]

2.Listing tags: You can list all the tags in the repository using the git tag command
without any arguments.

-> git tag

3.Deleting tags: You can delete a tag using the -d option followed by the tag
name.
-> git tag -d <tag_name>

4.Pushing tags to remote: Tags are not automatically pushed to remote


repositories when you push changes. You need to explicitly push tags
using the git push command with the --tags option.

-> git push origin --tags

Tags are useful for creating stable reference points in your repository's history, which can be
helpful for tracking releases, marking important commits, and collaborating with others.

24. How to create and push tags to a repository?

Creating and pushing tags to a Git repository involves a few simple steps. Here's a guide:

1.Create a tag: To create a new tag, you use the git tag command followed by the tag
name. Optionally, you can specify the commit hash to tag. If you don't specify a commit
hash, Git will default to the current HEAD. For example:

-> git tag v1.0


or if you want to tag a specific commit:
-> git tag v1.0 <commit_hash>

3.Push tags to remote: By default, the git push command doesn't push tags to the remote
repository. You need to use the --tags option to push all tags to the remote repository.
Here's how you can push tags:

-> git push origin --tags

This command pushes all tags to the remote repository named origin.

25. Explain the concept of remote repositaries in Git?

Remote repositories in Git are essentially versions of your project that are hosted on the internet
or on a network somewhere. These repositories can be accessed and manipulated by team
members or collaborators, enabling collaboration and version control across distributed teams.
Here are some key concepts related to remote repositories:

1.Cloning: When you clone a Git repository from a remote, you're creating a local copy
of that repository on your machine.
2.Pushing: Pushing refers to the process of sending your locally committed changes to
the remote repository.
3.Pulling: Pulling involves fetching changes from the remote repository and merging
them into your local repository.
4.Forking: Forking is a concept commonly used in open-source development. It involves
creating a personal copy of a repository within your own GitHub account (or similar
platform). You can then make changes to this forked repository independently of the
original repository. After making changes, you can submit pull requests to the original
repository to propose your changes be merged.
5.Remote: In Git, a remote is simply a reference to a remote repository. By default, the
main remote repository is often named origin, but you can configure multiple remotes if
needed. Remotes allow you to interact with and push changes to different repositories.
6.Collaboration: Remote repositories facilitate collaboration among team members by
providing a centralized location for storing and sharing project files. Team members can
push their changes to the remote repository, allowing others to review, comment on, and
incorporate those changes into the project.

26. How do you add the remote repository in Git?

To add a remote repository in Git, you use the git remote add command followed by a
name for the remote and the URL of the remote repository. Here's the basic syntax:

---> git remote add <name> <url>

Here's a step-by-step breakdown:

1.Get the URL of the remote repository: You'll need the URL of the remote repository
that you want to add as a remote in your local Git repository. This URL typically points
to a repository hosting service like GitHub, GitLab, or Bitbucket.
2.Add the remote repository: In your local Git repository directory, run the git remote
add command followed by a name for the remote (often origin, but you can choose any
name) and the URL of the remote repository. For example:
-> git remote add origin https://github.com/username/repository.git

Replace https://github.com/username/repository.git with the actual


URL of the remote repository.

3.Verify the addition of the remote: You can verify that the remote repository has been
added by running the git remote -v command, which lists all remotes associated with
your local repository along with their URLs:

-> git remote -v

This command should display the name and URL of the remote
repository you just added.

Once you've added the remote repository, you can push changes to it using git push, pull
changes from it using git pull, or fetch information about it using git fetch.

27. How do you push the changes to a remote repository?

To push changes from your local repository to a remote repository in Git, you typically use
the git push command followed by the name of the remote repository and
the name of the branch you want to push. Here's the basic syntax:

-> git push <remote_name> <branch_name>

Here's a step-by-step guide on how to push changes to a remote repository:

1.Commit your changes: Before pushing changes to the remote repository, make sure
you've committed your changes locally using the git commit command. For example:

-> git commit -am "Commit message"

2.Fetch and merge any changes from the remote repository (optional): It's a good
practice to fetch and merge any changes from the remote repository before pushing your
changes. This helps ensure that you're not overwriting any changes made by others. You
can do this using:
-> git fetch <remote_name>
git merge <remote_name>/<branch_name>

3.Push your changes: Once your local changes are committed and you've fetched and
merged any changes from the remote repository, you can push your changes using the git
push command. For example, to push changes from your local main branch to the remote
repository named origin, you would use:

-> git push origin main

Replace origin with the name of your remote repository and main with the name of the
branch you want to push.

4.Enter credentials if prompted: Depending on your Git configuration and the


authentication method used by the remote repository, you may be prompted to enter your
username and password or provide other authentication credentials.
5.Verify the push: After pushing your changes, you can verify that they have been
successfully pushed to the remote repository by visiting the repository's web interface or
by running git log on another clone of the repository.

28. How do you pull the changes to a remote repository?

To pull changes from a remote repository to your local repository in Git, you typically
use the git pull command. This command fetches changes from the remote
repository and merges them into your local branch. Here's the basic syntax:

--> git pull <remote_name> <branch_name>

Here's a step-by-step guide on how to pull changes from a remote repository:

1.Navigate to your local repository: Open a terminal or command prompt and navigate
to the directory of your local Git repository.
2.Pull changes from the remote repository: Run the git pull command followed by the
name of the remote repository and the branch from which you want to pull changes. For
example, to pull changes from the remote repository named origin into your local main
branch, you would use:
-> git pull origin main

Replace origin with the name of your remote repository and main with the name of the
branch you want to pull from.

3.Enter credentials if prompted: Depending on your Git configuration and the


authentication method used by the remote repository, you may be prompted to enter your
username and password or provide other authentication credentials.
4.Resolve any merge conflicts (if applicable): If there are conflicts between the changes
you're pulling and the changes you've made locally, Git will pause and ask you to resolve
them. You'll need to manually resolve these conflicts by editing the affected files and
then commit the changes.
5.Verify the pull: After pulling changes from the remote repository, you can verify that
they have been successfully merged into your local branch by running git log or
inspecting the files in your working directory.

29. What is the purpose of the git fetch command?

The git fetch command is used to retrieve changes from a remote repository without merging
them into your local branch. Instead, it updates your remote tracking branches to reflect the
changes that have occurred in the remote repository since your last fetch or clone. This allows
you to see what's changed in the remote repository without altering your local working directory
or making any changes to your local branches.

Here's a breakdown of the purpose of the git fetch command:

1.Retrieve changes from the remote: When you run git fetch, Git contacts the remote
repository specified and fetches any new branches, commits, or other objects that have
been added to the remote repository since your last fetch or clone.
2.Update remote tracking branches: After fetching changes from the remote repository,
Git updates your local remote tracking branches (e.g., origin/main, origin/feature) to
reflect the latest state of the remote branches. These remote tracking branches act as
references to the state of the branches in the remote repository.
3.Review changes before merging: By fetching changes without automatically merging
them into your local branches, git fetch allows you to review the changes that have
occurred in the remote repository before deciding whether to merge them into your local
branch using git merge or git pull.
4.Synchronize your local repository: git fetch helps you keep your local repository in
sync with the remote repository by fetching any new changes that have been added to the
remote since your last synchronization.
30. How do you delete a branch in Git?

To delete a branch in Git, you can use the git branch -d or git branch -D command.

1.Delete a branch locally:

To delete a branch that has been merged into the current branch, you can use:

-> git branch -d <branch_name>

Replace <branch_name> with the name of the branch you want to delete.

If the branch has unmerged changes (i.e., changes that have not been incorporated
into another branch), Git will refuse to delete it with the -d option. In this case, you can
force delete the branch using the -D option:

-> git branch -D <branch_name>

This will delete the branch regardless of whether it has been merged or not.

2.Delete a branch on the remote repository:

To delete a branch on the remote repository, you use the git push
command with the --delete or -d option:

-> git push <remote_name> --delete <branch_name>

Replace <remote_name> with the name of the remote repository, and


<branch_name> with the name of the branch you want to delete.

For example, to delete a branch named feature on the remote named origin, you would use:

bash
Copy code
git push origin --delete feature

After executing this command, the branch will be deleted from the remote repository.
Git Exercises

31.Create a new Git repository and configure your username and email.

32.Create a file, add some content to it, and commit the changes.
33.Create a .gitignore file and add rules to ignore specific files and directories

git add .gitignore


git commit -m "Add .gitignore file"

34.Clone an existing repository from GitHub and make some changes.

git clone https://github.com/manikanta/repository.git


cd repository
git add .
git commit -m "first commit"
git push origin main

35. Create a new branch, make some changes, and switch back to the main branch

git stash
git checkout -b newBranch
git switch main

36. Merge changes from a feature branch into the main branch.

git checkout main

git merge mani

git commit -m "first commit"

git push origin main

37. Resolve a merge conflict between two branches.

git add manikanta

git merge --continue

git commit -m "Merge conflict resolved"

git push origin main

38.Use git stash to save your work and then apply the stashed changes.

git stash save "Your stash message here"

git stash apply

39.Create a tag for a specific commit and push the tag to the remote repository.

git tag v1.0


git push origin v1.0

40.Add a remote repository, push your local changes, and pull changes from the remote

git remote add origin [email protected]:manikanta290/food-munch.git

git push origin foodmunch


git pull origin Foofdmunch

41. Create a Git alias for a commonly used command.

git config --global –edit


co = checkout
git config --global --get alias.co

42.View the commit history with a graph representation.

git log
git log -n

43.Revert a commit and understand the differences between git revert and git reset.

Git log
Git revert <commit id>

Git reset HEAD~1

44.Squash multiple commits into a single commit using git rebase.

git rebase -i HEAD~3


git push origin mani –force

45.Use git bisect to find the commit that introduced a bug.

git bisect start


git bisect bad HEAD
git bisect good abcdef1
git bisect bad #if the bug exit.
git bisect good # if the bug not exit.

46.Set up a Git hook to run a script before or after certain Git events.
47.Use git cherry-pick to apply a commit from one branch to another.

git checkout manikanta


git cherry-pick “first commit”
git cherry-pick – continu
git push origin manikanta

48.Learn how to use git blame to find out who made specific changes to a file.
Git init
Git clone <link>
Git blame mani
git blame CONTRIBUTING.md

49.Configure and use a Git GUI client.

50.Create a fork of a repository on GitHub, make changes, and open a pull request.

git clone https://github.com/pallavisharma26/manikanta.git


git branch mani
git checkout master
git add .
git commit -m “first commit”
git config –global core.editor “nano”
git status
git push -u origin mani
git remote -v
git remote add <link>
git romote -v
git fetch -u
git checkout master
git merg master

You might also like