Version Control With Git & Github
Version Control With Git & Github
➢ Git Basics
➢ Configuring Git
➢ Git Workflow
➢ Git Commands
➢ Gitingore
➢ Types of Version Control
➢ Staging vs. Production
➢ Types of Merge
➢ Pull Requests
➢ Merge Conflicts
GIT BASICS
Git :
A distributed version control system (VCS) created by Linus Torvalds in 2005. Git
helps track changes in source code during software development, allowing multiple
people to collaborate on a project. Each collaborator has a copy of the repository,
which they can update, modify, and merge with the central repository as needed.
GitHub :
A web-based platform built around Git, providing a graphical interface to manage
code repositories. It includes features like bug tracking, task management, and wikis,
enabling developers to share and collaborate on projects more easily. GitHub hosts
repositories and allows version control with Git functionalities.
Repository (Repo) :
A repository is a directory or storage space where your project files and their
complete history are stored. There are two types of repositories:
1. Local repository: The repository that exists on your local machine.
2. Remote repository: The version of the repository stored on a remote server
(e.g., GitHub).
Commit :
A commit is a snapshot of your repository at a specific point in time. Commits
represent the changes made to the repository, and each commit includes a unique
ID (SHA-1 hash) and metadata like the author, date, and commit message.
Branch :
A branch in Git represents a separate line of development. By default, Git has a
main branch (formerly master). Developers create new branches for working on
different features, bug fixes, etc., without affecting the main codebase.
Merge :
Merging is the process of integrating changes from one branch into another. For
example, when a feature branch is complete, it is merged into the main branch.
Staging Area :
The staging area (or index) is an intermediate area where changes are stored before
they are committed. It allows you to prepare and review changes before finalizing
them.
Remote :
A remote is a version of the repository that is hosted on the internet or another
network. Remote repositories like GitHub or GitLab are used for collaboration and
sharing changes with others.
Working Directory :
This is the directory on your local machine where your project files are stored and
where you work. The working directory reflects the current state of the repository.
Fork :
A fork in Git is a personal copy of someone else's repository. It allows developers
to freely experiment with changes in their own copy without affecting the original
project. Forks are typically used in open-source projects, where contributors fork
the original repository, make changes, and then propose those changes back to the
original project via pull requests. This allows for independent development while
maintaining the ability to sync with and contribute to the upstream project.
Merge Conflicts :
A merge conflict occurs when Git can't automatically merge changes due to
conflicting changes in the same file(s) on different branches. This typically
happens when two people edit the same part of a file or when changes on two
branches diverge significantly.
To configure Git, set up user information (name and email) and verify that Git is
correctly installed. Here are the steps and commands for configuring Git:
1. Install Git
• If Git is not installed, download and install it from git-scm.com.
• Verify the installation by checking the Git version.
• Command : git –version
Last stage is the committed stage . After git push its available and the files can be
fetched from the remote repository by git pull or git fetch.
1. Purpose of .gitignore
The main purpose of the .gitignore file is to ensure that certain files and directories
are not tracked by Git. This is particularly useful for:
• Temporary files: Files that are generated during development but are not part
of the source code (e.g., log files).
• Build artifacts: Compiled or built code and files generated by build tools.
• Sensitive information: Files like API keys, configuration files, or credentials
that should not be shared or stored in version control.
General Syntax:
• Use * as a wildcard to match multiple characters.
• Use / to specify directories.
• Use ! to negate a pattern and include files that would otherwise be ignored.
Examples:
• *.log ignores all files with a .log extension.
• build/ ignores the entire build directory.
• !important.log includes the file important.log, even if all .log files are ignored.
3. Location of .gitignore
The .gitignore file is typically placed in the root directory of your Git repository.
Also add additional .gitignore files in subdirectories to apply specific rules only to
those directories.
• If a file has already been tracked by Git and you now want to ignore it, you
must first remove it from version control:
git rm --cached <filename>
Important Notes
• Global .gitignore: You can create a global .gitignore file to apply to all your
Git repositories (for files like .DS_Store or .log across all projects):
git config --global core.excludesfile ~/.gitignore_global
Version control is a system that tracks changes to files over time, allowing
developers to:
• Keep track of code modifications.
• Access previous versions of code (revision history).
• Revert to earlier versions when necessary.
• Collaborate with others while minimizing file conflicts.
Advantages:
• Easy access control.
• Simple and easier to learn for new developers.
Disadvantages:
• Requires an active connection to the server for most actions.
• Slower performance due to reliance on a central server.
Advantages:
• Fast and efficient.
• Can work offline.
• Higher performance and flexibility.
Production Environment:
The live environment where the application runs for users. All staging changes
should be thoroughly tested before deployment to production to avoid issues.
1. Merge :
A standard merge will take each commit in the branch being merged and add them
to the history of the base branch based on the timestamp of when they were
created.
It will also create a merge commit, a special type of “empty” commit that indicates
when the merge occurred.
• Example: You’re on main, and you want to merge a feature branch called
feature2. If no other commits have been made to main since feature2
branched off, Git can simply "fast-forward" main to include all of the
feature2 commits.
• Fast-forward merges result in a linear history.
• Git looks at the common ancestor between the two branches (the commit
where they diverged), compares it with the current state of each branch, and
creates a new merge commit.
• This merge commit brings the histories of both branches together.
3. No Merge Commit:
• Unlike a regular merge, no merge commit is created. The result is a linear
history, where the commits from the feature branch appear as if they were
added directly on top of the main branch, without the divergence that occurs
in regular merges.
4. Fast-Forward Merge:
• After rebasing, the feature branch can be merged into the main branch
with a fast-forward merge, creating a clean, linear history.
<<<<<<< HEAD
Your changes in the current branch
=======
Changes in the branch you're merging into
>>>>>>> <branch-name>
Abort a Merge:
• If you don't want to continue with the merge due to conflicts or other
reasons, you can abort it: git merge --abort