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
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.
git clone [URL]
Git Status
To check the status of git.
git status
Git Config
To configure git author with name and email.
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.
git add [file_name]
To add all modified files
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.
git rm [file_name]
git rm -r [file_name]
Git Commit
To commit changes into git version history.
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.
git pull
git pull [branch_name]
git pull orgin [branch_name]
Git Push
To push the changes to a remote repository.
git push
git push -u origin [branch_name]
Push the changes to the branch.
git push -all
Git Reset
To reset the uncommitted file
git reset [file_name]
To undo all commits after a certain commit and preserve the changes in the working directory.
git reset [commit]
To go back to the specified commit.
git reset --soft [commit]
git reset --hard [commit]
Git Status
This status of files and list of pending commits.
git status
Git Branch
This command gives a list of branches.
git branch
To list all branches including local and remote.
git branch -a
To create a new branch.
git branch [branch_name]
To delete a branch.
git branch -d [branch_name]
To delete the remote branch.
git push origin --delete [branch_name]
To rename a local git branch.
git branch -m [old_branch_name] [new_branch_name]
Git Diff
To show the differences of files that are not committed yet.
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.
git diff [one_branch_name] [another_branch_name]
Git Checkout
To switch the branch
git checkout [branch_name]
To switch to last checked out.
git checkout – [file_name]
Git Merge
To merge a specific branch into the current branch
git merge [branch_name]
To merge one branch into another branch
git merge [source_branch] [target_branch]
Git Stash
It changes in a dirty working directory
git stash
Remove all stashed entries
git stash clear
Git Log
To view the version or change history.
git log
To view a summary in detail
git log --summary
To view the log in brief
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.