top 10 git commands for beginners

Top 10 Git Commands For Beginner Developers

Previously, we shared an article about 10 Essential Commands to Master Git. Even though that post is also focused on beginners, we didn’t get into detail about each git command. So in this article, we will list the top 10 git commands for beginner developers in detail.

Table of Contents:

git init – Initialize Local Repository

The “git init” command initializes or sets up an empty Git repository in your current project directory locally on your computer. This creates a hidden folder named “.git,” that stores all necessary metadata for versioning and tracking changes to your codebase over time.

git clone – Create Local Copy

The “git clone” command clones or creates a local copy of an existing remote Git repository on your computer. You can clone any public repository on platforms like Github, or Bitbucket. You can also clone private repositories if you’ve got the permission to do so. The command to clone a repo is in the format specified below

git clone <remote_repo_url>   
eg: git clone

If you want to clone directly to a specific branch, then use the –branch option as specified below.

git clone --branch <branch_name> <remote_repo_url>

git branch – Create & Manage Branches

The “git branch” command enables you to create, delete, list, and switch between branches in your project as needed without affecting the production branch which is usually named “master” or “main”. Thanks to the branching feature, it is possible to incorporate changes from multiple contributors and maintain a stable working environment without breaking the existing code. You can use the below commands to create, delete, list, and switch between branches.

create a new branch locally.

git branch branch_name     

List all the local and remote branches of the repository.

git branch -a     

Delete an existing branch.

git branch -d branch_name

Rename a branch

git branch -m current_branch-name new-branch-name

git checkout – Switch Between Branches

Use the “git checkout” command to switch between branches in your project. This enables developers to work on different features or bug fixes independently without disrupting the primary codebase. Use this command followed by “<branch\_name>” (e.g., “git checkout feature\_branch”) to change contexts as required for various aspects of development work.

git checkout branch_name_to_switch_to

There is also another trick using git checkout, which is a bit dangerous.

git checkout file_name

The above command removes all the changes you made to a file. So please use this only if you need it and be extra careful.

git add – Stage Files

The “git add” command stages files that you want to include in the next commit. You can either stage a single file or all files at once using the “git add” command.

To stage a single file,

git add file_name

To stage all files with changes, including new files,

git add .

To add only modified and deleted files,

git add .

git commit – Save Changes

The “git commit” command saves changes you’ve made locally as commits. Every commit is a snapshot of your code at a specific point. After adding your changes using the “git add” command specified above, you can use git commit to get the changes ready to publish to a remote repository. Each Git commit should always include a message describing the changes made to provide context and clarity on what was updated within the codebase.

To commit changes:

git commit -m "description of changes"

To change the message of a previous commit, use the –amend option:

git commit --amend

To add all files and commit changes with a message at once, you can use the -am option:

git commit -am "description of changes"

git push – Publish Committed Changes

The “git push” command sends your local commits to remote repositories. Use this command followed by “<remote\_name> <branch\_name>”

git push remote_name branch_name
eg: git push origin master

git pull – Synchronize Remote Changes

The “git pull” command fetches and merges new changes from a remote repository to your local repository with the latest modifications, bug fixes, or enhancements made by team members. This ensures consistency across all branches in your Git workflow.

git pull remote_name branch_name
eg: git pull origin master

git merge – Combine Branches

The “git merge” command allows you to combine changes from two or more distinct branches into a single branch. Just make sure you are in the right branch before you merge changes from different branches. To avoid any mishaps, you can checkout to the branch that you need changes to be merged into.

git checkout master
git merge feature_branch 

git status – Monitor Project State

The “git status” command displays all files that have been modified or changed since their last commit. These files are normally called “Untracked files”. This information helps you keep track of progress and manage changes effectively as part of your version control workflow within Git repositories.