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

Version Control - Working with GIT

Git is a distributed version control system that facilitates collaboration and version management in software development, allowing developers to work independently and merge changes efficiently. It offers features such as change tracking, version rollback, and integration, making it popular among developers, researchers, and various professionals. The document also provides installation instructions for different operating systems, guides on generating SSH keys, and basic Git commands for managing repositories.

Uploaded by

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

Version Control - Working with GIT

Git is a distributed version control system that facilitates collaboration and version management in software development, allowing developers to work independently and merge changes efficiently. It offers features such as change tracking, version rollback, and integration, making it popular among developers, researchers, and various professionals. The document also provides installation instructions for different operating systems, guides on generating SSH keys, and basic Git commands for managing repositories.

Uploaded by

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

Welcome to the world of Git, a powerful and widely-used distributed

version control system designed to streamline collaboration and version


management in software development projects.

With its robust set of features, Git has become the go-to tool for
developers, researchers, and professionals in various industries worldwide.

Distributed Version Control with Git

Git is a distributed version control system, which means that each


developer's local repository stores a complete history of the project. This
distributed architecture allows for efficient collaboration, as developers
can work independently and merge their changes into a shared repository
when needed.

With Git, you can quickly restore your code to any previous snapshot and
manage multiple branches, allowing for flexible development workflows.

Key Features and Benefits of Git

Git offers a comprehensive set of features to facilitate efficient project


management and collaboration:

 Change tracking: Git allows you to track changes across multiple


files, making it easy to monitor the evolution of your codebase.
 Version comparison: Git enables you to compare different versions
of your project, helping you identify changes and improvements over
time.
 Version rollback: If needed, Git allows you to revert your project to a
previous version, providing a safety net in case of errors or
unforeseen issues.
 Change integration: Git streamlines the process of combining
changes made by different developers, ensuring that your codebase
remains cohesive and up-to-date.
 Collaboration and sharing: With Git, you can easily share your
changes with others and collaborate on projects, fostering a
productive and efficient development environment.
 Distributed architecture: Git's distributed nature ensures that each
developer has a complete copy of the repository, allowing for greater
redundancy and reducing the reliance on a central server.
Who Uses Git?

Git's versatility extends beyond the realm of software development, and its
user base encompasses a diverse range of professionals:

 Developers: Git is the go-to tool for developers at tech giants, start-
ups, and everything in between.
 Tech Industry Professionals: Non-developer roles in the tech
industry often require a basic understanding of Git to facilitate
collaboration with developer colleagues.
 Researchers: Research teams across the globe use Git to manage
code, datasets, and other research-related materials, leveraging the
power of Git+GitHub for peer-reviewed scientific endeavours.
 Governments: Various government entities, including the US
Government and Washington D.C.'s city council, use Git to draft and
manage legislation, promoting transparency and citizen
involvement.
 Creative Applications: Git's flexibility has inspired countless
innovative use cases, from personal diaries and thesis writing to
version control for creative projects like music composition and
graphic design.

Git, GitHub, and GitLab?

It's essential to distinguish among Git, GitHub, and GitLab, as each serves a
different purpose within the version control ecosystem.

Git is the local version control software that runs on your machine, allowing
you to work without an account or an active internet connection.

GitHub and GitLab, on the other hand, are online platforms that host Git
repositories and facilitate collaboration. While not required for using Git,
these services enhance Git's collaborative capabilities by providing an
accessible, centralized platform for sharing and managing project files.

Installing Git:

Windows
1. Download the Git installer from
here: https://gitforwindows.org/index.html
2. Run the installer with the default settings

Mac

1. Open the Terminal on your Mac


2. Install Homebrew by entering the following command in the Terminal
window:
/bin/bash -c "$(curl -
fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/i
nstall.sh)"
3. Once Homebrew is installed, you can use it to install Git by entering
the following command in the Terminal window:
brew install git

Linux

Debian-based Linux (e.g. Ubuntu):

1. Open the terminal on your Linux machine.


2. Enter the following command to update the package list:
sudo apt-get update
3. Once the update is complete, enter the following command to install
Git:
sudo apt-get install git
4. Wait for the installation to complete. Once it's finished, you can start
using Git by entering Git commands in the terminal.

