Version Control - Working with GIT
Version Control - Working with GIT
With its robust set of features, Git has become the go-to tool for
developers, researchers, and professionals in various industries worldwide.
With Git, you can quickly restore your code to any previous snapshot and
manage multiple branches, allowing for flexible development workflows.
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.
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
Linux
Once git is installed, you can you can check the version by running the
command git --version:
$ git --version
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.
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
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
GitHub
GitLab
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:
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.
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:
Once git is installed, you can you can check the version by running the
command git --version:
$ git --version
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.
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.
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.
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:
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.
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 .
The period refers to the current directory, so this command stages all
changes in the current directory and any subdirectories.
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.
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.
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.
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.
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.
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:
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 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.
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.
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)
4. Add and commit the empty file, with the message "add empty recipe file"
MARGHERITA PIZZA
Ingredients:
- Dough
- Tomato sauce
- Mozzarella
- Basil
- Olive oil
8. Add and commit the changes, with the commit message "add
margherita pizza recipe"
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`
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)
What is merging?
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.
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:
Task - Merging
Completion requirements
Part 1: Fast Forward Merge
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. 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:
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.
1.To push an existing local Git repository to the external repo, use the
command:
git push
If you've just created a new branch, use the following command to push it
to the 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:
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: