Git is an essential tool for version control in modern software development. Here are some of the most common commands you will use with Git, along with simple examples to help you understand their usage.
The git init
command is used to initialize a new Git repository.
git init
This command creates a new .git directory in your project, which contains all the necessary repository files.
The git clone command is used to copy an existing Git repository from a remote server.
git clone https://github.com/username/repository.git
This command downloads the repository and creates a directory with the same name as the repository.
The git add command adds changes in the working directory to the staging area.
git add file.txt
You can also add all changes using:
git add .
This command stages the changes, making them ready for commit.
The git commit command captures a snapshot of the project's currently staged changes.
git commit -m "Add new feature"
The -m flag allows you to add a commit message directly from the command line.
The git push command is used to upload local repository content to a remote repository.
git push origin main
This command pushes the changes in the local main branch to the remote repository.
The git pull command fetches and integrates changes from a remote repository into the current branch.
git pull origin main
This command combines git fetch and git merge to update the local branch with changes from the remote branch.
The git branch command lists, creates, or deletes branches.
git branch
This command lists all the branches in your repository.
git branch new-feature
git branch -d old-feature
The git checkout command is used to switch between branches or restore working directory files.
git checkout new-feature
This command switches to the new-feature branch.
To create and switch to a new branch:
git checkout -b new-feature
The git merge command integrates changes from one branch into another.
git merge new-feature
This command merges the new-feature branch into the current branch.
The git rebase command is used to reapply commits on top of another base tip.
git rebase main
This command replays commits from the current branch on top of the main branch.
These are some of the most common Git commands you will use in your development workflow. Understanding these commands will help you manage your codebase efficiently and collaborate effectively with other developers.
For any further queries, feel free to reach out or explore more resources online. Happy coding!