Advertisement
boxglue

My GIT cheatsheet

Nov 30th, 2017
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. Git commands
  2.  
  3. git clone https://github.com/salamander2/leds
  4.  
  5. git fetch --all [no repository argument]
  6.  
  7. git pull ____ does a "git fetch" then a "git merge"
  8.  
  9. http://www.answering-islam.org/authors/toler/abraham_kaaba.html
  10. gitk # starts a GUI !!!
  11. ================= SETUP ===========================
  12.  
  13. cd to the location desired (probably above the package folder)
  14.  
  15. git init
  16. git remote add origin ssh://git@github.com/salamander2/Display-Array-Java
  17.  
  18. #use set-url to update/change the url (ie from HTTPS to SSH)
  19. git remote set-url origin ssh://git@github.com/salamander2/Display-Array-Java ## NO, NOT THIS ONE
  20.  
  21.  
  22.  
  23. git remote -v # list the settings for git remote
  24. git remote show origin # this works better
  25.  
  26. eval `ssh-agent -s`
  27. ssh-add ~/.ssh/id_rsa_git.ssh # don't use default SSH-key (id.rsa)
  28. ssh -T git@github.com #test conection
  29.  
  30. git pull origin master # get the local branch synced to the remote one.
  31.  
  32. *** For some reason this doesn't always work.
  33.  
  34. HTTP version:
  35.  
  36. using: git clone https://github.com/salamander2/myRPGGame
  37. results in .git/config having these lines:
  38. [remote "origin"]
  39. url = https://github.com/salamander2/myRPGGame
  40. but this:
  41. git remote set-url origin git@github.com/salamander2/myRPGGame
  42. ends up with:
  43. [remote "origin"]
  44. url = git@github.com/salamander2/myRPGGame
  45.  
  46. WHICH DOES NOT WORK FOR pit push origin master !!!
  47. Copy the .git/config file from some other project
  48. ******************************************
  49. OR:
  50.  
  51. git remote add origin https://github.com/RaiderRobotics/FRC-LCD-Display.git
  52. git push -u origin master #using some user
  53.  
  54.  
  55. ********************************************
  56. #Fixing the author and email used for commits:
  57.  
  58. git config --global user.name "Michael Harwood"
  59. git config --global user.email you@example.com
  60.  
  61. git config --global --list
  62.  
  63. git commit --amend --reset-author
  64.  
  65. ## This is for HTTP access so that you don't have to keep entering username and password
  66. git config --global credential.helper cache
  67. git config --global credential.helper 'cache --timeout=3600'
  68. # Set the cache to timeout after 1 hour (setting is in seconds)
  69.  
  70. git remote -v # show current connection
  71. git remote show origin # this works better than the previous line
  72.  
  73. ================= COMMITS ========================
  74.  
  75. git status
  76.  
  77. git add filename
  78.  
  79. get reset # This will undo the git add command
  80.  
  81. get rm filename #only if you need to delete a file
  82. ex
  83. git commit
  84.  
  85. git commit -a #this does the add & rm & commit in one step
  86.  
  87. OR
  88. git fetch
  89. git diff origin/master
  90.  
  91.  
  92. git push origin master
  93. git pull origin master
  94.  
  95. ================ LOG DISPLAY ==========================
  96. git log --graph --decorate --abbrev-commit --all # use this to compare local and remote repos.
  97. # you need --decorate
  98.  
  99. http://www.answering-islam.org/authors/toler/abraham_kaaba.html
  100. git log --graph --decorate --abbrev-commit --all --pretty=medium
  101.  
  102.  
  103. #follow only one file:
  104. git log -- filename
  105. http://www.answering-islam.org/authors/toler/abraham_kaaba.html
  106.  
  107. #show changes for one file:
  108. git log -p filename
  109.  
  110. ================ TAGS ==============================
  111.  
  112. git tag -a tagname HEAD # This creates an annotated tag (a more permanent version)
  113. (then enter a tag message)
  114. eg. git tag -a v2.0
  115. git tag -l -n2 # list tags with 2 lines of comments
  116. git log --decorate # this lists the tags along with the commits
  117.  
  118. git show v2.0 # this lists the comment for this ta
  119. TO PUSH TAGS you have to do the following ... add --tag
  120. (Annotated) TAGS automatically show up as releases in GitHub
  121.  
  122. git push -u --tag origin master
  123.  
  124. =============== DIFF ===================
  125. git diff HEAD^ compares the current files with the previous version.
  126.  
  127. > git status
  128. On branch master
  129. Changes to be committed:
  130. (use "git reset HEAD <file>..." to unstage)
  131.  
  132. modified: hsa2/ConsoleCanvas.java
  133. http://www.answering-islam.org/authors/toler/abraham_kaaba.html
  134. renamed: hsa2/Console.java -> hsa2/GraphicsConsole.java
  135.  
  136. > git diff HEAD^ -- hsa2/ConsoleCanvas.java
  137. # This command, above, shows the differences in this file only.
  138.  
  139. ================ MERGING LOCAL & REMOTE ============================
  140. (when Git cannot merge automatically)
  141.  
  142. git mergetool
  143. OR
  144. git mergetool -t meld
  145.  
  146. You'll see 3 wndows in MELD:
  147. * The local branch
  148. * The current version that you are merging into
  149. * The other (or “remote”) version
  150. Make the centre window look like the one you want. Then save it.
  151. You normally want the local version to also look like the middle version. That should eliminate the creation of annoying .orig files
  152.  
  153. Then use git status, add, commit, push.
  154.  
  155. ================= BRANCHES =======================
  156. git remote show origin # shows branches too
  157.  
  158. git branch -a # list all branches (local and remote)
  159. git checkout branchname # switch to branch
  160. git branch # list local branches and show which is active
  161. git branch newname # create a new branch
  162.  
  163. git branch -d branchname # delete local branch
  164. git push origin :branchname # delete this branch upstream
  165. OR
  166. git push origin --delete branchname
  167.  
  168.  
  169. git
  170. #rename a branch
  171. git branch -m old_branch new_branch # Rename branch locally
  172. OR
  173. git branch -m new_branch # rename currently checked out branch
  174. git push origin :old_branch # Delete the old branch
  175.  
  176. git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
  177. OR
  178. git push -u origin new_branch # new branch is the current branch that you're on
  179.  
  180. OR
  181. git push origin # this will default to pushing the current branch to the same named remote.
  182.  
  183. ================ MERGING BRANCHES =================
  184. You can run your tests, make sure the hotfix is what you want, and merge it back into your master branch to deploy to production. You do this with the git merge command:
  185.  
  186. git checkout master
  187. git merge hotfix
  188.  
  189.  
  190. ================ FIXING ERRORS ====================
  191.  
  192. ---------------------------------- UNDOING MOST RECENT COMMIT ------------------------
  193. git reset --hard HEAD^ # HEAD^ is the parent of the current commit.
  194. OR
  195. git revert HEAD^ # this creates a NEW commit that undoes the previous one.
  196.  
  197. THEN
  198. git push --force origin master #force github to also go back to the previous commit
  199. --------------------------------- Fixing pulling wrong branch! ----------------------------
  200.  
  201. *** If you're in a different branch and run "git pull origin master" it will MERGE the master branch into the current branch!!!
  202. To undo this:
  203.  
  204. (1) run git reflog
  205.  
  206. git reflog
  207. 46d3be4 HEAD@{0}: pull origin master: Merge made by the 'recursive' strategy.
  208. 346f2d2 HEAD@{1}: commit: renamed ArrayGame.java to UserArrayGame.java
  209. ec0eb50 HEAD@{2}: commit: First working version of this branch. Using the MouseObserver pattern.
  210. 00ba00d HEAD@{3}: checkout: moving from master to Observer-pattern
  211. 00ba00d HEAD@{4}: commit (merge): Fixed Merge branch 'master' of github.com:salamander2/Display-Array-Java
  212. 3ba071e HEAD@{5}: commit (initial): First commit
  213.  
  214. (2) find the previous header id, and run git reset :
  215. git reset --hard 346f2d2
  216. HEAD is now at 346f2d2 renamed ArrayGame.java to UserArrayGame.java
  217.  
  218.  
  219. ===============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement