15 most used git commands
Git

Git Commands Cheat Sheet: Top 15 Commands You Need to Know

Git is the most popular version control tool in current times. Many organizations, in one way or another, are using Git to manage their source code and files. Also, for the many other features the providers make available. In this post we are going to discuss top 15 Commands You Need to Know.

Github, Gitlab, and Bitbucket are a few of the most popular git platforms. They provide a lot of additional features apart from the basic git system.

Developers need to know how Git works and what are the most useful git commands that help you with your day-to-day git-related needs.

Let’s discuss some of the most used git commands.

1. Initialize a new Git repository

git init

The Git init command initializes a new Git repository in the current directory. This is typically one of the first commands you’ll run when starting a new project. After running this command, you can start tracking changes to your project files with Git.

2. Create a local copy of the remote repository

git clone <Remote_Repository>

Git clone is the first command you will need to clone the repository on your local machine. You can understand cloning as creating a local copy of the code that is present on the remote repository.

3. Add all the files to the staged area

git add <file_name><br>Example: git add file.txt

This command will add the files in the unstaged area to the staged area on your local machine.

4. Shows the current state of the repository

git status

This command shows the current state of the repository, including which files are modified and which are staged for commit. This command is useful for determining which files have changed and which changes have not yet been committed.

5. Commit the changes

git commit<br>Example: git commit -m "initial commit"

This command will create a new commit with all the files/ changes in the staged area. You can and should put a message with each commit so that it will be clear what the purpose of the commit was. An example is shown above of how you can use the git commit command with the relevant flag.

6. Pushing the changes to the remote repository

git push

Once you have committed the changes and the same you can verify with the git log command. You can see a new entry in the git log with the recent changes you committed.

7. Shows a log of all the commits in the repository

git log

This command shows a log of all the commits in the repository, including who made the commit and when. This command is useful for tracking the history of changes to your project.

8. Fetch the remote repository changes

git pull

Git pull is the command to fetch all the changes to your local copy of the repository from the remote repository. Any changes done in the remote repository will be synced with your local copy with this command.

9. Creating a new branch from the current branch

git checkout -b <new_branch_name>

There is often a need to create a new branch from the main or current branch. Often you would not want to push the changes in the main branch instead you would like to keep your worn on a separate branch. This command helps you to create a new branch from the current branch. All the changes you have made will be carried to the new branch.

10. Show all the branches in the repository

git branch

This command shows all the branches in the repository. A branch is a separate line of development in Git and is typically used for experimenting with new features or making changes to existing code without affecting the main codebase.

11. Merging a branch in the current branch

git merge <branch_name_to_be_merged>

Once you decide to merge the branch into the main branch or the current branch you are working on. You can merge all the changes using the above command. All the commits done on the other branch will be applied to the current branch.

12. Merging a branch in the current branch without commits

git merge -squash <branch_name_to_be_merged>

The ‘-squash’ flag only applies the changes on your current branch. It does not retain the commit log. After the above command, you will have to commit the changes on the current branch. No matter how many commits are there on the other branch, all the changes will be merged without any commit.

13. Retrieve changes from a remote repository

git fetch

The command retrieves changes from a remote repository but does not merge those changes into your local repository. This command is useful for checking what changes have been made in the remote repository without affecting your local repository.

14. Unstage changes

git reset

git reset is a command used to unstage changes that have been added to the staging area. This command can also be used to undo commits by moving the branch pointer to a previous commit. When used with the — hard option, it will discard all changes made to tracked files since the specified commit. This command can be useful when you have made changes to your local repository that you want to undo or when you want to unstage changes that you accidentally added to the staging area.

15. Apply a specific commit from one branch to another

git cherry-pick

This command is used to apply a specific commit from one branch to another. This is useful when you want to apply a specific change from one branch to another without merging the entire branch.

In the last post we had discussed about Crucial Tasks to Tackle When Learning a new Programming Language. You can read that post here.

Thanks for reading.

Subscribe to the BitsToGigs Newsletter

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

You may also like

More in:Git

Leave a reply

Your email address will not be published. Required fields are marked *