{"id":1189,"date":"2022-05-24T13:10:22","date_gmt":"2022-05-24T13:10:22","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1189"},"modified":"2022-06-24T07:54:47","modified_gmt":"2022-06-24T07:54:47","slug":"frequently-used-git-commands","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/frequently-used-git-commands\/","title":{"rendered":"Frequently Used Git Commands"},"content":{"rendered":"\n<p><em>Git<\/em>&nbsp;is a free and open source distributed <strong>Version Control System (VCS)<\/strong> designed to handle everything from small to very large projects with speed and efficiency.  It&nbsp;is designed to track changes to a project (code) in software development. It is intended to enforce coordination, collaboration, speed, and efficiency among developers. <\/p>\n\n\n\n<p>Version control, also known as source control, is the process of tracking and managing changes to files over time. VCS are software tools designed to help teams work in parallel and manage source code.<\/p>\n\n\n\n<p>In this article I am going to explain how to work with Git and list out frequently used Git commands. Before moving to the git commands, let us first understand the basic git workflow:<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"351\" src=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2022\/05\/git-workflow.png\" alt=\"Git Worflow\" data-id=\"1212\" data-link=\"https:\/\/datatype.co.in\/blog\/frequently-used-git-commands\/git-workflow\/\" class=\"wp-image-1212\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2022\/05\/git-workflow.png 516w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2022\/05\/git-workflow-300x204.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/figure><\/li><\/ul>\n\n\n\n<p><strong>Step 1<\/strong>&nbsp;\u2212 You clone or download a copy of project source code from remote git repository to your local machine&#8217;s workspace.<\/p>\n\n\n\n<p><strong>Step 2<\/strong>&nbsp;\u2212 You modify, add or delete files in your downloaded copy of  project.<\/p>\n\n\n\n<p><strong>Step 3<\/strong>&nbsp;\u2212 You add these files to the git staging area.<\/p>\n\n\n\n<p><strong>Step 4<\/strong>&nbsp;\u2212 You perform commit operation that moves the files from the staging area.<\/p>\n\n\n\n<p><strong>Step 5<\/strong>&nbsp;\u2212 You perform push operation, it stores the changes permanently to the remote Git repository.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Configure Author Details And Other Settings: <\/h4>\n\n\n\n<p>It is imperative to configure author details in git before performing any git commit operation. <code>git config<\/code> command is used to set the author name and email address respectively.  This information is used by Git for each commit. Git stores all global configurations in&nbsp;<strong>.gitconfig<\/strong>&nbsp;file, which is located in your home directory . The usage is given below, we need to execute following two commands to set name and email address.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git config \u2013global user.name \"&lt;your-name>\"\ngit config \u2013global user.email \"&lt;your-email-address>\" \n\n# Example:\ngit config \u2013global user.name \"James Bond\"\ngit config \u2013global user.email \"jamesbond@example.com\" <\/code><\/pre>\n\n\n\n<p><strong>Configuration to avoid merge commits while pulling:<\/strong><\/p>\n\n\n\n<p>When we pull the latest changes from a remote repository, and if these changes are divergent, then by default Git creates merge commits. We can avoid this by using the following settings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git config --global branch.autosetuprebase always<\/code><\/pre>\n\n\n\n<p>To view all the settings, you can use <code>git config --list<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git config --list\n\n# Output example:\nuser.name=James Bond\nuser.email=jamesbond@example.com\nbranch.autosetuprebase=always\n...<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Cloning Code Repository: <\/h4>\n\n\n\n<p>Cloning code repository basically means downloading a copy of code repository or project source code from remote Git repository to our local machine.  <code>git clone<\/code> command used to clone remote repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone &lt;url>\n\n# Cloning Specific Branch:\ngit clone -b &lt;branch-name> &lt;url> \nOR \ngit clone --branch &lt;branch-name> &lt;url> \n\n# Example:\ngit clone https:\/\/github.com\/projects\/project-1.github.io.git\ngit clone -b new-idea https:\/\/github.com\/projects\/project-1.github.io.git<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Checking Local Code Changes: <\/h4>\n\n\n\n<p>Once project source code is cloned in our local machine,  we will start working on the project and do some changes to existing source code files or create new files as required to complete the given task or requirement. <code>git status<\/code> command is used to check the changes that has been made to project source code. This command will list all the modified, newly added and deleted files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git status<\/code><\/pre>\n\n\n\n<p>This command also shows the current branch name that we are working on.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Committing And Pushing Code Changes: <\/h4>\n\n\n\n<p>As soon as the task is completed and tested locally, we need to make the changes available to every one who is working on the same project. To do so, we need to add modified or new file(s) to git stage (<code>git add<\/code>), commit the changes (<code>git commit<\/code>) and then push to changes (<code>git push<\/code>) to remote repository. <\/p>\n\n\n\n<p><strong>4.1) Adding Files to Git Stage:<\/strong><\/p>\n\n\n\n<p> Adding file(s) to Git stage or Git staging area means saving a copy of the current state of the project.  This tells Git that you want to add the files to the next commit you do. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Adding single file to stage\ngit add &lt;file-name>\nExample:\ngit add dashboard.html\n\n# Adding multiple file to stage, as many you want\ngit add &lt;file-1> &lt;file-2> &lt;file-3>\nExample:\ngit add dashboard.html main.js style.css\n\n# Adding all files to stage\ngit add * \nOR\ngit add -A\nOR\ngit add .\nOR\n\n# Adding all files by extension\ngit add *.&lt;extension>\n# Example:\ngit add *.js\n\n<\/code><\/pre>\n\n\n\n<p><strong>4.2) Committing Changes:<\/strong><\/p>\n\n\n\n<p> The&nbsp;<strong>commit<\/strong>&nbsp;command is used to save changes to a local repository after staging in Git. We commit our changes with <code>git commit<\/code> command as shown below. It is good practice to add meaningful message to each commit  &nbsp;to identify your changes. We should describe what changes we are going to commit. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit -m \"Commit message that describes what this change does\"\n\/\/Example:\ngit commit -m \"TASK 123: Add trend pie chart in dashboard\"<\/code><\/pre>\n\n\n\n<p>Always follow the commit message convention defined by your team or follow the best practices.<\/p>\n\n\n\n<p>We can also add modified files to the staging area and commit them at the same time. Git provides the following super command: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit -a -m \"TASK 123: Add trend pie chart in dashboard\"<\/code><\/pre>\n\n\n\n<p><strong>4.3) Pushing Changes to Remote Repository:<\/strong><\/p>\n\n\n\n<p> The <code>git push<\/code> command is&nbsp;<strong>used to upload local repository content to a remote repository<\/strong>. Pushing is how you transfer commits from your local repository to a remote repository. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. Deleting or Reverting Local Changes:<\/h4>\n\n\n\n<p>If you have done some changes to source code and want to revert it back, then you can use <code>git reset<\/code> command to do so.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard<\/code><\/pre>\n\n\n\n<p>This will restore everything to recent.  It will remove the available changes from the staging area. It will also remove newly added files as well.<\/p>\n\n\n\n<p>If you want to keep the changes, but remove from staging area, use <code>--soft<\/code> option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft<\/code><\/pre>\n\n\n\n<p>For more details on git reset read  <a href=\"https:\/\/datatype.co.in\/blog\/git-reset-vs-git-revert\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Git Reset Vs Git Revert (opens in a new tab)\">Git Reset Vs Git Revert<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6. Pulling Changes:<\/h4>\n\n\n\n<p>Pulling is basically opposite to pushing. When we do a ,code pull, we get all the changes that has been made by other developers in the project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git pull<\/code><\/pre>\n\n\n\n<p><strong>Note: It is good practice to pull code before you commit any changes. Otherwise most of the time you will face git error.<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7. Create, List, Switch, Delete, Merge Branch: <\/h4>\n\n\n\n<p>The <code>git checkout<\/code> command is&nbsp;used to switch from one branch to another in a repository and to create a new branch as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Switch to another branch\ngit checkout &lt;branch-name>\n\n# Create and switch to new branch\ngit checkout -b &lt;new-branch-name>\n\n# Example: Switched to another branch 'release-2'\ngit checkout &lt;release-2>\n\n# Example: Create and switch to new branch 'release-3'\ngit checkout -b release-3<\/code><\/pre>\n\n\n\n<p><code>git branch<\/code> command is used to list all the branches in the working repository. It is also used to create new branch, delete existing branch from repository and merge one branch with another. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># List all branches\ngit branch\n\n# Creates a new branch\ngit branch &lt;new-branch-name>\n\n# Delete branch\ngit branch -d &lt;branch-name>\n\n# Merge Branch\ngit merge &lt;target-branch-name>\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>To merge branches, you should first checkout the source branch (where you want me merge) and pull so that you have all latest code in the same. Then run the merge command. Don&#8217;t forget to create a <a rel=\"noreferrer noopener\" aria-label=\"git tag (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/git-tag-all-you-need-to-know\/\" target=\"_blank\">git tag<\/a> before you merge.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">8. Stashing and Unstashing Changes:<\/h4>\n\n\n\n<p> <code>git stash<\/code> command locally stores all the most recent changes (backup all changes made) in a workspace and resets the state of the workspace to the prior commit state. A user can retrieve all files put into the stash with the <code>git stash pop<\/code> and <code>git stash apply<\/code> commands. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Backup all your local changes in the workspace\ngit stash\n\n# Restore stashed changes or unstash\ngit stash apply\nOR\ngit stash pop\n\n# lists all stashed changesets\ngit stash list  \n\n# Discard the most recently stashed changeset\ngit stash drop<\/code><\/pre>\n\n\n\n<p><strong>Tip:<\/strong> Stashing can be used to prevent merge conflict if applied wisely. Let&#8217;s consider a very common scenario &#8211; You want to pull latest code, but you have some uncommitted changes locally. Git is not allowing you to pull code unless you commit your changes. What should you do? Yes, use Git stash feature.  Stash the changes first, then do a git pull and finally do unstash to restore and merge the stashed code. If you face any merge conflict after unstashing, resolve it manually and then commit and push your changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Backup your changes\ngit stash \n\n# Pull latest code\ngit pull\n\n# Restore backed up changes\ngit unstash apply\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9. Fetching Changes:<\/h4>\n\n\n\n<p>You can use&nbsp;<code>git fetch<\/code>&nbsp;to know the changes done in the remote repo\/branch since your last pull. This is useful to allow for checking before doing an actual pull, which could change files in your current branch and working copy (and potentially lose your changes, etc.). <\/p>\n\n\n\n<p><strong>How git fetch is different from git pull?<\/strong><\/p>\n\n\n\n<p><code> git fetch<\/code> is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn&#8217;t do any file transferring. It&#8217;s more like just checking to see if there are any changes available). <code>git pull<\/code> on the other hand does that AND brings (copy) those changes from the remote repository. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">10. Remove File(s) From Staging Area:<\/h4>\n\n\n\n<p>If you have accidentally added some file(s) to git stage and do not want it to commit, you can use <code>git restore<\/code> command to remove it from staging area.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Remove a specific file from the staging area\ngit restore --staged &lt;file-name>\n\n# Remove all the files that are currently staged\ngit restore --staged .\n\n# Alternatively, you can use git reset command\ngit reset &lt;file-name> <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> 11. Revert Committed Changes : <\/h4>\n\n\n\n<p>Many times for different reasons, we might need to revert committed changes. Let&#8217;s say you pushed some new changes (feature or bug fix) and it did not work as expected or it is breaking some other functionality. In such cases, we need to revert the changes immediately. There are two ways to do it &#8211;<\/p>\n\n\n\n<p>a) One way is manually revert the changes and then commit and push the code. If the changes are small like just one file was modified or added just few lines of code then this is feasible. But if the changes are huge, manually reverting will be hectic job. <\/p>\n\n\n\n<p>b) Another way is using <code>git revert<\/code> command. This command is life saver! To revert a particular commit, following command can be used-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert &lt;commit-hash><\/code><\/pre>\n\n\n\n<p>You need to pass the commit hash of the commit you want to revert. It can be picked from git log. To view git log i.e. git commit history, just run <code>git log<\/code> command-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline<\/code><\/pre>\n\n\n\n<p> For more details on git revert read  <a rel=\"noreferrer noopener\" href=\"https:\/\/datatype.co.in\/blog\/git-reset-vs-git-revert\/\" target=\"_blank\">Git Reset Vs Git Revert<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cheat Sheet<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Git Configuration\n#-------------------\n\ngit config \u2013global user.name \"James Bond\"\ngit config \u2013global user.email \"jamesbond@example.com\" \ngit config --global color.ui true\ngit config --list\n\n# Starting a Repository\n#-------------------\n\ngit init\ngit status\n\n# Stagging Files\n#-------------------\n\ngit add dashboard.html\ngit add dashboard.html main.js style.css\ngit add .\ngit add *\ngit add --all\ngit add *.js\n\n\/\/ Remove staged file\ngit rm --cached my-file.ts\ngit reset another-file.js\n\n\/\/ Revert local changes\ngit reset --hard\ngit reset --soft\n\n# Committing \n#-------------------\n\ngit commit -m \"TASK 123: Add trend pie chart in dashboard\"\n\n\/\/ Add modified file(s) to stage and commit at the same time\ngit commit -a -m \"TASK 123: Add trend pie chart in dashboard\"\n\n# Pulling &amp; Pushing From\/To Remote Repository\n#-------------------\n\n\/\/ Create local repository by cloning remote repository \ngit clone git@github.com:YourUsername\/your-app.git\n\n\/\/ Add remote origin to already started\/created local git repository\ngit remote add origin https:\/\/github.com\/YourUsername\/some-small-app.git\n\n\/\/ View remote origin (remote repository) url\ngit remote -v\n\ngit push\ngit pull\n\n# Reverting Commit\n#-------------------\n\ngit revert &lt;commit-hash>\n\n# Resetting (Deleting) Commits\n\/\/Should not be done in public repository\n\n\/\/ Delete last commit\ngit reset --hard Head~1\n\n\/\/ Reset to a particular commit\ngit reset --hard &lt;commit-hash>\ngit reset --soft &lt;commit-hash>\n\n# Branching\n#-------------------\n\ngit branch\ngit branch &lt;branch-name>\ngit checkout &lt;branch-name>\n\n\/\/ Create branch and checkout at the same time\ngit checkout -b &lt;branch-name>\n\ngit fetch\ngit merge &lt;target-branch-name>\ngit branch -d &lt;branch-name>\n\n# Checking Git Commit history OR Log\n#-------------------\n\ngit log\ngit log --stat\ngit log --oneline\ngit log --graph \ngit log --graph --oneline\ngit log --patch or git log -p\n\n<\/code><\/pre>\n\n\n\n<p>To learn more on frequently used git commands read <a href=\"https:\/\/datatype.co.in\/blog\/frequently-used-git-commands\/\">Frequently Used Git Commands<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git&nbsp;is a free and open source distributed Version Control System (VCS) designed to handle everything from small to very large projects with speed and efficiency. It&nbsp;is designed to track changes&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266,265],"tags":[264,263],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","category-git","category-vcs","tag-git","tag-git-commands","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1189"}],"version-history":[{"count":52,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1189\/revisions"}],"predecessor-version":[{"id":1318,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1189\/revisions\/1318"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}