Git Manual
Git Manual
PROJECT MANAGEMENT
WITH GIT
(BCS358C) ,
III Sem
LAB MANUAL
(2023-24)
Prepared by
Mr. RANJAN V
Assistant Professor,
Dept. of AIML,
JNNCE Shivamogga
2
3rd Sem, AIML, Project Management with GitLab MANUAL
Course Objectives:
Course Outcomes:
At the end of the course the student will be able to:
● Use the basics commands related to git repository
● Create and manage the branches
● Apply commands related to Collaboration and Remote Repositories
● Use the commands related to Git Tags, Releases and advanced git operations
● Analyse and change the git history
Textbooks:
● Version Control with Git, 3rd Edition, by Prem Kumar Ponuthorai, Jon Loeliger Released
October 2022, Publisher(s): O'Reilly Media, Inc.
● Pro Git book, written by Scott Chacon and Ben Straub and published by Apress,
https://gitscm. com/book/en/v2
●https://infyspringboard.onwingspan.com/web/en/app/toc/lex_auth_0130944433473699842782
_shared/overview
●https://infyspringboard.onwingspan.com/web/en/app/toc/lex_auth_0133013471217745921192
6_shared/overview
3
3rd Sem, AIML, Project Management with GitLab MANUAL
GIT BASICS
What is Git?
Git is a distributed version control system (VCS) that is widely used for tracking changes in source
code during software development. It was created by Linus Torvalds in 2005 and has since become the
de facto standard for version control in the software development industry.
Git allows multiple developers to collaborate on a project by providing a history of changes,
facilitating the tracking of who made what changes and when. Here are some key concepts and features
of Git:
1. Repository (Repo): A Git repository is a directory or storage location where your project's files
and version history are stored. There can be a local repository on your computer and remote
repositories on servers.
2. Commits: In Git, a commit is a snapshot of your project at a particular point in time. Each commit
includes a unique identifier, a message describing the changes, and a reference to the previous
commit.
3. Branches: Branches in Git allow you to work on different features or parts of your project
simultaneously without affecting the main development line (usually called the "master" branch).
Branches make it easy to experiment, develop new features, and merge changes back into the
main branch when they are ready.
4. Pull Requests (PRs): In Git-based collaboration workflows, such as GitHub or GitLab, pull
requests are a way for developers to propose changes and have them reviewed by their peers. This
is a common practice for open-source and team-based projects.
5. Merging: Merging involves combining changes from one branch (or multiple branches) into
another. When a branch's changes are ready to be incorporated into the main branch, you can
merge them.
6. Remote Repositories: Remote repositories are copies of your project stored on a different server.
Developers can collaborate by pushing their changes to a remote repository and pulling changes
from it. Common remote repository hosting services include GitHub, GitLab, and Bitbucket.
7. Cloning: Cloning is the process of creating a copy of a remote repository on your local machine.
This allows you to work on the project and make changes locally.
8. Forking: Forking is a way to create your copy of a repository, typically on a hosting platform like
GitHub. You can make changes to your fork without affecting the original project and later create
pull requests to contribute your changes back to the original repository.
A Version Control System (VCS), also commonly referred to as a Source Code Management (SCM)
system, is a software tool or system that helps manage and track changes to files and directories over
time. The primary purpose of a VCS is to keep a historical record of all changes made to a set of files,
allowing multiple people to collaborate on a project while maintaining the integrity of the codebase.
Centralized Version Control Systems (CVCS): In a CVCS, there is a single central repository that
stores all the project files and their version history. Developers check out files from this central
repository, make changes, and then commit those changes back to the central repository. Examples of
CVCS include CVS (Concurrent Versions System) and Subversion (SVN).
4
3rd Sem, AIML, Project Management with GitLab MANUAL
Distributed Version Control Systems (DVCS): In a DVCS, every developer has a complete copy of
the project's repository, including its full history, on their local machine. This allows developers to
work independently, create branches for experimentation, and synchronize their changes with remote
repositories. Git is the most well-known and widely used DVCS, but other DVCS options include
Mercurial and Bazaar.
Git Installation
To install Git on your computer, you can follow the steps for your specific operating system:
1. Installing Git on Windows:
a. Using Git for Windows (Git Bash):
• Go to the official Git for Windows website: https://gitforwindows.org/
• Download the latest version of Git for Windows.
• Run the installer and follow the installation steps. You can choose the default settings for most
options.
https://git-scm.com/download/win
After installation, you can open a terminal or command prompt and verify that Git is correctly
installed by running the following command:
$ git --version
If Git is installed successfully, you will see the Git version displayed in the terminal. You can now start
using Git for version control and collaborate on software development projects.
Git is a popular version control system used for tracking changes in software development projects.
Here's a list of common Git commands along with brief explanations:
5. git status: Shows the status of your working directory and the files that have been modified
or staged.
6. git log: Displays a log of all previous commits, including commit hashes, authors, dates, and
commit messages.
7. git diff: Shows the differences between the working directory and the last committed
version.
8. git branch: Lists all branches in the repository and highlights the currently checked-out branch.
9. git branch <branchname>: Creates a new branch with the specified name.
10. git checkout <branchname>: Switches to a different branch.
11. git merge <branchname>: Merges changes from the specified branch into the currently checked-
out branch.
12. git pull: Fetches changes from a remote repository and merges them into the current branch.
13. git push: Pushes your local commits to a remote repository.
14. git remote: Lists the remote repositories that your local repository is connected to.
15. git fetch: Retrieves changes from a remote repository without merging them.
16. git reset <file>: Unstages a file that was previously staged for commit.
17. git reset --hard <commit>: Resets the branch to a specific commit, discarding all changes
after that commit.
18. git stash: Temporarily saves your changes to a "stash" so you can switch branches without
committing or losing your work.
19. git tag: Lists and manages tags (usually used for marking specific points in history, like releases).
20. git blame <file>: Shows who made each change to a file and when.
21. git rm <file>: Removes a file from both your working directory and the Git repository.
22. git mv <oldfile> <newfile>: Renames a file and stages the change.
These are some of the most common Git commands, but Git offers a wide range of features and options
for more advanced usage. You can use git --help followed by the command name to get more
information about any specific command, e.g., git help commit.
6
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiments On
Experiment 1.
Initialize a new Git repository in a directory. Create a new file and add it to the staging
area and commit the changes with an appropriate commit message.
Solution:
To initialize a new Git repository in a directory, create a new file, add it to the staging area,
and commit the changes with an appropriate commit message, follow these steps:
1. Open your terminal and navigate to the directory where you want to create the Git
repository.
2. Initialize a new Git repository in that directory:
$ git init
1. Create a new file in the directory. For example, let's create a file named "my_file.txt."
You can use any text editor or command-line tools to create the file.
2. Add the newly created file to the staging area. Replace "my_file.txt" with the actual name
of your file:
example:
After these steps, your changes will be committed to the Git repository with the provided
commit message. You now have a version of the repository with the new file and its history
stored in Git.
7
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 2.
Create a new branch named "feature-branch." Switch to the "master" branch. Merge
the "feature-branch" into "master."
Solution:
To create a new branch named "feature-branch," switch to the "master" branch, and merge the
"feature-branch" into "master" in Git, follow these steps:
This command will create a new branch called "feature-branch" and switch to it.
1. Make your changes in the "feature-branch" by adding, modifying, or deleting files as
needed.
2. Stage and commit your changes in the "feature-branch":
$ git add .
$ git commit -m "Your commit message for feature-branch"
Replace "Your commit message for feature-branch" with a descriptive commit message for the
changes you made in the "feature-branch."
1. Switch back to the "master" branch:
This command will incorporate the changes from the "feature-branch" into the "master"
branch.
Now, your changes from the "feature-branch" have been merged into the "master" branch.
Your project's history will reflect the changes made in both branches
8
3rd Sem, AIML, Project Management with GitLab MANUAL
9
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 3.
Write the commands to stash your changes, switch branches, and then apply the stashed
changes.
Solution:
To stash your changes, switch branches, and then apply the stashed changes in Git, you can
use the following commands:
This command will save your changes in a stash, which acts like a temporary storage for
changes that are not ready to be committed.
1. Switch to the desired branch:
Replace "target-branch" with the name of the branch you want to switch to.
To reapply the stashed changes, switch back to your original branch and execute:
If you want to remove the stash after applying it, you can use git stash pop instead of git stash
apply.
Remember to replace "Your stash message" and "target-branch" with the actual message you
want for your stash and the name of the branch you want to switch to.
Solution
11
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 4.
https://www.youtube.com/watch?v=QUtk-Uuq9nE
a full example:
You can now work with the cloned repository on your local machine, make changes, and
push those changes back to the remote repository as needed.
12
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 5.
Git Tag
Git tags are used to label certain commit objects which are major milestones in project
development phase like deployment of project in various environment, release codes, bug
fixes etc
Lightweight tag
Acts as a pointer to a particular commit.
Experiment 6
Experiment 7
1. Go to your local repository where you have been playing with git so far
2. Execute
1. git remote add origin <repository-link>
2. git push –f origin master
3. As so as you execute push command you will be prompted to login on a separate
window , do that and you can see that you changes are pushed to the remote
repository
15
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 8.
Analyzing and Changing Git History
Write the command to list all commits made by the author "JohnDoe" between "2023-01-01"
and "2023-12-31."
Solution:
To list all commits made by the author "JohnDoe" between "2023-01-01" and "2023-12-31" in Git,
you can use the git log command with the --author and --since and --until options. Here's the
command:
This command will display a list of commits made by the author "JohnDoe" that fall within the
specified date range, from January 1, 2023, to December 31, 2023. Make sure to adjust the author
name and date range as needed for your specific use case.
Solution:
$ git log --author="RANJAN V" --since="2024-01-06" --until="2024-02-06"
16
3rd Sem, AIML, Project Management with GitLab MANUAL
Experiment 9.
Analysing and Changing Git History:
Given a commit ID, how would you use Git to view the details of that specific commit, including the author,
date, and commit message?
Solution:
To view the details of a specific commit, including the author, date, and commit message, you can
use the git show or git log command with the commit ID. Here are both options:
1. Using git show:
In bash
git show <commit-ID>
Replace <commit-ID> with the actual commit ID you want to view. This command will display
detailed information about the specified commit, including the commit message, author, date, and the
changes introduced by that commit.
For example:
$ git show abc123
Experiment 10.
Write the command to undo the changes introduced by the commit with the ID "abc123".
Solution:
To undo the changes introduced by a specific commit with the ID "abc123" in Git, you can use the
git revert command. The git revert command creates a new commit that undoes the changes made by
the specified commit, effectively "reverting" the commit. Here's the command:
Replace "abc123" with the actual commit ID that you want to revert. After running this command,
Git will create a new commit that negates the changes introduced by the specified commit. This is a
safe way to undo changes in Git because it preserves the commit history and creates a new commit to
record the reversal of the changes.
Once you give the command revert it will ask you for the type the message in new window type the
message for reverting and after the message
Type ctrl C
And Type :qa!
ENTER
Experiment 11.
Collaboration and Remote Repositories:
Fetch the latest changes from a remote repository and rebase your local branch onto the
updated remote branch.
Solution:
To fetch the latest changes from a remote repository and rebase your local branch onto the updated
remote branch in Git, follow these steps:
1. Open your terminal or command prompt.
2. Make sure you are in the local branch that you want to rebase. You can switch to the branch
using the following command, replacing <branch-name> with your actual branch name:
Replace <branch-name> with the name of the remote branch you want to rebase onto. This command
will reapply your local commits on top of the latest changes from the remote branch, effectively
incorporating the remote changes into your branch history.
Resolve any conflicts that may arise during the rebase process. Git will stop and notify you if there
are conflicts that need to be resolved. Use a text editor to edit the conflicting files, save the changes,
and then continue the rebase with:
Experiment 12.
Write the command to display the last five commits in the repository's history.
Solution:
To display the last five commits in a Git repository's history, you can use the git log command with
the -n option, which limits the number of displayed commits. Here's the command:
$ git log -n 5
This command will show the last five commits in the repository's history. You can adjust the number
after -n to display a different number of commits if needed.