Git is a distributed version control system (DVCS) that allows multiple people to work on the
same project simultaneously. Git is open-source and provides a rich set of features for
managing changes to source code and other files. In this guide, we will cover the following
topics:
1. Setting up Git
2. Creating a Repository
3. Making Changes
4. Branching
5. Merging
6. Viewing Changes
7. Undoing Changes
8. Remote Repositories
9. Tags
10. Git Workflow
11. Cheat Sheet
1. Setting up Git
To install Git, follow the instructions for your operating system:
MacOS: Install Homebrew, and then run brew install git in the terminal.
Windows: Download and install Git from the official Git website.
Linux: Run sudo apt-get install git (for Ubuntu) or sudo yum install git (for CentOS/Fedora) in
the terminal.
Once you have installed Git, you should set up your Git configuration.
You can set your name and email using the following commands:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
You can set the default text editor using the following command:
git config --global core.editor <editor_name>
To check your Git configuration, use the following command:
git config list
2. Creating a Repository
To create a new Git repository, navigate to the directory where you want to create the
repository and use the following command:
git init
This will create a new repository in the current directory. You can also clone an existing
repository using the following command:
git clone <repository_url>
This will create a copy of the repository on your computer.
3. Making Changes
Once you have a repository, you can make changes to the files in it. To add changes to the
staging area, use the following command:
git add <file_name>
You can also add all changes to the staging area using the following command:
git add .
To unstage changes, use the following command:
git reset <file_name>
Once you have added the changes you want to commit, use the following command to commit
the changes:
git commit -m "Commit message"
To amend the last commit, use the following command:
git commit amend
This will open the text editor specified in your Git configuration, allowing you to edit the
commit message.
4. Branching
Branching is a powerful feature of Git that allows you to work on different versions of the code
simultaneously. To create a new branch, use the following command:
git branch <branch_name>
To switch to a different branch, use the following command:
git checkout <branch_name>
You can also create and switch to a new branch using the following command:
git checkout -b <branch_name>
To merge changes from one branch to another, use the following command:
git merge <branch_name>
5. Merging
Merging is the process of combining changes from one branch to another. There are two main
types of merges: fast-forward and three-way merge.
A fast-forward merge occurs when the changes in the branch being merged are a direct
extension of the current branch. To perform a fast-forward merge, use the following command:
git merge <branch_name>
A three-way merge occurs when the changes in the branch being merged conflict with the
changes in the current branch. Git will attempt to automatically merge the changes, but
sometimes manual intervention is required.
6. Viewing Changes
Git provides several commands for viewing changes to your code. To view the changes in the
working directory, use the following command:
git status
This will show you which files have been modified and which files are staged for the next
commit.
To view the changes in a specific file, use the following command:
git diff <file_name>
This will show you the differences between the current version of the file and the previous
version.
To view the changes in a commit, use the following command:
git show <commit_hash>
This will show you the changes made in the specified commit.
7. Undoing Changes
Git provides several commands for undoing changes to your code. To discard changes in the
working directory, use the following command:
git checkout -- <file_name>
This will discard all changes to the specified file since the last commit.
To unstage changes, use the following command:
git reset <file_name>
To undo the last commit, use the following command:
git reset --soft HEAD^
This will reset the repository to the state it was in before the last commit.
To discard changes in a commit, use the following command:
git revert <commit_hash>
This will create a new commit that undoes the changes made in the specified commit.
8. Remote Repositories
Git allows you to work with remote repositories, which are copies of the repository that are
hosted on a server. To add a remote repository, use the following command:
git remote add <remote_name> <repository_url>
To push changes to a remote repository, use the following command:
git push <remote_name> <branch_name>
To pull changes from a remote repository, use the following command:
git pull <remote_name> <branch_name>
9. Tags
Git allows you to create tags, which are named references to specific commits. To create a tag,
use the following command:
git tag <tag_name> <commit_hash>
To list tags, use the following command:
git tag
To delete a tag, use the following command:
git tag -d <tag_name>
10. Git Workflow
There are many different Git workflows that teams can use, but one popular workflow is the
Gitflow workflow. In the Gitflow workflow, there are two main branches: master and develop.
The master branch contains the production-ready code, while the develop branch contains the
code that is being actively developed.
When working on a new feature or bug fix, a developer creates a new branch off of the develop
branch. Once the work is complete, the developer creates a pull request (PR) to merge the
changes into the develop branch. The PR is reviewed by other team members, and once it is
approved, the changes are merged into the develop branch.
When it is time to release a new version of the code, the develop branch is merged into the
master branch. This creates a new version of the code that is ready for release.
Conclusion
Git is a powerful tool for managing changes to code and collaborating with other developers. In
this guide, we covered the essential concepts and commands for using Git. With this
knowledge, you should be able to use Git to manage your code and work with other developers
effectively.
GIT CHEAT SHEET
Configuration:
Set up Git with your name and email:
git config --global user.name "Your Name"
git config --global user.email
"your.email@example.com"
Set the default text editor:
git config --global core.editor
<editor_name>
Check the Git configuration:
git config list
Create a new local repository:
git init
Clone an existing remote repository:
git clone <repository_url>
Gitignore:
Ignore certain files or directories in your
repository, create a file called .gitignore in
the root directory of your repository and
add the filenames and patterns of the files
you want to ignore. For example:
*.log
node_modules/
This .gitignore file will ignore all files with
the .log extension and the node_modules
directory.
Making Changes:
Add changes to the staging area:
git add <file_name>
git add .
Unstage changes:
git reset <file_name>
Commit changes with a message:
git commit -m "Commit message"
Amend the last commit:
git commit amend
Change the last commit message:
git commit --amend -m "New commit
message"
Change the author of the last commit:
git commit --amend --author "Your Name
<your.email@example.com>"
Rebasing:
Rebase your current branch onto
another branch:
git rebase <branch_name>
Branching:
Create a new branch:
git branch <branch_name>
List all branches:
git branch
Switch to a branch:
git checkout <branch_name>
Create and switch to a new branch:
git checkout -b <branch_name>
Merge changes from another branch:
git merge <branch_name>
Delete a branch:
git branch -d <branch_name>
Viewing Changes:
View changes in the working directory:
git status
View the differences between the
working directory and the repository:
git diff
View the differences between the
staging area and the repository:
git diff staged
View the differences between two
branches:
git diff <branch_name_1>
<branch_name_2>
View the commit history:
git log
View the commit logs in a graph
format:
git log graph
View the commit logs with a summary
of changes:
git log stat
Undoing Changes:
Revert changes in the working
directory:
git checkout -- <file_name>
Undo the last commit and keep
changes in the working directory:
git reset HEAD^
Discard changes and reset to the last
commit:
git reset --hard HEAD
Undo a merge:
git merge abort
Tags:
Create a tag:
git tag <tag_name>
Create an annotated tag with a
message:
git tag -a <tag_name> -m "Tag message"
List all tags:
git tag
Show details of a tag:
git show <tag_name>
Push tags to a remote repository:
git push <remote_name> <tag_name>
Remote Repositories:
Add a remote repository:
git remote add <remote_name>
<repository_url>
List all remote repositories:
git remote -v
Remove a remote repository:
git remote remove <remote_name>
Push changes to a remote repository:
git push <remote_name> <branch_name>
Push changes to a remote repository
and set the upstream branch:
git push -u <remote_name>
<branch_name>
Pull changes from a remote repository:
git pull <remote_name> <branch_name>
Stashing:
Stash your current changes:
git stash
Retrieve your stashed changes:
git stash apply