Red Hat-based Linux (e.g. CentOS):

1. Open the terminal on your Linux machine.


2. Enter the following command to update the package list:
sudo yum update
3. Once the update is complete, enter the following command to install
Git:
sudo yum install git

Once git is installed, you can you can check the version by running the
command git --version:

$ git --version

git version 2.40.0


Generating keys:

To authenticate secure connections with SSH, you'll need to generate a pair


of keys and add them to your Git hosting service, such as GitHub or GitLab.
This section provides an overview of the steps to follow for Windows and
Mac/Linux users.

How to use the terminal:

The terminal is a command-line interface that allows you to interact with


your computer's operating system. You'll use the terminal to run Git
commands.

 Windows: Use Git Bash. To open it, open File Explorer and navigate to
your home directory (C:\Users\your-user). Right-click and select
"Open Git Bash here".
 Mac/Linux: Use any terminal.

Verify existing keys:

SSH keys come in pairs: a public key and a private key. Before generating
new SSH keys, check if you already have a pair on your computer. Run this
command:

$ ls –al ~/.ssh
If a pair of files exists (one with no extension and another with the .pub
extension), you already have a key. In this case, do not create a new one.

File structure:
 Public Keys: These files end with the .pub extension and can be
shared freely. They are used by others to verify your identity. Think of
it as a digital signature that verifies your identity when you make a
connection. When someone sees it, they can confirm that you are
who you say you are.
 Private Keys: These files have no extension (e.g., id_rsa). They should
never be copied or shared. If the public key is your digital signature,
the private key would be the unique fingerprint that only you possess.
Sharing your private key would be equivalent to giving away the
password to your bank account.

Windows

1. Open File Explorer and navigate to your home directory


(C:\Users\your-user).
2. Right-click anywhere in the File Explorer window and select "Git Bash
Here" from the context menu.
3. In the Git Bash window, enter the following command to generate
your SSH key:
ssh-keygen -t rsa
4. You will be prompted to enter a file name for the key. You can accept
the default file name (id_rsa) or enter a different name.
5. You will also be prompted to enter a passphrase for the key. You can
enter a passphrase or leave it blank if you don't want to use one.
6. Once the key is generated, you can view the public key with the
following command in Git Bash:
cat ~/.ssh/id_rsa.pub
7. Copy the entire output from the command, including the "ssh-rsa" at
the beginning.
8. Now you can add the public key to your GitHub or other Git hosting
service account.

Mac

1. Open Terminal. You can find it in the Applications > Utilities folder or
search for it using Spotlight.
2. In the Terminal window, enter the following command to generate
your SSH key:
ssh-keygen -t rsa
3. You will be prompted to enter a file name for the key. You can accept
the default file name (id_rsa) or enter a different name.
4. You will also be prompted to enter a passphrase for the key. You can
enter a passphrase or leave it blank if you don't want to use one.
5. Once the key is generated, you can view the public key with the
following command in Terminal:
cat ~/.ssh/id_rsa.pub
6. Copy the entire output from the command, including the "ssh-rsa" at
the beginning.
7. Now you can add the public key to your GitHub or other Git hosting
service account

Adding the Public Key to GitHub/GitLab:

GitHub

1. Go to your GitHub account settings.


2. Click on "SSH and GPG keys" in the left sidebar.
3. Click the "New SSH key" button.
4. In the "Title" field, give the key a descriptive name.
5. In the "Key" field, paste the copied SSH key.
6. Click the "Add SSH key" button.

GitLab

1. Go to your GitLab account settings.


2. Click on "SSH Keys" in the left sidebar.
3. In the "Title" field, give the key a descriptive name.
4. In the "Key" field, paste the copied SSH key.
5. Click the "Add key" button.

Adding key to the SSH agent:

The SSH agent is a program that runs in the background and manages
your SSH keys. Adding your SSH key to the SSH agent streamlines the
authentication process when working with Git and other SSH services while
enhancing security.
1. Check if the ssh-agent is running with the following command:
$ eval “$(ssh -agent -s)”
2. If needed, start the agent manually:
$ ssh-agent –s
3. Add the newly generated key to the agent:
$ ssh-add ~/.ssh/id_rsa

