{"id":10166,"date":"2017-05-22T14:00:00","date_gmt":"2017-05-22T14:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=10166"},"modified":"2019-02-14T20:23:44","modified_gmt":"2019-02-15T03:23:44","slug":"practical-git-commands-kickstart","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/practical-git-commands-kickstart\/","title":{"rendered":"Practical Git Commands Kickstart"},"content":{"rendered":"<p>This very useful guide to Git commands comes to us from Premier Developer consultant <a href=\"https:\/\/www.linkedin.com\/in\/crystal-tenn-6a0b9b67\/\" target=\"_blank\">Crystal Tenn<\/a>.<\/p>\n<hr>\n<p><a href=\"https:\/\/git-scm.com\/\" target=\"_blank\">Git<\/a> command line is extremely useful for managing your resources \u2013 it is the only way to utilize all Git commands possible as most GUI\u2019s only have a set of available commands. Although many guides exist to explain Git command line, many are brief introductions or extremely comprehensive. I will share a practical guide of commands I needed to use to get through my day, no more, no less as part of a kick start to get you going with Git. I will list commands by group in tables and will give an explanation below each table in italics for why\/when you need these commands if you are new to Git. <\/p>\n<p>&nbsp;<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"733\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><b>Project\/Directory Setup Commands<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git init<b><\/b><\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>New repository from current directory<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git init &lt;directory&gt;<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>New repository to a specific directory<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git clone \/path\/to\/repository<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Connecting to an existing local repository<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git clone username@host:\/path\/to\/repository<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Connecting to an existing remote repository<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>You need initialize a local file directory to be a Git repository for a new project (your local file directory will be associated with a certain online repository). Initializing will create a .git folder in the directory and this is where Git will record different versions of the project and the .git folder will contain all of the metadata and changes to the project (rest of the project will not be altered while you are working).<\/i><\/p>\n<p><em><\/em>&nbsp;<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"731\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><b>Adding\/Staging\/Unstaging Files Commands<\/b><b><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git add filename.txt<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Stages a specific file to commit<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git add -A<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Stages all<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git add .<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Stages new and modified, without deleted. <b><i>*This add command is most commonly used<\/i><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git add -u<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Stages modified and deleted, without new<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git add *<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Stages all files in current directory except files whose name begin with a dot. Wildcard interpreted as part of git.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git reset &#8212; filename.txt<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Unstages a file by name, the changes are still there that you made but it will not be committed until you add it back<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git reset<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Unstages all files (again changes are still there and now nothing is committed until you add it in)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"362\">\n<p><font face=\"Consolas\">git status<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"367\">\n<p>Checking file status (see which files are staged\/unstaged for the commit)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>Staging and unstaging do not affect the changes of the files or delete any files, they are for adding\/removing files to track for a commit or stash. You push your commits to make a change. In Git, you stage(add) the files you want to commit and unstage (reset) files you do not want to commit. Note \u2018&#8211;\u2019 in a command specifies we are talking about a file, not a branch (in case some file names are the same as a branch name).<\/i>  <\/p>\n<p><em><\/em>&nbsp; <\/p>\n<p><i><\/i> <\/p>\n<p><i><\/i> <\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"736\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><b>Commit\/Reset Commands<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git commit -m \u201cYour message here to describe what you are committing to your branch.\u201d<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Commit changes with a message<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git checkout &lt;file&gt;<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Abandon\/Undo changes to one file<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git reset &#8211;hard HEAD~1<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Removing the most recent one commit (HEAD~1 means the commit before the head)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git log<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Find a commit id<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git reset &#8211;hard &lt;sha1-commit-id&gt;<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Removing a very specific commit id<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"365\">\n<p><font face=\"Consolas\">git reset &#8211;hard<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"369\">\n<p>Removing all commits\/changes and reset back to original branch pulled<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>Committing is not permanent, do not be afraid to commit. You don\u2019t have to push your commit. You can roll back this commit easily if you want to erase all of it and go back to the original code in the branch. You must commit or stash everything before you can change branches, pull new commits in, etc. (see final table for stashing info).<\/i><\/p>\n<p><em><\/em>&nbsp;<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"739\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><b>Push\/Pull Commands<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p><b>Use\/<b>Explanation<\/b><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git push<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p>Pushing changes from current branch to the same current branch<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git push originally-cloned-branch new-branch<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p>Pushing changes from an originally cloned branch to a new branch<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git fetch<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p>Fetching commits and remote branch changes, does not put new changes into your code until you pull though<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"364\">\n<p><font face=\"Consolas\">git pull<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"373\">\n<p>Pulls all the latest commits to your current branch from the server<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>You must run a <\/i><b><i>git fetch<\/i><\/b><i> to see new commits or any new branches added to your repository. <b>Git pull<\/b> does a <b>git fetch + git merge<\/b>. You can <b>git fetch<\/b> anytime to update remote tracking branches or see any latest commits from others. If you already pushed a commit and need to revert it, you can do a force push to get rid of it but honestly it is safer for you to create a new branch and fix your changes as others are working in the same code (pushing is a little permanent, make sure you test everything out before you push your changes to the server where other developers will see your work).<\/i> <\/p>\n<p><em><\/em>&nbsp; <\/p>\n<p><i><\/i> <\/p>\n<p><i><\/i> <\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"737\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"370\">\n<p><b>Switching\/Creating Branches<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"365\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"370\">\n<p><font face=\"Consolas\">git checkout -b my-new-branch-name<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"365\">\n<p>Create a new branch based on your current branch, and switch to the new branch. <i>Note the -b means that you need to create a new branch<\/i><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"370\">\n<p><font face=\"Consolas\">git checkout -b new-branch existing-branch<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"365\">\n<p>Create a new branch based on a specific branch<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"370\">\n<p><font face=\"Consolas\">git checkout existing-branch-name<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"365\">\n<p>Switching to another existing branch<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>Make sure you commit or stash your changes before trying to change branches. Note: you are able to swap between different branches with one code same base\u2014all of the changes that appear are applied based on the contents of the .git folder on top of the original files. <\/i> <\/p>\n<p><em><\/em>&nbsp; <\/p>\n<p><i><\/i> <\/p>\n<p><i><\/i> <\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"736\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"371\">\n<p><b>Merging Branches Commands<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"363\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"371\">\n<p><font face=\"Consolas\">git merge branchname<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"363\">\n<p>Merges <i>branchname<\/i> into your current branch. Make sure to pull latest into both your current branch and the branch to merge in<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"371\">\n<p><font face=\"Consolas\">git reset &#8211;merge .<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"363\">\n<p>Abandon a merge with merge conflicts, goes back to state you were in before you attempted to merge. Can use this command if Git v1.6.1+<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"371\">\n<p><font face=\"Consolas\">git reset &#8211;hard HEAD<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"363\">\n<p>If Git version is less than 1.6.1 then use this to abandon a merge<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>You merge one branch into another one, for example if you are working in a feature branch and want to put your work into develop.. or if you want to merge stable develop changes into QA. Careful with merge conflicts and to test after fixing all of them before committing and finally pushing to the server.<\/i><\/p>\n<p><em><\/em>&nbsp;<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"737\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><b>Dealing with Stashes Commands<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p><b>Use\/Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Stash all tracked files without a message\/description to index 0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash save \u201cmy description here\u201d<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Stash all tracked files that have not yet been committed<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash save -u \u201cmy description here\u201d<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Stash all files that have not yet been committed <i>including<\/i> untracked files<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash pop<\/font>  <\/p>\n<p><font face=\"Consolas\">-or- <\/font> <\/p>\n<p><font face=\"Consolas\">git stash apply<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>All changes to stashed files will be applied to the current workspace (unstashing). You can reapply to same branch or apply changes to a new branch. By default gets stash@{0}. <b><u>Important: <\/u><\/b><b>git stash pop throws away the stash after applying it, whereas git stash apply leaves it in the stash list for possible later reuse.<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash pop stash@{2} <br>-or- <br>git stash apply stash@{2}<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Specifies which index to apply\/unstash to your current workspace<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash list<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>List the current stashes, you can see the index of each one to delete a certain one<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash clear<\/font> <\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Clear\/Delete all the current stashes<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"373\">\n<p><font face=\"Consolas\">git stash drop stash@{0}<\/font><\/p>\n<\/td>\n<td valign=\"top\" width=\"362\">\n<p>Replace {0} with the index of the stash you want to drop<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><i>Use git add\/reset to determine which files you want to stash. You can stash changes as a \u201csave-state\u201d so you can do work in another branch, all changes will be saved in your local Git repo and nothing from the stash will go to the server. You can easily unstash your changes onto the same or another branch. You can apply one stash to multiple other branches. Sometimes you may make a feature branch and make a lot of foundational domain changes.. then suddenly you realize another developer needs your changes to work. You can stash your changes and unstash them on top of the other branch so you are both working in the same branch with shared foundational code. New git stashes by default are always created to stash@{0}, and older ones\u2019 will have the index pushed to higher index numbers.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This very useful guide to Git commands comes to us from Premier Developer consultant Crystal Tenn. Git command line is extremely useful for managing your resources \u2013 it is the only way to utilize all Git commands possible as most GUI\u2019s only have a set of available commands. Although many guides exist to explain Git [&hellip;]<\/p>\n","protected":false},"author":581,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[76,43],"class_list":["post-10166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-crystal-tenn","tag-git"],"acf":[],"blog_post_summary":"<p>This very useful guide to Git commands comes to us from Premier Developer consultant Crystal Tenn. Git command line is extremely useful for managing your resources \u2013 it is the only way to utilize all Git commands possible as most GUI\u2019s only have a set of available commands. Although many guides exist to explain Git [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/10166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=10166"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/10166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=10166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=10166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=10166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}