Introduction
Git is a popular version control system used by developers worldwide. With Git, developers can work on and track changes in codebases effectively. Git's distributed model allows developers to have their own copies of the codebase, enabling them to work independently yet collaboratively. Git also provides tools for branching, merging, and tracking changes, making it an essential tool in software development.
In this article, we will discuss 20 basic Git commands every developer should know.
Basic Git Commands
1. git init
The git init
command initializes a new Git repository in the current directory. This command creates a new .git
directory in the project directory, which contains files that Git uses to keep track of changes in the codebase.
git init
2. git clone
The git clone
command creates a copy of an existing Git repository. Cloning a repository creates a complete copy of the codebase, including the entire history of changes made.
git clone <repository URL>
3. git add
The git add
command stages changes in the working directory. Staged changes are then committed by Git to the local repository. Staging changes means that they will be included in the next commit.
git add <file or directory>
4. git commit
The git commit
command creates a new commit in Git. A commit records the current state of files and directories. The commit message describes the changes made in the commit.
git commit -m "commit message"
5. git status
The git status
command shows the status of the working directory in Git. This command tells developers which changes are staged, which changes are not staged, and which files are untracked.
git status
6. git push
The git push
command uploads changes made in the local repository to a remote repository. This command is used to share changes made with other developers.
git push
7. git pull
The git pull
command fetches changes from a remote repository and merges them in the local repository. This command is used to keep the local repository up to date with changes made by other developers.
git pull
8. git branch
The git branch
command creates and manages branches in Git. Branches allow developers to work on different features or versions of the project simultaneously.
// To list all branches
git branch
// To create a new branch
git branch <branch-name>
9. git checkout
The git checkout
command switches between branches in Git. This command is used to move from one branch to another.
// To switch to a particular branch
git checkout <branch-name>
// To create a new branch and switch to it
git checkout -b <branch-name>
10. git merge
The git merge
command combines changes made in two or more branches. This command is used to merge changes made in different branches into one branch.
// To merge a branch into the current branch
git merge <branch-name>
11. git log
The git log
command shows the commit history of the local repository. This command displays a list of commits in reverse chronological order.
git log
12. git diff
The git diff
command shows the differences between the working directory and the most recent commit. This command is used to view changes made but not committed.
git diff
13. git reset
The git reset
command resets the staging area to the last commit. This command discards any changes in the staging area.
git reset
14. git revert
The git revert
command creates a new commit that undoes changes made in a previous commit. This command is used to roll back changes made to a project.
git revert <commit-hash>
15. git stash
The git stash
command saves changes made in the working directory but not yet committed. This command allows developers to switch to a different branch without committing changes.
git stash
16. git stash apply
The git stash apply
command restores changes previously stashed. This command is used to apply stashed changes to the working directory.
git stash apply
17. git remote
The git remote
command lists the remote repositories associated with the local repository. This command tells developers which repositories can be pushed to or pulled from.
// To list remote repositories
git remote
// To add a remote repository
git remote add <remote-name> <remote-URL>
18. git fetch
The git fetch
command fetches changes from a remote repository but does not merge them into the local repository. This command is used to view changes made in a remote repository before merging them.
git fetch
19. git switch
The git switch
command switches between branches in Git. This command was introduced in Git 2.23 and is a replacement for the git checkout
command.
// To switch to a particular branch
git switch <branch-name>
// To create a new branch and switch to it
git switch -c <branch-name>
20. git restore
The git restore
command restores files in the working directory to their state in the last commit. This command is used to discard changes made since the last commit.
git restore <file>
Conclusion
Git is a powerful version control system for managing codebases. These 20 basic Git commands will enable developers to track changes, collaborate with other developers and manage codebases effectively. Mastery of these Git commands will help developers become more productive and efficient in software development.