- Introduction to Git
- Definition and Importance of Git
- Basic Concepts in Git
- Git Setup and Configuration
- Installation of Git
- Initial Configuration (username, email)
- Creating and Cloning Repositories
- Initializing a New Repository
- Cloning an Existing Repository
- Basic Git Commands
- git add
- git commit
- git status
- git log
- Branching and Merging
- Creating Branches
- Switching Branches
- Merging Branches
- Merge Conflicts
- Remote Repositories
- Connecting to a Remote Repository
- Pushing Changes to Remote
- Pulling Changes from Remote
- Undoing Changes
- git revert
- git reset
- Dealing with Merge Conflicts
- Understanding Merge Conflicts
- Resolving Merge Conflicts
- Git Stash and Advanced Stashing
- Using Git Stash
- Applying and Managing Stashes
- Rebasing in Detail
- Understanding Rebasing
- Performing a Rebase
- Tags and Releases
- Creating Tags
- Managing Release Versions
- Git Best Practices
- Committing Best Practices
- Branch Management
- Git Workflows
- Centralized Workflow
- Feature Branch Workflow
- Gitflow Workflow
- Forking Workflow
- Git Hooks
- Implementing Git Hooks
- Gitignore File
- Ignoring Files in GitSecurity in Git
- Signing Commits and TagsGit GUI Clients
- Overview of GUI Options
- Collaborating with Pull Requests
- Process and Benefits of Pull RequestsGit in the Cloud
- Cloud Services for Git Hosting and Collaboration
1. Introduction to Git
What is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. It’s designed to handle everything from small to very large projects with speed and efficiency. Git is distributed, meaning that every developer’s computer holds the full history of the project, enabling easy branching and merging.
Importance of Version Control
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to:
- Revert files back to a previous state.
- Revert the entire project back to a previous state.
- Compare changes over time.
- See who last modified something that might be causing a problem.
- Who introduced an issue and when.
Key Terms
- Repository (Repo): A directory which contains your project work, as well as a few files (hidden by default in Unix) which are used to communicate with Git. Repositories can exist either locally on your computer or as a remote copy on another computer.
- Commit: A commit, or “revision”, is an individual change to a file (or set of files). It’s like when you save a file, but with Git, every time you save it creates a unique ID (a.k.a. the “commit hash”) that allows you to keep a record of what changes were made when and by who.
- Branch: A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is
master
. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically. - Merge: Merging is Git’s way of putting a forked history back together again. The
git merge
command lets you take the independent lines of development created bygit branch
and integrate them into a single branch.
2. Setting Up and Configuring Git
Before you can use Git, you need to install it and configure it on your machine.
Installing Git
- On Windows: Download the official Git installer from git-scm.com, and follow the instructions.
- On macOS: Use Homebrew by typing
brew install git
in the terminal, or download the installer as with Windows. - On Linux: Use your distro’s package manager, e.g.,
sudo apt-get install git
for Ubuntu orsudo yum install git
for Fedora.
Basic Git Configuration
After installing Git, you should configure your personal information.
- Set your name (which will appear in commits):
git config --global user.name "Your Name"
- Set your email address (which should match your version control service account, like GitHub):
git config --global user.email "your_email@example.com"
Checking Your Settings
You can check your configuration at any time:
git config --list
Configuring Text Editor
Set your favorite text editor to be used by default with Git:
- For Vim:
git config --global core.editor "vim"
- For Nano:
git config --global core.editor "nano"
- For VS Code:
git config --global core.editor "code --wait"
Caching Your Login Credentials
So you don’t have to keep re-entering your username and password, you can tell Git to remember them for a while:
git config --global credential.helper cache
3. Getting Started with Git
Creating a New Repository
- To create a new repo, you’ll use the
git init
command. Here’s how you do it:
mkdir MyNewProject
cd MyNewProject
git init
This initializes a new Git repository. Inside your project folder, Git has created a hidden directory named .git
that houses all of the necessary repository files.
Cloning an Existing Repository
- If you want to work on an existing project that is hosted on a remote server, you will clone it using:
git clone [url]
For example:
git clone https://github.com/user/repo.git
This command makes a complete copy of the entire history of the project.
4. Basic Git Operations
Checking the Status
- The
git status
command gives you all the necessary information about the current branch.
git status
Tracking New Files
- To start tracking a file, use the
git add
command.
git add <filename>
- To add everything at once:
git add .
Ignoring Files
- Sometimes there are files you don’t want to track. Create a file named
.gitignore
in your project root and list the files/folders to ignore.
# Example .gitignore content
log/*.log
tmp/
Committing Changes
- To commit changes to your repository, use:
git commit -m "Commit message here"
- To commit all staged changes:
git commit -a -m "Commit message here"
Viewing the Commit History
- To see the commit history:
git log
- For a more condensed view:
git log --oneline
5. Branching and Merging in Git
Branching in Git
Branches are a powerful feature in Git that enable you to diverge from the main line of development and work independently, without affecting the main line.
Creating a New Branch
- To create a new branch:
git branch <branch-name>
- To switch to the new branch:
git checkout <branch-name>
- You can also create and switch to a new branch in one command using:
git checkout -b <branch-name>
Listing Branches
- To list all the branches in your repo, including remote branches:
git branch -a
Merging Branches
- To merge changes from one branch into another:
git checkout <branch-you-want-to-merge-into>
git merge <branch-you-want-to-merge-from>
- If Git can’t automatically merge changes, you may have to solve conflicts manually. After resolving the conflicts, you need to stage the changes and make a commit.
Deleting Branches
- To delete a branch:
git branch -d <branch-name>
The -d
option deletes the branch only if you have already merged it into another branch. If you want to force deletion, use -D
instead.
6. Working with Remote Repositories
Remote repositories are versions of your project that are hosted on the internet or network somewhere.
Adding a Remote Repository
- When you clone a repository, it automatically adds that remote repository under the name “origin”.
- To add a new remote URL:
git remote add <name> <url>
Viewing Remote Repositories
- To view the remote repositories configured for your project:
git remote -v
Pulling Changes from a Remote Repository
- To fetch changes from a remote repository and merge them into your current branch:
git pull <remote>
Pushing Changes to a Remote Repository
- To send your commits to a remote repository:
git push <remote> <branch>
Checking out Remote Branches
- To check out a remote branch:
git fetch
git checkout -b <branch-name> <remote>/<branch-name>
7. Advanced Git Features
Stashing Changes
- You can use
git stash
to record the current state of the working directory and the index, but want a clean working directory:
git stash
git stash apply # re-apply the stashed changes
Rebasing
- Rebasing is another way to integrate changes from one branch into another. Rebasing re-writes the commit history by creating new commits for each commit in the original branch.
git rebase <base>
Tagging
- Tags are used to mark specific points in history as being important:
git tag <tagname>
This concludes the essentials of branching, merging, and working with remote repositories, as well as touching on some advanced features. Each of these areas has much more depth to explore, such as dealing with merge conflicts, managing remotes, and leveraging advanced rebasing and stashing strategies for complex workflows.
8. Dealing with Merge Conflicts
Understanding Merge Conflicts
Merge conflicts happen when Git is unable to automatically resolve differences in code between two commits. Conflicts only affect the developer conducting the merge; the rest of the team is unaffected until the conflict is resolved.
Resolving Merge Conflicts
- When you encounter a merge conflict, Git will mark the files that are conflicting.
- You can open these files and look for the lines marked with
<<<<<<<
,=======
, and>>>>>>>
. These markers define the conflicting sections. - Resolve the conflicts by editing the files to remove the markers and make sure the code is as you want it.
- After fixing the conflicts, stage the files:
git add <file>
- Then, continue the merge process by committing the changes:
git commit
9. Git Stash and Advanced Stashing
Using Git Stash
- `git stash` is useful when you need a clean working directory (for example, when pulling in changes from a remote repository).
- To stash changes:
git stash
- To list all stashes:
git stash list
- To apply a stash and remove it from the stash list:
git stash pop
- To apply a stash without removing it from the stash list:
git stash apply stash@{}
10. Rebasing in Detail
Rebasing vs. Merging
- Rebasing is a way to integrate changes from one branch into another by moving the entire branch to begin on the tip of the other branch.
- Unlike merging, rebasing flattens the history because it transfers the completed work from one branch to another in a linear process.
Performing a Rebase
- To rebase:
git checkout feature-branch
git rebase master
- If conflicts arise, resolve them in a similar way to merge conflicts.
- After solving conflicts, continue the rebase with:
git rebase –continue
11. Tags and Releases
Creating Tags
- Tags mark specific points in history as being significant, typically as release points.
- To create an annotated tag:
git tag -a v1.0 -m “Release version 1.0”
- To push tags to a remote repository:
git push origin –tags
12. Git Best Practices
- Commit often. Smaller, more frequent commits are easier to understand and roll back if something goes wrong.
- Write meaningful commit messages. Others should understand the purpose of your changes from the commit message.
- Don’t commit half-done work.
- Test before you commit. Don’t commit anything that breaks the development build.
- Keep your branches focused. Each branch should represent a single feature or fix.
13. Git Workflows
Understanding and choosing the right Git workflow is crucial for a team to manage code changes effectively.
Centralized Workflow
- Similar to SVN, all developers work on a single branch.
- The master branch is the source of truth, and all changes are committed into this branch.
Feature Branch Workflow
- Each feature is developed in its own branch and then merged into the master branch when complete.
- Ensures the master branch always contains production-quality code.
Gitflow Workflow
- A set structure that assigns very specific roles to different branches and defines how and when they should interact.
- It uses individual branches for preparing, maintaining, and recording releases.
Forking Workflow
- Each developer has their own server-side repository.
- Offers a robust way to integrate contributions from all developers through pull requests or merge requests.
14. Git Hooks
- Scripts that can run automatically before or after certain important Git actions, such as commit or push.
- They are used for automating tasks and enforcing certain rules before a commit can be submitted.
15. Gitignore File
- Specifies intentionally untracked files that Git should ignore.
- Files already tracked by Git are not affected.
22. Collaborating with Pull Requests
- Pull requests let you tell others about changes you’ve pushed to a branch in a repository on GitHub.
- Once a pull request is opened, you can discuss and review the potential changes with collaborators.