Configuring Git:

To personalize your Git settings and streamline your workflow, set up your
name and email as the default/global user for Git by running these
commands with your details:

$ git config --global user.name “your name”

$ git config --global user.email “[email protected]


Your Git is now configured for ease of use. You won't be prompted for
passwords often, and commits will be correctly attributed to you.

Adding key to the SSH agent:

The SSH agent is a program that runs in the background and manages
your SSH keys. Adding your SSH key to the SSH agent streamlines the
authentication process when working with Git and other SSH services while
enhancing security.

1. Check if the ssh-agent is running with the following command:


$ eval “$(ssh -agent -s)”
2. If needed, start the agent manually:
$ ssh-agent –s
3. Add the newly generated key to the agent:
$ ssh-add ~/.ssh/id_rsa

Configuring Git:

To personalize your Git settings and streamline your workflow, set up your
name and email as the default/global user for Git by running these
commands with your details:

$ git config --global user.name “your name”

$ git config --global user.email “[email protected]


Your Git is now configured for ease of use. You won't be prompted for
passwords often, and commits will be correctly attributed to you.

Once git is installed, you can you can check the version by running the
command git --version:

$ git --version

git version 2.40.0


Generating keys:

To authenticate secure connections with SSH, you'll need to generate a pair


of keys and add them to your Git hosting service, such as GitHub or GitLab.
This section provides an overview of the steps to follow for Windows and
Mac/Linux users.

How to use the terminal:

The terminal is a command-line interface that allows you to interact with


your computer's operating system. You'll use the terminal to run Git
commands.

 Windows: Use Git Bash. To open it, open File Explorer and navigate to
your home directory (C:\Users\your-user). Right-click and select
"Open Git Bash here".
 Mac/Linux: Use any terminal.

Verify existing keys:

SSH keys come in pairs: a public key and a private key. Before generating
new SSH keys, check if you already have a pair on your computer. Run this
command:

$ ls –al ~/.ssh
If a pair of files exists (one with no extension and another with the .pub
extension), you already have a key. In this case, do not create a new one.

File structure:

 Public Keys: These files end with the .pub extension and can be
shared freely. They are used by others to verify your identity. Think of
it as a digital signature that verifies your identity when you make a
connection. When someone sees it, they can confirm that you are
who you say you are.
 Private Keys: These files have no extension (e.g., id_rsa). They should
never be copied or shared. If the public key is your digital signature,
the private key would be the unique fingerprint that only you possess.
Sharing your private key would be equivalent to giving away the
password to your bank account.

Adding key to the SSH agent:

The SSH agent is a program that runs in the background and manages
your SSH keys. Adding your SSH key to the SSH agent streamlines the
authentication process when working with Git and other SSH services while
enhancing security.

1. Check if the ssh-agent is running with the following command:


$ eval “$(ssh -agent -s)”
2. If needed, start the agent manually:
$ ssh-agent –s
3. Add the newly generated key to the agent:
$ ssh-add ~/.ssh/id_rsa

Configuring Git:

To personalize your Git settings and streamline your workflow, set up your
name and email as the default/global user for Git by running these
commands with your details:

$ git config --global user.name “your name”

$ git config --global user.email “[email protected]


Your Git is now configured for ease of use. You won't be prompted for
passwords often, and commits will be correctly attributed to you.

Initializing a repo

To work with Git, you need to create a new repository (repo) or clone an
existing one to track changes to your files.
To create anew local repo, open git bash in the folder where you want to
initialize a new repo and use the following command:

git init
⚠ Note: Make sure not to initialize a repo inside of an existing repo, as Git
tracks all nested subdirectories.

git status

Once you have a repo set up, you can use the git status command to get
information about the current status of your repo. This command will show
you tracked and untracked files and changes, but it will not display any
commit history or information.

The git status command is mostly used to show the state between git
add and git commit. You can check whether the changes and files are
tracked by using this command.

Staging changes with git add

