Introduction to Git
Git is an essential version control system that enables developers to track changes and collaborate effectively on projects. It has become a crucial tool for managing code, whether you're working alone or with a team. However, learning Git can be daunting, especially for beginners unfamiliar with its commands and functionality. In this Git cheatsheet, we’ll explore key commands every developer should know, from setting up a repository to advanced tasks like branching and merging. This guide is designed to help both newcomers and seasoned developers enhance their Git skills and streamline their workflow, making Git easier and more efficient to use.
Basic Git commands
Initialization
To initialize a new Git repository in the current directory, run the following command:
This creates a hidden .git
directory in the current directory that tracks changes to your code.
Cloning
To clone an existing Git repository to your local machine, run the following command:
This creates a new directory on your computer with a copy of the repository.
Staging changes
Before you commit changes to your code, you need to stage them using the git add
command. This tells Git which changes you want to include in your commit.
To stage a file or directory, run the following command:
You can also stage all changes in the current directory by running:
Committing changes
To commit the changes in the staging area with a commit message, run the following command:
The commit message should briefly describe the changes you made in this commit.
Checking status
To check the current status of your repository, run the following command:
This will show you which files have been modified, which files are staged for commit, and which files are untracked.
Viewing changes
To view the changes between the working directory and the staging area, run the following command:
To view the changes between the staging area and the last commit, run the following command:
Branching
Git allows you to create multiple branches of your code, enabling you to work on different features or fixes without affecting the main codebase. The default branch in Git is called master
.
To create a new branch with a specified name, run the following command:
To switch to the specified branch, run the following command:
You can also create and switch to a new branch in one command by running:
To merge the specified branch into the current branch, run the following command:
Pushing changes
To push changes to a remote repository, run the following command:
Here, <remote>
is the name of the remote repository (commonly origin
), and <branch>
is the name of the branch you want to push.
Pulling changes
To pull changes from a remote repository, run the following command:
In this command, <remote>
refers to the name of the remote repository (often origin
), and <branch>
is the name of the branch you want to pull.
Viewing history
To view the commit history, run the following command:
This will show you a list of all the commits in the repository, along with the commit message, author, and date.
Connect with Me
Happy coding! 💻👨💻👩💻
Top comments (0)