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 along with is roll back 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 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 in 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.
Below are the top most git commands for programmers and developers.
To initialize a new git repository. Namely, to start git repository
git init git init [repository name]
simply navigate to folder which you want to add in git repository and run this command.
This command is to create a local copy of a remote git repository through URL.
git clone [URL] Git Status
To check the status of git.
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]”
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.
To remove the file or folder from your working directory and stages deletion.
git rm [file_name] git rm -r [file_name]
To commit changes into git version history.
git commit -m “commit message” git commit -a
Above commit command will commits all the files you have added via git or modified.
To fetch and merge the latest commit from remote server to your local working directory.
git pull git pull [branch_name] git pull orgin [branch_name]
To push the changes to remote repository.
git push git push -u origin [branch_name]
Push the changes to the branch.
git push -all
To reset the uncommitted file
git reset [file_name]
To undo all commits after certain commit and preserve the changes in working directory.
git reset [commit]
To go back to specified commit.
git reset --soft [commit] git reset --hard [commit]
This status of files and list of pending commits.
This command gives list of branches.
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 remote branch.
git push origin --delete [branch_name]
To rename a local git branch.
git branch -m [old_branch_name] [new_branch_name]
To show the differences of file which are not committed yet.
git diff -staged
Above command will show the differences in files in the staging area with latest remote git repository.
To preview the changes between two branches before merging.
git diff [one_branch_name] [another_branch_name]
To switch the branch
git checkout [branch_name]
To switch to last checked out
git checkout – [file_name]
To merge specific branch into current branch
git merge [branch_name]
To merge one branch into another branch
git merge [source_branch] [target_branch]
It changes in a dirty working directory
Remove all stashed entries
git stash clear
To view the version or change history.
To view summary in detail
git log --summary
To view log in brief
git log --oneline
In this article, I have shared most used git commands specially for developers. There are several other commands available for git, however, above mentioned are top most and popular commands.