snipt

Ctrl+h for KB shortcuts

Text only

Git Major Commands

Major GIT commands:

git config
Sets configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more.

Example: git config --global user.name "My Name"
                 git config --global user.email "[email protected]"

cat ~/.gitconfig
[user]
    name = My Name
    email = [email protected]

git init
Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project.

Example: cd /home/user/my_new_git_folder/
git init

 

git clone
Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.

Example: git clone [email protected]:user/test.git

git add
Adds files changes in your working directory to your index.

Example: git add .

git rm
Removes files from your index and your working directory so they will not be tracked.

Example: git rm filename

git commit
Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.

Examples: git commit -m ‘committing added changes’
git commit -a -m ‘committing all changes, equals to git add and git commit’

git status
Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example: git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    README
nothing added to commit but untracked files present (use "git add" to track)

git branch
Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.

Example: git branch -a
* master
  remotes/origin/master

git checkout
Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.

Example: git checkout newbranch

git merge
Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.

Example: git merge newbranchversion

git reset
Resets your index and working directory to the state of your last commit.

Example: git reset --hard HEAD

git stash
Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.

Example: git stash
Saved working directory and index state "WIP on master: 84f241e first commit"
HEAD is now at 84f241e first commit
(To restore them type "git stash apply")

git tag
Tags a specific commit with a simple, human readable handle that never moves.

Example: git tag -a v1.0 -m 'this is version 1.0 tag'

git fetch
Fetches all the objects from the remote repository that are not present in the local one.

Example: git fetch origin

git pull
Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence.

Example: git pull origin

git push
Pushes all the modified local objects to the remote repository and advances its branches.

Example: git push origin master

git remote
Shows all the remote versions of your repository.

Example: git remote
origin

git log
Shows a listing of commits on a branch including the corresponding details.

Example: git log
commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <[email protected]>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

git show
Shows information about a git object.

Example: git show
commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <[email protected]>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

diff --git a/README b/README
new file mode 100644
index 0000000..e69de29

git ls-tree
Shows a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to.

Example: git ls-tree master^{tree}
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    README

git cat-file
Used to view the type of an object through the SHA-1 value.

Example: git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
blob

git grep
Lets you search through your trees of content for words and phrases.

Example: git grep "www.siteground.com" -- *.php

git diff
Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.

Example: git diff

