Software development is all about writing code and developing solutions following requirements and processes. Managing code becomes complex with time and multiple contributors. In the real scenario, multiple developers work on the same project and write code on a daily basis. Tracking changes and merging those codes are hassle and time-consuming without any proper tools or platforms. Another issue is rollback which is not possible without using any version control tools. When it comes to writing code, source code management is another important factor to consider. Source code management is not only just storing the code safely but also tracking the changes, resolving the conflicts, and merging from multiple contributors.

To know more about Git, and GitHub, please check this article. Click Here

In this article, I will share some most useful git commands for all developers and programmers which are used on daily basis working with git repositories.

Git is a version control system for managing the source code which keeps the track of it with many options. Basically, it is a software to track the changes of files mostly used for coders to work collaboratively and source code management during the software development.

As per Git-SCM (https://git-scm.com/)

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git Commands

Below are the top most git commands for programmers and developers.

  • Git Init
  • Git Clone
  • Git Add
  • Git Config
  • Git Add
  • Git Remove
  • Git Commit
  • Git Pull
  • Git Push
  • Git Reset
  • Git Status
  • Git Branch
  • Git Diff
  • Git Checkout
  • Git Merge
  • Git Stash
  • Git Log

Git Init

To initialize a new git repository. Namely, to start git repository

Code
git init
git init [repository name]

simply navigate to the folder which you want to add to git repository and run this command.

Git Clone

This command is to create a local copy of a remote git repository through a URL.

Code
git clone [URL]
Git Status

To check the status of git.

Code
git status

Git Config

To configure git author with name and email.

Code
git config -global user.name “[user_name]”
git config -global user.email “[user_email]”
git config -local user.name “[user_name]”
git config -local user.email “[user_email]”

Git Add

This command is used for adding file(s) to git repository.

Code
git add [file_name]

To add all modified files

Code
git add -A 
git add .
git add *

Note: this will add the file(s) into staging area.

Git Remove

To remove the file or folder from your working directory and stages deletion.

Code
git rm [file_name]
git rm -r [file_name]

Git Commit

To commit changes into git version history.

Code
git commit -m “commit message”
git commit -a

Above commit command will commit all the files you have added via git or modified.

Git Pull

To fetch and merge the latest commit from a remote server to your local working directory.

Code
git pull
git pull [branch_name]
git pull orgin [branch_name]

Git Push

To push the changes to a remote repository.

Code
git push
git push -u origin [branch_name]

Push the changes to the branch.

Code
git push -all

Git Reset

To reset the uncommitted file

Code
git reset [file_name]

To undo all commits after a certain commit and preserve the changes in the working directory.

Code
git reset [commit]

To go back to the specified commit.

Code
git reset --soft [commit]
git reset --hard [commit]

Git Status

This status of files and list of pending commits.

Code
git status

Git Branch

This command gives a list of branches.

Code
git branch

To list all branches including local and remote.

Code
git branch -a

To create a new branch.

Code
git branch [branch_name]

To delete a branch.

Code
git branch -d [branch_name]

To delete the remote branch.

Code
git push origin --delete [branch_name]

To rename a local git branch.

Code
git branch -m [old_branch_name]  [new_branch_name]

Git Diff

To show the differences of files that are not committed yet.

Code
git diff -staged

Above command will show the differences in files in the staging area with the latest remote git repository.

To preview the changes between two branches before merging.

Code
git diff [one_branch_name] [another_branch_name]

Git Checkout

To switch the branch

Code
git checkout [branch_name]

To switch to last checked out.

Code
git checkout – [file_name]

Git Merge

To merge a specific branch into the current branch

Code
git merge [branch_name]

To merge one branch into another branch

Code
git merge [source_branch] [target_branch]

Git Stash

It changes in a dirty working directory

Code
git stash

Remove all stashed entries

Code
git stash clear

Git Log

To view the version or change history.

Code
git log

To view a summary in detail

Code
git log --summary

To view the log in brief

Code
git log --oneline

Conclusion

In this article, I have shared the most used git commands, especially for developers. There are several other commands available for git, however, above mentioned are the topmost and popular commands.