Git Basics
What is Git?
Git is a distributed version control system that allows developers to track changes to their code, collaborate with others, and manage software development projects effectively.
In this guide, we'll explore some fundamental Git commands that every developer should know.
1. git config
The git config
command is used to configure settings for our git environment. This allows us to set various options such as our name and email address.
git config --global user.name "Your Name"
git config --global user.email "something@example.com"
2. git init
The git init
command is used to initialize an empty git repository. It creates a hidden .git
directory to store repository metadata.
git init
3. git clone
The git clone
command is used to clone a repository. For creating a local copy of a remote repository, use the git clone
command followed by the repository URL.
git clone <repository_url>
For example:
git clone https://github.com/username/repository.git
4. git add
The git add
command is used for staging the changes done to the repository.
git add <file_name>
We can stage all the changes in the current repository by using the following command.
git add .
5. git commit
The git commit
command is used for committing the staged changes to the repository.
git commit -m "Commit message"
git commit
will save the changes to the local repository.
6. git push
The git push
command is used for pushing the local repository changes to a remote repository.
git push origin <branch_name>
For example:
git push origin main
7. git pull
The git pull
command is used for fetching and merging changes from a remote repository to our local repository.
git pull origin <branch_name>
For example:
git pull origin main
This command will update our local repository with changes from the specified branch on the remote repository.
8. git status
The git status
command is used for checking the status of the repository. This command is used to see which files have been modified, staged, or untracked.
git status
9. git log
The git log
command is used for viewing the commit history of our repository.
git log
This command gives a detailed view of all the commits done in the repository.
To get a graphical representation of the commit history and the relationships between branches and commits, use the following command.
git log --all --graph
10. git branch
The git branch
command is used for managing and changing branches within a Git repository.
The below command is used for listing all the branches in the repository.
git branch
The below command is used for creating a new branch.
git branch <branch_name>
Happy coding!