gitk
https://snipt.net/embed/2cc93389f6312ddb915df0a24382a260/
/raw/2cc93389f6312ddb915df0a24382a260/
2cc93389f6312ddb915df0a24382a260
text
Text only
159
2019-07-21T04:48:27
True
False
False
Apr 24, 2013 at 06:52 PM
/api/public/snipt/59940/
git-major-commands
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10"> 10</a> <a href="#L-11"> 11</a> <a href="#L-12"> 12</a> <a href="#L-13"> 13</a> <a href="#L-14"> 14</a> <a href="#L-15"> 15</a> <a href="#L-16"> 16</a> <a href="#L-17"> 17</a> <a href="#L-18"> 18</a> <a href="#L-19"> 19</a> <a href="#L-20"> 20</a> <a href="#L-21"> 21</a> <a href="#L-22"> 22</a> <a href="#L-23"> 23</a> <a href="#L-24"> 24</a> <a href="#L-25"> 25</a> <a href="#L-26"> 26</a> <a href="#L-27"> 27</a> <a href="#L-28"> 28</a> <a href="#L-29"> 29</a> <a href="#L-30"> 30</a> <a href="#L-31"> 31</a> <a href="#L-32"> 32</a> <a href="#L-33"> 33</a> <a href="#L-34"> 34</a> <a href="#L-35"> 35</a> <a href="#L-36"> 36</a> <a href="#L-37"> 37</a> <a href="#L-38"> 38</a> <a href="#L-39"> 39</a> <a href="#L-40"> 40</a> <a href="#L-41"> 41</a> <a href="#L-42"> 42</a> <a href="#L-43"> 43</a> <a href="#L-44"> 44</a> <a href="#L-45"> 45</a> <a href="#L-46"> 46</a> <a href="#L-47"> 47</a> <a href="#L-48"> 48</a> <a href="#L-49"> 49</a> <a href="#L-50"> 50</a> <a href="#L-51"> 51</a> <a href="#L-52"> 52</a> <a href="#L-53"> 53</a> <a href="#L-54"> 54</a> <a href="#L-55"> 55</a> <a href="#L-56"> 56</a> <a href="#L-57"> 57</a> <a href="#L-58"> 58</a> <a href="#L-59"> 59</a> <a href="#L-60"> 60</a> <a href="#L-61"> 61</a> <a href="#L-62"> 62</a> <a href="#L-63"> 63</a> <a href="#L-64"> 64</a> <a href="#L-65"> 65</a> <a href="#L-66"> 66</a> <a href="#L-67"> 67</a> <a href="#L-68"> 68</a> <a href="#L-69"> 69</a> <a href="#L-70"> 70</a> <a href="#L-71"> 71</a> <a href="#L-72"> 72</a> <a href="#L-73"> 73</a> <a href="#L-74"> 74</a> <a href="#L-75"> 75</a> <a href="#L-76"> 76</a> <a href="#L-77"> 77</a> <a href="#L-78"> 78</a> <a href="#L-79"> 79</a> <a href="#L-80"> 80</a> <a href="#L-81"> 81</a> <a href="#L-82"> 82</a> <a href="#L-83"> 83</a> <a href="#L-84"> 84</a> <a href="#L-85"> 85</a> <a href="#L-86"> 86</a> <a href="#L-87"> 87</a> <a href="#L-88"> 88</a> <a href="#L-89"> 89</a> <a href="#L-90"> 90</a> <a href="#L-91"> 91</a> <a href="#L-92"> 92</a> <a href="#L-93"> 93</a> <a href="#L-94"> 94</a> <a href="#L-95"> 95</a> <a href="#L-96"> 96</a> <a href="#L-97"> 97</a> <a href="#L-98"> 98</a> <a href="#L-99"> 99</a> <a href="#L-100">100</a> <a href="#L-101">101</a> <a href="#L-102">102</a> <a href="#L-103">103</a> <a href="#L-104">104</a> <a href="#L-105">105</a> <a href="#L-106">106</a> <a href="#L-107">107</a> <a href="#L-108">108</a> <a href="#L-109">109</a> <a href="#L-110">110</a> <a href="#L-111">111</a> <a href="#L-112">112</a> <a href="#L-113">113</a> <a href="#L-114">114</a> <a href="#L-115">115</a> <a href="#L-116">116</a> <a href="#L-117">117</a> <a href="#L-118">118</a> <a href="#L-119">119</a> <a href="#L-120">120</a> <a href="#L-121">121</a> <a href="#L-122">122</a> <a href="#L-123">123</a> <a href="#L-124">124</a> <a href="#L-125">125</a> <a href="#L-126">126</a> <a href="#L-127">127</a> <a href="#L-128">128</a> <a href="#L-129">129</a> <a href="#L-130">130</a> <a href="#L-131">131</a> <a href="#L-132">132</a> <a href="#L-133">133</a> <a href="#L-134">134</a> <a href="#L-135">135</a> <a href="#L-136">136</a> <a href="#L-137">137</a> <a href="#L-138">138</a> <a href="#L-139">139</a> <a href="#L-140">140</a> <a href="#L-141">141</a> <a href="#L-142">142</a> <a href="#L-143">143</a> <a href="#L-144">144</a> <a href="#L-145">145</a> <a href="#L-146">146</a> <a href="#L-147">147</a> <a href="#L-148">148</a> <a href="#L-149">149</a> <a href="#L-150">150</a> <a href="#L-151">151</a> <a href="#L-152">152</a> <a href="#L-153">153</a> <a href="#L-154">154</a> <a href="#L-155">155</a> <a href="#L-156">156</a> <a href="#L-157">157</a> <a href="#L-158">158</a> <a href="#L-159">159</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>Major GIT commands: </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a>git config </span><span id="L-4"><a name="L-4"></a>Sets configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more. </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a>Example: git config --global user.name &quot;My Name&quot; </span><span id="L-7"><a name="L-7"></a> git config --global user.email &quot;[email protected]&quot; </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a>cat ~/.gitconfig </span><span id="L-10"><a name="L-10"></a>[user] </span><span id="L-11"><a name="L-11"></a> name = My Name </span><span id="L-12"><a name="L-12"></a> email = [email protected] </span><span id="L-13"><a name="L-13"></a> </span><span id="L-14"><a name="L-14"></a>git init </span><span id="L-15"><a name="L-15"></a>Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project. </span><span id="L-16"><a name="L-16"></a> </span><span id="L-17"><a name="L-17"></a>Example: cd /home/user/my_new_git_folder/ </span><span id="L-18"><a name="L-18"></a>git init </span><span id="L-19"><a name="L-19"></a> </span><span id="L-20"><a name="L-20"></a> </span><span id="L-21"><a name="L-21"></a> </span><span id="L-22"><a name="L-22"></a>git clone </span><span id="L-23"><a name="L-23"></a>Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions. </span><span id="L-24"><a name="L-24"></a> </span><span id="L-25"><a name="L-25"></a>Example: git clone [email protected]:user/test.git </span><span id="L-26"><a name="L-26"></a> </span><span id="L-27"><a name="L-27"></a>git add </span><span id="L-28"><a name="L-28"></a>Adds files changes in your working directory to your index. </span><span id="L-29"><a name="L-29"></a> </span><span id="L-30"><a name="L-30"></a>Example: git add . </span><span id="L-31"><a name="L-31"></a> </span><span id="L-32"><a name="L-32"></a>git rm </span><span id="L-33"><a name="L-33"></a>Removes files from your index and your working directory so they will not be tracked. </span><span id="L-34"><a name="L-34"></a> </span><span id="L-35"><a name="L-35"></a>Example: git rm filename </span><span id="L-36"><a name="L-36"></a> </span><span id="L-37"><a name="L-37"></a>git commit </span><span id="L-38"><a name="L-38"></a>Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit. </span><span id="L-39"><a name="L-39"></a> </span><span id="L-40"><a name="L-40"></a>Examples: git commit -m ‘committing added changes’ </span><span id="L-41"><a name="L-41"></a>git commit -a -m ‘committing all changes, equals to git add and git commit’ </span><span id="L-42"><a name="L-42"></a> </span><span id="L-43"><a name="L-43"></a>git status </span><span id="L-44"><a name="L-44"></a>Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing). </span><span id="L-45"><a name="L-45"></a> </span><span id="L-46"><a name="L-46"></a>Example: git status </span><span id="L-47"><a name="L-47"></a># On branch master </span><span id="L-48"><a name="L-48"></a># </span><span id="L-49"><a name="L-49"></a># Initial commit </span><span id="L-50"><a name="L-50"></a># </span><span id="L-51"><a name="L-51"></a># Untracked files: </span><span id="L-52"><a name="L-52"></a># (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed) </span><span id="L-53"><a name="L-53"></a># </span><span id="L-54"><a name="L-54"></a># README </span><span id="L-55"><a name="L-55"></a>nothing added to commit but untracked files present (use &quot;git add&quot; to track) </span><span id="L-56"><a name="L-56"></a> </span><span id="L-57"><a name="L-57"></a>git branch </span><span id="L-58"><a name="L-58"></a>Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided. </span><span id="L-59"><a name="L-59"></a> </span><span id="L-60"><a name="L-60"></a>Example: git branch -a </span><span id="L-61"><a name="L-61"></a>* master </span><span id="L-62"><a name="L-62"></a> remotes/origin/master </span><span id="L-63"><a name="L-63"></a> </span><span id="L-64"><a name="L-64"></a>git checkout </span><span id="L-65"><a name="L-65"></a>Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch. </span><span id="L-66"><a name="L-66"></a> </span><span id="L-67"><a name="L-67"></a>Example: git checkout newbranch </span><span id="L-68"><a name="L-68"></a> </span><span id="L-69"><a name="L-69"></a>git merge </span><span id="L-70"><a name="L-70"></a>Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts. </span><span id="L-71"><a name="L-71"></a> </span><span id="L-72"><a name="L-72"></a>Example: git merge newbranchversion </span><span id="L-73"><a name="L-73"></a> </span><span id="L-74"><a name="L-74"></a>git reset </span><span id="L-75"><a name="L-75"></a>Resets your index and working directory to the state of your last commit. </span><span id="L-76"><a name="L-76"></a> </span><span id="L-77"><a name="L-77"></a>Example: git reset --hard HEAD </span><span id="L-78"><a name="L-78"></a> </span><span id="L-79"><a name="L-79"></a>git stash </span><span id="L-80"><a name="L-80"></a>Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later. </span><span id="L-81"><a name="L-81"></a> </span><span id="L-82"><a name="L-82"></a>Example: git stash </span><span id="L-83"><a name="L-83"></a>Saved working directory and index state &quot;WIP on master: 84f241e first commit&quot; </span><span id="L-84"><a name="L-84"></a>HEAD is now at 84f241e first commit </span><span id="L-85"><a name="L-85"></a>(To restore them type &quot;git stash apply&quot;) </span><span id="L-86"><a name="L-86"></a> </span><span id="L-87"><a name="L-87"></a>git tag </span><span id="L-88"><a name="L-88"></a>Tags a specific commit with a simple, human readable handle that never moves. </span><span id="L-89"><a name="L-89"></a> </span><span id="L-90"><a name="L-90"></a>Example: git tag -a v1.0 -m &#39;this is version 1.0 tag&#39; </span><span id="L-91"><a name="L-91"></a> </span><span id="L-92"><a name="L-92"></a>git fetch </span><span id="L-93"><a name="L-93"></a>Fetches all the objects from the remote repository that are not present in the local one. </span><span id="L-94"><a name="L-94"></a> </span><span id="L-95"><a name="L-95"></a>Example: git fetch origin </span><span id="L-96"><a name="L-96"></a> </span><span id="L-97"><a name="L-97"></a>git pull </span><span id="L-98"><a name="L-98"></a>Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence. </span><span id="L-99"><a name="L-99"></a> </span><span id="L-100"><a name="L-100"></a>Example: git pull origin </span><span id="L-101"><a name="L-101"></a> </span><span id="L-102"><a name="L-102"></a>git push </span><span id="L-103"><a name="L-103"></a>Pushes all the modified local objects to the remote repository and advances its branches. </span><span id="L-104"><a name="L-104"></a> </span><span id="L-105"><a name="L-105"></a>Example: git push origin master </span><span id="L-106"><a name="L-106"></a> </span><span id="L-107"><a name="L-107"></a>git remote </span><span id="L-108"><a name="L-108"></a>Shows all the remote versions of your repository. </span><span id="L-109"><a name="L-109"></a> </span><span id="L-110"><a name="L-110"></a>Example: git remote </span><span id="L-111"><a name="L-111"></a>origin </span><span id="L-112"><a name="L-112"></a> </span><span id="L-113"><a name="L-113"></a>git log </span><span id="L-114"><a name="L-114"></a>Shows a listing of commits on a branch including the corresponding details. </span><span id="L-115"><a name="L-115"></a> </span><span id="L-116"><a name="L-116"></a>Example: git log </span><span id="L-117"><a name="L-117"></a>commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe </span><span id="L-118"><a name="L-118"></a>Author: User &lt;[email protected]&gt; </span><span id="L-119"><a name="L-119"></a>Date: Mon May 3 09:24:05 2010 +0300 </span><span id="L-120"><a name="L-120"></a> </span><span id="L-121"><a name="L-121"></a> first commit </span><span id="L-122"><a name="L-122"></a> </span><span id="L-123"><a name="L-123"></a>git show </span><span id="L-124"><a name="L-124"></a>Shows information about a git object. </span><span id="L-125"><a name="L-125"></a> </span><span id="L-126"><a name="L-126"></a>Example: git show </span><span id="L-127"><a name="L-127"></a>commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe </span><span id="L-128"><a name="L-128"></a>Author: User &lt;[email protected]&gt; </span><span id="L-129"><a name="L-129"></a>Date: Mon May 3 09:24:05 2010 +0300 </span><span id="L-130"><a name="L-130"></a> </span><span id="L-131"><a name="L-131"></a> first commit </span><span id="L-132"><a name="L-132"></a> </span><span id="L-133"><a name="L-133"></a>diff --git a/README b/README </span><span id="L-134"><a name="L-134"></a>new file mode 100644 </span><span id="L-135"><a name="L-135"></a>index 0000000..e69de29 </span><span id="L-136"><a name="L-136"></a> </span><span id="L-137"><a name="L-137"></a>git ls-tree </span><span id="L-138"><a name="L-138"></a>Shows a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to. </span><span id="L-139"><a name="L-139"></a> </span><span id="L-140"><a name="L-140"></a>Example: git ls-tree master^{tree} </span><span id="L-141"><a name="L-141"></a>100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 README </span><span id="L-142"><a name="L-142"></a> </span><span id="L-143"><a name="L-143"></a>git cat-file </span><span id="L-144"><a name="L-144"></a>Used to view the type of an object through the SHA-1 value. </span><span id="L-145"><a name="L-145"></a> </span><span id="L-146"><a name="L-146"></a>Example: git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 </span><span id="L-147"><a name="L-147"></a>blob </span><span id="L-148"><a name="L-148"></a> </span><span id="L-149"><a name="L-149"></a>git grep </span><span id="L-150"><a name="L-150"></a>Lets you search through your trees of content for words and phrases. </span><span id="L-151"><a name="L-151"></a> </span><span id="L-152"><a name="L-152"></a>Example: git grep &quot;www.siteground.com&quot; -- *.php </span><span id="L-153"><a name="L-153"></a> </span><span id="L-154"><a name="L-154"></a>git diff </span><span id="L-155"><a name="L-155"></a>Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory. </span><span id="L-156"><a name="L-156"></a> </span><span id="L-157"><a name="L-157"></a>Example: git diff </span><span id="L-158"><a name="L-158"></a> </span><span id="L-159"><a name="L-159"></a>gitk </span></pre></div> </td></tr></table>