Most Used Commands in Git

https://images.unsplash.com/photo-1618401471353-b98afee0b2eb
Jun 9, 2024

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.

git init

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.

git clone

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.

git add

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.

git 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.

git push

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.

git pull

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.

git branch

The git branch command lists, creates, or deletes branches.

git branch

This command lists all the branches in your repository.

To create a new branch:

git branch new-feature

To delete a branch:

git branch -d old-feature

git checkout

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

git merge

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.

git rebase

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.

Conclusion

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!