10 Git Beginner Commands You Should Know

git init

The init command initializes a new Git repository in your current working directory. This is the first step to start tracking your project with Git. Simply navigate to your project’s root folder in the command line and type git init.

git clone

The clone command allows you to create a copy of an existing remote repository on your local machine. This is useful when you want to work on a project that is already hosted on a platform like GitHub or GitLab. To clone a repository, run git clone <repository-url>.

git add

git add is used to stage changes in your working directory for a commit. You can either add specific files or all changes at once by running git add <file-path> or git add ., respectively. Staging your changes allows you to selectively include changes in your next commit.

git commit

The commit command saves your staged changes to the repository’s history with a descriptive message. This is a crucial step to document your progress and make your code history easier to understand. Use the command git commit -m "Your commit message" to commit your changes.

git status

git status gives you an overview of the current state of your working directory, including modified, staged, and untracked files. This command is helpful to review your changes before committing, ensuring you don’t accidentally commit unwanted files or miss important changes.

git log

The log command displays a list of all the commits in the repository’s history. This allows you to review previous changes, commit messages, and authors. To view the log with a more visually appealing format, use git log --oneline --graph --decorate.

git diff

git diff shows the differences between your working directory and the latest commit. This is useful to review your changes before staging or committing them. To see the differences between the staged changes and the latest commit, use git diff --staged.

git checkout

The checkout command allows you to switch between branches, commits, or files in your repository. To switch to a specific branch, run git checkout <branch-name>. If you want to discard changes in a specific file and revert it to the last committed state, use git checkout -- <file-path>.

git branch

git branch lists all the branches in your repository and highlights the currently active branch. To create a new branch, run git branch <new-branch-name>. Remember, creating a new branch doesn’t automatically switch you to that branch. Use git checkout to switch branches. For newer versions of Git, you can also use git switch to switch branches.

git merge

The merge command combines the changes from one branch into another, typically used when merging feature branches into the main branch. To merge a branch, first checkout the target branch (e.g., main) and then run git merge <source-branch-name>.