To save changes to a Git repository, you must first stage your modified files
using the git add command. Staging tells Git which changes you want to
include in your next commit.

You can stage individual files with the git add command followed by the file
path. For example, if you want to stage changes to a file named script.js,
you would run the following command:

git add script.js


If you have multiple changed files and want to stage them all at once, you
can use the command

git add .
The period refers to the current directory, so this command stages all
changes in the current directory and any subdirectories.

Commiting changes with git commit

Once your files are staged, you can create a new commit using the git
commit command. A commit is like a snapshot of your repository at a
specific point in time, and it represents a complete set of changes that you
want to save. When you run git commit, you must include a commit
message that describes the changes you made. The message should be a
short summary that explains the purpose of the commit.

The command to commit with a message is:

git commit -m " commit message"


Committing is an important part of the Git workflow and should be done
frequently to ensure that your repository is up to date. You can commit
changes as often as you like, and each commit is saved with a unique ID
that allows you to track and undo changes if necessary.

Also note that committing changes locally is not the same as pushing
those changes to a remote repository. After committing, you can push your
changes to a remote repository (such as GitHub or GitLab) using the git
push command. This allows other collaborators to access your changes
and work on them together.

Commit messages

Commit messages are an important part of using Git. They help you and
your team keep track of changes and understand what was done at each
step of the development process.

It's important to make your commit messages meaningful and descriptive.


The specific structure of your commit messages will most likely be
determined by your team, but in general, each commit should include a
single change, feature, or bugfix. This is known as an "atomic commit".

List of common ‘conventional commit’ messages

 fix: - Correct an error/ bug


 docs: - Documentation changes
 style: - Changes of style in the code (semicolon, indentation, etc.)
 refactor: - A code change that does not fix bugs or add functionality
 perf: - Improving the performance of the code.
 test: - Add test to an existing function.
 chore: - Update something without affecting the user.
 revert: Revert a previous commites
By keeping each commit focused on a single thing, it makes it much easier
to undo or roll back changes later. It also makes the code or project easier
to review for both you and the rest of the team.

Using git log to view version history

After initializing a repository and making a few commits, you might want to
have an overview of the project's commit history. Git log provides a simple
way to view the commit history of your repository.

To view the commit history of your repository, simply run:

git log
This command will display a list of commits in reverse chronological order,
with the most recent commit at the top. For each commit, you'll see the
commit hash, author, date, and commit message.

If you find the default output of git log to be overwhelming or difficult to


read, you can customize the output using various formatting options. For
example, to display the commit history in a more concise, one-line format,
use:

git log --oneline


This command will show a shortened version of the commit hash, followed
by the commit message, making it easier to scan through the commit
history quickly.

Task - Commit basics


Completion requirements

1. Create a new folder called BatmanTasks


2. Initialize a new git repo inside of the BatmanTasks folder (make sure
you're not already inside of a Git repo!)
3. Make a new file called crime_fighting.txt
4. Make another new file called training.txt
5. Make a commit that includes both empty files. The message should
be “create crime fighting and training files”
6. In the crime_fighting.txt file, add the following tasks:
- Patrol Gotham City
- Investigate suspicious activity at Arkham Asylum
7. In the training.txt file, add the following:
- Boxing practice
- Strength training
- Gadget testing
8. Make a new commit, including ONLY the changes from
the training.txt file. The commit message should be “add initial
training tasks”
9. Make a second commit including ONLY the changes to
the crime_fighting.txt file. It should have the commit message “add
initial crime fighting tasks”
10. Next up, add the following line to the end of training.txt
- Stealth training
11. In the crime_fighting.txt file, change the second line so that it says
Investigate and thwart suspicious activity at Arkham Asylum
12. Make a commit that includes the changes to BOTH files. The
message should read “add intel analysis to training and update
Arkham task”
13. Use a Git command to display a list of the commits. (You should see
4)

It should look something like this

Branching
Completion requirements
Branches are a fundamental concept in Git that allow developers to work
on different aspects of a project simultaneously without interfering with
each other's work. By understanding branches and their use, you can
maximize the efficiency of your development process.

What are Branches?


In Git, branches are like alternate timelines for a project. They allow
developers to create separate contexts where they can experiment,
develop new features, or fix bugs without affecting the main codebase.
Essentially, branches make it possible to work on several ideas in parallel,
enhancing collaboration among team members.

The Main/Master Branch

When you initialize a Git repository, it creates a default branch


called main or master. This branch represents the "public face" of your
repository and should be kept clean, stable, and free from breaking
changes. As a best practice, you should not commit code changes directly
to the main branch to maintain its stability.

Creating and Using Branches for Various Tasks

Creating separate branches for distinct tasks, such as new features, bug
fixes, or code refactoring, enables you to work on multiple tasks
simultaneously. This approach helps you manage and organize your work
more effectively while maintaining an uncluttered codebase.

When you want to work on a specific task, you should create a dedicated
branch for it. This ensures that each branch serves a well-defined purpose,
making it easier to track progress and manage changes. To create a new
branch, run the following command:

git branch <name-of-branch>


Using meaningful names for your branches is essential, as it allows
collaborators to quickly grasp the intent behind each branch. For example,
a branch named sean-feature-login clearly indicates that Sean is working
on implementing a login feature.

Switching Between Branches for Seamless Task Management

One of the main benefits of using branches is the ability to switch between
them effortlessly, allowing you to work on different tasks without affecting
other branches. This can be particularly useful when you need to pivot
between tasks or collaborate with other team members on various aspects
of a project.To switch between branches, use the following command:
git switch <branch>
Suppose you're working on a new user registration feature in a branch
called dean-feature-registration and you need to review and provide
feedback on the login feature that Jane is working on. You can switch to
Sean's branch using:

git switch jane-feature-login


After providing feedback or making any necessary changes, you can
switch back to your branch and continue working on the user registration
feature:

git switch dean-feature-registration


Listing All Branches

To view a list of all the branches in your repository, simply run:

git branch
This command will display a list of all branches, with an asterisk (*) next to
the currently active branch. This can be helpful when you need to quickly
check which branch you're currently working on or to verify the existence of
other branches.

Further reading: What is HEAD?

In Git, HEAD is a reference pointing to the last commit in the current


branch. It helps you understand where you currently are in the repository.

New branches are created based on HEAD. You can use git checkout to
check out a specific commit using its commit hash ID if you want to view
the state of your project files at a previous commit. Note that this action will
result in a detached HEAD.

To return to the latest commit in the current branch, use the


usual switch command:

git switch <branch-name>

Task - Branching
Completion requirements
Branching Exercise
1. Make a new folder called `PizzaRecipes`

2. Make a new git repo inside the folder (make sure you're not in an existing
repo)

3. Create a new file called recipes.txt (leave it empty for now)

4. Add and commit the empty file, with the message "add empty recipe file"

5. Immediately make a new branch called `margherita` and another


branch called `ham` (both based on the master branch)

6. Move to the `margherita` branch using the appropriate command to


change branches.

7. In the recipes.txt file, add the following:

MARGHERITA PIZZA

Ingredients:

- Dough

- Tomato sauce

- Mozzarella

- Basil

- Olive oil

8. Add and commit the changes, with the commit message "add
margherita pizza recipe"

9. Move over to the `ham` branch using the appropriate command to


change branches.

10. In the recipes.txt file, add the following text:


HAM PIZZA

Ingredients:

- Dough

- Tomato sauce

- Mozzarella

- Ham slices

11. Add and commit the changes on the `ham` branch with the commit
message "add ham pizza recipe"

12. Next, create a new branch based upon the `ham` branch called
`hawaiian`

13. Move to the `hawaiian` branch

14. Edit the recipes.txt title of the `Ham Pizza' to `Hawaiian Pizza`, and add
pineapple to the ingredients list:

HAWAIIAN PIZZA

Ingredients:

- Dough

- Tomato sauce

- Mozzarella

- Ham slices

- Pineapple chunks
16. Add and commit the change with the message "Modify Ham pizza
recipe to create Hawaiian pizza"

17. Run a git command to list all branches (you should see 4)

Merging branches & resolving


conflicts
Completion requirements
Merging is an essential part of working with Git. In this section, we will
explore the process of merging branches, the different types of merging,
and how to resolve conflicts that may arise during the merge.

What is merging?

Merging is the process of integrating changes from one branch into


another. It allows you to incorporate new features, bug fixes, or
improvements made in different branches into a single, unified branch. The
command used for merging branches in Git is:

git merge <branch-name>


Merging occurs in the branch that the HEAD (latest commit of the active
branch) pointer is currently on i.e., the branch you are working in.

How to Merge Branches

To merge two branches, follow these simple steps:

1. Switch (or checkout) to the target branch:

git switch master


In this step, you change your working directory to the target branch (in this
example, 'master'). This is the branch that you want to merge the changes
into. The git switch command updates your working directory to reflect the
latest commit in the target branch and moves the HEAD pointer to the
target branch.

2. Merge the source branch into the target branch:

git merge <branch-name>


During this step, Git attempts to automatically merge the source branch
(specified by <branch-name>) into the target branch (the one you
switched to in step 1). Git analyzes the commit history of both branches
and tries to apply the changes from the source branch to the target
branch.

If the changes in the source branch don't conflict with the changes in the
target branch, Git performs a "clean" merge. In the case of a fast-forward
merge, Git simply moves the target branch's pointer to the latest commit
in the source branch. If a new commit is needed (e.g., when the branches
have diverged), Git creates a new commit to represent the merge,
preserving the commit history of both branches.

If Git detects conflicts between the changes in the source and target
branches, it will display a message indicating that there are merge
conflicts. In this case, you'll need to manually resolve the conflicts following
the steps outlined in the next section.

Resolving Merge Conflicts

Merge conflicts occur when changes in the branches being merged


conflict with each other. To resolve these conflicts, follow these steps:

1. Identify the conflicted files: Git will display a message indicating


which files have conflicts. Take note of these files, as you'll need to
address the conflicts in each one.
2. Understand the conflict markers: Open the conflicted files in a text
editor. Git uses conflict markers to indicate the conflicting changes
from both branches.
These markers are:
<<<<<<< HEAD: This marker indicates the beginning of the conflicting
changes in the current branch (the branch you are merging into).
=======: This marker separates the conflicting changes in the
current branch from the changes in the branch being merged.
>>>>>>> <branch-name>: This marker indicates the end of the
conflicting changes in the branch being merged.
3. Review the conflicting changes: Between the <<<<<<<
HEAD and ======= markers, you will find the changes from the
current branch. Between the ======= and >>>>>>> <branch-
name> markers, you will find the changes from the branch being
merged.
4. Carefully review the changes from both branches and decide which
version you want to keep, or manually edit the file to create a
combined version that resolves the conflict.
5. Remove the conflict markers: Once you have resolved the conflict in
the file, delete the conflict markers (<<<<<<< HEAD, =======,
and >>>>>>> <branch-name>). Make sure you remove all the
markers, as they will cause errors if left in the code.
6. Save and stage the resolved files: Save the changes you made in the
text editor. Then, stage the resolved files using the following
command:
7. Commit the resolved merge: After staging the resolved files, commit
the merge with an appropriate message:

git commit -m "Resolved merge conflicts"


git add <resolved-file>
Deleting Branches

After a successful merge, you may want to delete the source branch if it is
no longer needed. To do so, use the following command:

git branch -d <branch-name>


This will delete the local branch. If you want to delete a remote branch, use:

git push origin --delete <branch-name>

Task - Merging
Completion requirements
Part 1: Fast Forward Merge

Your goal is to create a fast-forward merge. Follow these steps:

1. Start by creating a new repo. Create a file called jokes.txt and


commit it to master.
2. Create a new branch and switch to it.
3. Add a joke to the jokes.txt file.
Why did the developer go broke?
Because he used up all his cache!
4. Commit the changes in the new branch.
5. Switch back to the master branch and merge the new branch into
master.

If you followed the steps correctly, this should result in a fast-forward


merge.

Part 2: Merge Commit (No Conflicts)

Your goal is to create a merge commit without any merge conflicts. Follow
these steps:

1. In the master branch, add a new joke to the jokes.txt file and commit
the changes. For example:
Why do Java developers wear glasses?
Because they don't C#!
2. Create a new branch and switch to it.
3. Add another joke to the jokes.txt file in the new branch, without
modifying the existing jokes.
How do functions break up?
They stop calling each other!
4. Commit the changes in the new branch.
5. Switch back to the master branch and merge the new branch into
master.

If you followed the steps correctly, this should result in a merge commit
without any conflicts.

Part 3: Conflicts

Your goal is to create a merge conflict and then resolve it. Follow these
steps:

1. Create a new branch and switch to it.


2. Modify one of the existing jokes in the jokes.txt file. For example,
change the punchline of the first joke:
Why did the developer go broke?
Because he kept flushing his cache!
3. Commit the changes in the new branch.
4. Switch back to the master branch.
5. Modify the same joke in the jokes.txt file, altering the punchline
differently.
Why did the developer go broke?
Because he couldn't optimize his cache flow!
6. Commit the changes in the master branch.
7. Attempt to merge the new branch into master.
If you followed the steps correctly, this should result in a merge
conflict.
8. Resolve the conflict by choosing one of the punchlines or creating a
new one.
9. Stage the resolved file and commit the merge.

Working with External Repositories -


GitLab/GitHub
Completion requirements
External repositories like GitLab and GitHub provide a centralized location
for hosting your Git repositories, making collaboration, version control, and
project management easier and more efficient.

Creating an External Repository:

1. Sign up for an account on gitlab.com or github.com, depending on


your preference.
2. Log in to your account and navigate to the main dashboard.
3. Click on 'New project' or 'New repository' to create a new, blank
repository.

Connecting to an External Repository:

1. Open your preferred terminal, and navigate to the local Git repository
you want to link to the external repository.
2. To add the remote repository, execute the following command:

git remote add origin <repository_URL>


Note: If you cloned the repository, the remote is added automatically.
Cloning an External Repository:

Cloning an external repository allows you to create a local copy of the


entire project, including its history, on your machine. This enables you to
work on the project locally and push any changes back to the remote
repository when needed.

To clone an existing repository, you can use either SSH or HTTPS:

 Cloning with SSH: If you have set up an SSH key, you can clone a
repository using the following command,
replacing name and repo.git with the appropriate details:
git clone https://gitlab.com/username/repo-name
Cloning with SSH provides a secure and convenient way to access
the remote repository without needing to enter your username and
password each time you interact with the repo.

 Cloning with HTTPS: If you don't have an SSH key set up or prefer
using HTTPS, you can clone a repository using the following
command, replacing username and repo-name with the
appropriate details:
git clone [email protected]:name/repo.git
Cloning with HTTPS is more accessible and doesn't require any
additional setup. However, you'll need to enter your username and
password each time you interact with the remote repository.

After cloning a repository, you'll have a complete local copy of the project,
including its commit history. You can then navigate to the cloned directory,
make changes, commit them, and push the updates back to the remote
repository.

Pushing Changes to an External Repository:

1.To push an existing local Git repository to the external repo, use the
command:

git push -u origin --all


2. To commit code to the external repo, simply use:

git push
If you've just created a new branch, use the following command to push it
to the external repository:

git push -u origin <branch_name>


Fetching Changes from an External Repository:

1. To retrieve the latest code changes from the external repository, execute
the following command:

git pull
To fetch changes from a specific branch in the external repository, first,
ensure you have the remote repository added. Then, use the following
command, replacing <branch_name> with the name of the branch you
want to fetch changes from:

git fetch origin <branch_name>


This command retrieves the branch's latest changes without merging
them into your local branch.

After fetching the changes, you can either create a new local branch
based on the fetched branch or switch to your existing local branch and
merge the changes. To create a new local branch and switch to it, use the
following command:

git switch -c <local_branch_name> origin/<branch_name>


If you already have a local branch and want to merge the fetched
changes, switch to that branch and execute the following command:

git merge origin/<branch_name>


By using these commands, you can fetch changes from a specific branch
in an external repository, giving you more control over the updates you
want to incorporate into your local project.

You might also like