Advertisement
krtek_net

Untitled

Jun 12th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.34 KB | None | 0 0
  1.  
  2. $ cheat git
  3.  
  4. Setup
  5. -----
  6.  
  7. git clone <repo>
  8. clone the repository specified by <repo>; this is similar to "checkout" in
  9. some other version control systems such as Subversion and CVS
  10.  
  11. Add colors to your ~/.gitconfig file:
  12.  
  13. [color]
  14. ui = auto
  15. [color "branch"]
  16. current = yellow reverse
  17. local = yellow
  18. remote = green
  19. [color "diff"]
  20. meta = yellow bold
  21. frag = magenta bold
  22. old = red bold
  23. new = green bold
  24. [color "status"]
  25. added = yellow
  26. changed = green
  27. untracked = cyan
  28.  
  29. Highlight whitespace in diffs
  30.  
  31. [color]
  32. ui = true
  33. [color "diff"]
  34. whitespace = red reverse
  35. [core]
  36. whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
  37.  
  38. Add aliases to your ~/.gitconfig file:
  39.  
  40. [alias]
  41. st = status
  42. ci = commit
  43. br = branch
  44. co = checkout
  45. df = diff
  46. dc = diff --cached
  47. lg = log -p
  48. lol = log --graph --decorate --pretty=oneline --abbrev-commit
  49. lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
  50. ls = ls-files
  51.  
  52. # Show files ignored by git:
  53. ign = ls-files -o -i --exclude-standard
  54.  
  55.  
  56. Configuration
  57. -------------
  58.  
  59. git config -e [--global]
  60. edit the .git/config [or ~/.gitconfig] file in your $EDITOR
  61.  
  62. git config --global user.name 'John Doe'
  63. git config --global user.email johndoe@example.com
  64. sets your name and email for commit messages
  65.  
  66. git config branch.autosetupmerge true
  67. tells git-branch and git-checkout to setup new branches so that git-pull(1)
  68. will appropriately merge from that remote branch. Recommended. Without this,
  69. you will have to add --track to your branch command or manually merge remote
  70. tracking branches with "fetch" and then "merge".
  71.  
  72. git config core.autocrlf true
  73. This setting tells git to convert the newlines to the system's standard
  74. when checking out files, and to LF newlines when committing in
  75.  
  76. git config --list
  77. To view all options
  78.  
  79. git config apply.whitespace nowarn
  80. To ignore whitespace
  81.  
  82. You can add "--global" after "git config" to any of these commands to make it
  83. apply to all git repos (writes to ~/.gitconfig).
  84.  
  85.  
  86. Info
  87. ----
  88. git reflog
  89. Use this to recover from *major* mess ups! It's basically a log of the
  90. last few actions and you might have luck and find old commits that
  91. have been lost by doing a complex merge.
  92.  
  93. git diff
  94. show a diff of the changes made since your last commit
  95. to diff one file: "git diff -- <filename>"
  96. to show a diff between staging area and HEAD: `git diff --cached`
  97.  
  98. git status
  99. show files added to the staging area, files with changes, and untracked files
  100.  
  101. git log
  102. show recent commits, most recent on top. Useful options:
  103. --color with color
  104. --graph with an ASCII-art commit graph on the left
  105. --decorate with branch and tag names on appropriate commits
  106. --stat with stats (files changed, insertions, and deletions)
  107. -p with full diffs
  108. --author=foo only by a certain author
  109. --after="MMM DD YYYY" ex. ("Jun 20 2008") only commits after a certain date
  110. --before="MMM DD YYYY" only commits that occur before a certain date
  111. --merge only the commits involved in the current merge conflicts
  112.  
  113. git log <ref>..<ref>
  114. show commits between the specified range. Useful for seeing changes from
  115. remotes:
  116. git log HEAD..origin/master # after git remote update
  117.  
  118. git show <rev>
  119. show the changeset (diff) of a commit specified by <rev>, which can be any
  120. SHA1 commit ID, branch name, or tag (shows the last commit (HEAD) by default)
  121.  
  122. also to show the contents of a file at a specific revision, use
  123. git show <rev>:<filename>
  124. this is similar to cat-file but much simpler syntax.
  125.  
  126. git show --name-only <rev>
  127. show only the names of the files that changed, no diff information.
  128.  
  129. git blame <file>
  130. show who authored each line in <file>
  131.  
  132. git blame <file> <rev>
  133. show who authored each line in <file> as of <rev> (allows blame to go back in
  134. time)
  135.  
  136. git gui blame
  137. really nice GUI interface to git blame
  138.  
  139. git whatchanged <file>
  140. show only the commits which affected <file> listing the most recent first
  141. E.g. view all changes made to a file on a branch:
  142. git whatchanged <branch> <file> | grep commit | \
  143. colrm 1 7 | xargs -I % git show % <file>
  144. this could be combined with git remote show <remote> to find all changes on
  145. all branches to a particular file.
  146.  
  147. git diff <commit> head path/to/fubar
  148. show the diff between a file on the current branch and potentially another
  149. branch
  150.  
  151. git diff --cached [<file>]
  152. shows diff for staged (git-add'ed) files (which includes uncommitted git
  153. cherry-pick'ed files)
  154.  
  155. git ls-files
  156. list all files in the index and under version control.
  157.  
  158. git ls-remote <remote> [HEAD]
  159. show the current version on the remote repo. This can be used to check whether
  160. a local is required by comparing the local head revision.
  161.  
  162. Adding / Deleting
  163. -----------------
  164.  
  165. git add <file1> <file2> ...
  166. add <file1>, <file2>, etc... to the project
  167.  
  168. git add <dir>
  169. add all files under directory <dir> to the project, including subdirectories
  170.  
  171. git add .
  172. add all files under the current directory to the project
  173. *WARNING*: including untracked files.
  174.  
  175. git rm <file1> <file2> ...
  176. remove <file1>, <file2>, etc... from the project
  177.  
  178. git rm $(git ls-files --deleted)
  179. remove all deleted files from the project
  180.  
  181. git rm --cached <file1> <file2> ...
  182. commits absence of <file1>, <file2>, etc... from the project
  183.  
  184. Ignoring
  185. ---------
  186.  
  187. Option 1:
  188.  
  189. Edit $GIT_DIR/info/exclude. See Environment Variables below for explanation on
  190. $GIT_DIR.
  191.  
  192. Option 2:
  193.  
  194. Add a file .gitignore to the root of your project. This file will be checked in.
  195.  
  196. Either way you need to add patterns to exclude to these files.
  197.  
  198. Staging
  199. -------
  200.  
  201. git add <file1> <file2> ...
  202. git stage <file1> <file2> ...
  203. add changes in <file1>, <file2> ... to the staging area (to be included in
  204. the next commit
  205.  
  206. git add -p
  207. git stage --patch
  208. interactively walk through the current changes (hunks) in the working
  209. tree, and decide which changes to add to the staging area.
  210.  
  211. git add -i
  212. git stage --interactive
  213. interactively add files/changes to the staging area. For a simpler
  214. mode (no menu), try `git add --patch` (above)
  215.  
  216. Unstaging
  217. ---------
  218.  
  219. git reset HEAD <file1> <file2> ...
  220. remove the specified files from the next commit
  221.  
  222.  
  223. Committing
  224. ----------
  225.  
  226. git commit <file1> <file2> ... [-m <msg>]
  227. commit <file1>, <file2>, etc..., optionally using commit message <msg>,
  228. otherwise opening your editor to let you type a commit message
  229.  
  230. git commit -a
  231. commit all files changed since your last commit
  232. (does not include new (untracked) files)
  233.  
  234. git commit -v
  235. commit verbosely, i.e. includes the diff of the contents being committed in
  236. the commit message screen
  237.  
  238. git commit --amend
  239. edit the commit message of the most recent commit
  240.  
  241. git commit --amend <file1> <file2> ...
  242. redo previous commit, including changes made to <file1>, <file2>, etc...
  243.  
  244.  
  245. Branching
  246. ---------
  247.  
  248. git branch
  249. list all local branches
  250.  
  251. git branch -r
  252. list all remote branches
  253.  
  254. git branch -a
  255. list all local and remote branches
  256.  
  257. git branch <branch>
  258. create a new branch named <branch>, referencing the same point in history as
  259. the current branch
  260.  
  261. git branch <branch> <start-point>
  262. create a new branch named <branch>, referencing <start-point>, which may be
  263. specified any way you like, including using a branch name or a tag name
  264.  
  265. git push <repo> <start-point>:refs/heads/<branch>
  266. create a new remote branch named <branch>, referencing <start-point> on the
  267. remote. Repo is the name of the remote.
  268. Example: git push origin origin:refs/heads/branch-1
  269. Example: git push origin origin/branch-1:refs/heads/branch-2
  270. Example: git push origin branch-1 ## shortcut
  271.  
  272. git branch --track <branch> <remote-branch>
  273. create a tracking branch. Will push/pull changes to/from another repository.
  274. Example: git branch --track experimental origin/experimental
  275.  
  276. git branch --set-upstream <branch> <remote-branch> (As of Git 1.7.0)
  277. Make an existing branch track a remote branch
  278. Example: git branch --set-upstream foo origin/foo
  279.  
  280. git branch -d <branch>
  281. delete the branch <branch>; if the branch you are deleting points to a
  282. commit which is not reachable from the current branch, this command
  283. will fail with a warning.
  284.  
  285. git branch -r -d <remote-branch>
  286. delete a remote-tracking branch.
  287. Example: git branch -r -d wycats/master
  288.  
  289. git branch -D <branch>
  290. even if the branch points to a commit not reachable from the current branch,
  291. you may know that that commit is still reachable from some other branch or
  292. tag. In that case it is safe to use this command to force git to delete the
  293. branch.
  294.  
  295. git checkout <branch>
  296. make the current branch <branch>, updating the working directory to reflect
  297. the version referenced by <branch>
  298.  
  299. git checkout -b <new> <start-point>
  300. create a new branch <new> referencing <start-point>, and check it out.
  301.  
  302. git push <repository> :<branch>
  303. removes a branch from a remote repository.
  304. Example: git push origin :old_branch_to_be_deleted
  305.  
  306. git co <branch> <path to new file>
  307. Checkout a file from another branch and add it to this branch. File
  308. will still need to be added to the git branch, but it's present.
  309. Eg. git co remote_at_origin__tick702_antifraud_blocking
  310. ..../...nt_elements_for_iframe_blocked_page.rb
  311.  
  312. git show <branch> -- <path to file that does not exist>
  313. Eg. git show remote_tick702 -- path/to/fubar.txt
  314. show the contents of a file that was created on another branch and that
  315. does not exist on the current branch.
  316.  
  317. git show <rev>:<repo path to file>
  318. Show the contents of a file at the specific revision. Note: path has to be
  319. absolute within the repo.
  320.  
  321. Merging
  322. -------
  323.  
  324. git merge <branch>
  325. merge branch <branch> into the current branch; this command is idempotent
  326. and can be run as many times as needed to keep the current branch
  327. up-to-date with changes in <branch>
  328.  
  329. git merge <branch> --no-commit
  330. merge branch <branch> into the current branch, but do not autocommit the
  331. result; allows you to make further tweaks
  332.  
  333. git merge <branch> -s ours
  334. merge branch <branch> into the current branch, but drops any changes in
  335. <branch>, using the current tree as the new tree
  336.  
  337.  
  338. Cherry-Picking
  339. --------------
  340.  
  341. git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] <commit>
  342. selectively merge a single commit from another local branch
  343. Example: git cherry-pick 7300a6130d9447e18a931e898b64eefedea19544
  344.  
  345. git hash-object <file-path>
  346. get the blob of some file whether it is in a repository or not
  347.  
  348. Find the commit in the repository that contains the file blob:
  349.  
  350. obj_blob="$1"
  351. git log --pretty=format:'%T %h %s' \
  352. | while read tree commit subject ; do
  353. if git ls-tree -r $tree | grep -q "$obj_blob" ; then
  354. echo $commit "$subject"
  355. fi
  356. done
  357.  
  358.  
  359. Squashing
  360. ---------
  361. WARNING: "git rebase" changes history. Be careful. Google it.
  362.  
  363. git rebase --interactive HEAD~10
  364. (then change all but the first "pick" to "squash")
  365. squash the last 10 commits into one big commit
  366.  
  367.  
  368. Conflicts
  369. ---------
  370.  
  371. git mergetool
  372. work through conflicted files by opening them in your mergetool (opendiff,
  373. kdiff3, etc.) and choosing left/right chunks. The merged result is staged for
  374. commit.
  375.  
  376. For binary files or if mergetool won't do, resolve the conflict(s) manually
  377. and then do:
  378.  
  379. git add <file1> [<file2> ...]
  380.  
  381. Once all conflicts are resolved and staged, commit the pending merge with:
  382.  
  383. git commit
  384.  
  385.  
  386. Sharing
  387. -------
  388.  
  389. git fetch <remote>
  390. update the remote-tracking branches for <remote> (defaults to "origin").
  391. Does not initiate a merge into the current branch (see "git pull" below).
  392.  
  393. git pull
  394. fetch changes from the server, and merge them into the current branch.
  395. Note: .git/config must have a [branch "some_name"] section for the current
  396. branch, to know which remote-tracking branch to merge into the current
  397. branch. Git 1.5.3 and above adds this automatically.
  398.  
  399. git push
  400. update the server with your commits across all branches that are *COMMON*
  401. between your local copy and the server. Local branches that were never
  402. pushed to the server in the first place are not shared.
  403.  
  404. git push origin <branch>
  405. update the server with your commits made to <branch> since your last push.
  406. This is always *required* for new branches that you wish to share. After
  407. the first explicit push, "git push" by itself is sufficient.
  408.  
  409. git push origin <branch>:refs/heads/<branch>
  410. E.g. git push origin twitter-experiment:refs/heads/twitter-experiment
  411. Which, in fact, is the same as git push origin <branch> but a little
  412. more obvious what is happening.
  413.  
  414. Reverting
  415. ---------
  416.  
  417. git revert <rev>
  418. reverse commit specified by <rev> and commit the result. This does *not* do
  419. the same thing as similarly named commands in other VCS's such as "svn
  420. revert" or "bzr revert", see below
  421.  
  422. git checkout <file>
  423. re-checkout <file>, overwriting any local changes
  424.  
  425. git checkout .
  426. re-checkout all files, overwriting any local changes. This is most similar
  427. to "svn revert" if you're used to Subversion commands
  428.  
  429.  
  430. Fix mistakes / Undo
  431. -------------------
  432.  
  433. git reset --hard
  434. abandon everything since your last commit; this command can be DANGEROUS.
  435. If merging has resulted in conflicts and you'd like to just forget about
  436. the merge, this command will do that.
  437.  
  438. git reset --hard ORIG_HEAD or git reset --hard origin/master
  439. undo your most recent *successful* merge *and* any changes that occurred
  440. after. Useful for forgetting about the merge you just did. If there are
  441. conflicts (the merge was not successful), use "git reset --hard" (above)
  442. instead.
  443.  
  444. git reset --soft HEAD^
  445. forgot something in your last commit? That's easy to fix. Undo your last
  446. commit, but keep the changes in the staging area for editing.
  447.  
  448. git commit --amend
  449. redo previous commit, including changes you've staged in the meantime.
  450. Also used to edit commit message of previous commit.
  451.  
  452.  
  453. Plumbing
  454. --------
  455.  
  456. test <sha1-A> = $(git merge-base <sha1-A> <sha1-B>)
  457. determine if merging sha1-B into sha1-A is achievable as a fast forward;
  458. non-zero exit status is false.
  459.  
  460.  
  461. Stashing
  462. --------
  463.  
  464. git stash
  465. git stash save <optional-name>
  466. save your local modifications to a new stash (so you can for example
  467. "git svn rebase" or "git pull")
  468.  
  469. git stash apply
  470. restore the changes recorded in the stash on top of the current working tree
  471. state
  472.  
  473. git stash pop
  474. restore the changes from the most recent stash, and remove it from the stack
  475. of stashed changes
  476.  
  477. git stash list
  478. list all current stashes
  479.  
  480. git stash show <stash-name> -p
  481. show the contents of a stash - accepts all diff args
  482.  
  483. git stash drop [<stash-name>]
  484. delete the stash
  485.  
  486. git stash clear
  487. delete all current stashes
  488.  
  489.  
  490. Remotes
  491. -------
  492.  
  493. git remote add <remote> <remote_URL>
  494. adds a remote repository to your git config. Can be then fetched locally.
  495. Example:
  496. git remote add coreteam git://github.com/wycats/merb-plugins.git
  497. git fetch coreteam
  498.  
  499. git push <remote> :refs/heads/<branch>
  500. delete a branch in a remote repository
  501.  
  502. git push <remote> <remote>:refs/heads/<remote_branch>
  503. create a branch on a remote repository
  504. Example: git push origin origin:refs/heads/new_feature_name
  505.  
  506. git push <repository> +<remote>:<new_remote>
  507. replace a <remote> branch with <new_remote>
  508. think twice before do this
  509. Example: git push origin +master:my_branch
  510.  
  511. git remote prune <remote>
  512. prune deleted remote-tracking branches from "git branch -r" listing
  513.  
  514. git remote add -t master -m master origin git://example.com/git.git/
  515. add a remote and track its master
  516.  
  517. git remote show <remote>
  518. show information about the remote server.
  519.  
  520. git checkout -b <local branch> <remote>/<remote branch>
  521. Eg.:
  522. git checkout -b myfeature origin/myfeature
  523. git checkout -b myfeature remotes/<remote>/<branch>
  524.  
  525. Track a remote branch as a local branch. It seems that
  526. somtimes an extra 'remotes/' is required, to see the exact
  527. branch name, 'git branch -a'.
  528.  
  529. git pull <remote> <branch>
  530. git push
  531. For branches that are remotely tracked (via git push) but
  532. that complain about non-fast forward commits when doing a
  533. git push. The pull synchronizes local and remote, and if
  534. all goes well, the result is pushable.
  535.  
  536. git fetch <remote>
  537. Retrieves all branches from the remote repository. After
  538. this 'git branch --track ...' can be used to track a branch
  539. from the new remote.
  540.  
  541. Submodules
  542. ----------
  543.  
  544. git submodule add <remote_repository> <path/to/submodule>
  545. add the given repository at the given path. The addition will be part of the
  546. next commit.
  547.  
  548. git submodule update [--init]
  549. Update the registered submodules (clone missing submodules, and checkout
  550. the commit specified by the super-repo). --init is needed the first time.
  551.  
  552. git submodule foreach <command>
  553. Executes the given command within each checked out submodule.
  554.  
  555. Removing submodules
  556.  
  557. 1. Delete the relevant line from the .gitmodules file.
  558. 2. Delete the relevant section from .git/config.
  559. 3. Run git rm --cached path_to_submodule (no trailing slash).
  560. 4. Commit and delete the now untracked submodule files.
  561.  
  562. Updating submodules
  563. To update a submodule to a new commit:
  564. 1. update submodule:
  565. cd <path to submodule>
  566. git pull
  567. 2. commit the new version of submodule:
  568. cd <path to toplevel>
  569. git commit -m "update submodule version"
  570. 3. check that the submodule has the correct version
  571. git submodule status
  572. If the update in the submodule is not committed in the
  573. main repository, it is lost and doing git submodule
  574. update will revert to the previous version.
  575.  
  576. Patches
  577. -------
  578.  
  579. git format-patch HEAD^
  580. Generate the last commit as a patch that can be applied on another
  581. clone (or branch) using 'git am'. Format patch can also generate a
  582. patch for all commits using 'git format-patch HEAD^ HEAD'
  583. All page files will be enumerated with a prefix, e.g. 0001 is the
  584. first patch.
  585.  
  586. git format-patch <Revision>^..<Revision>
  587. Generate a patch for a single commit. E.g.
  588. git format-patch d8efce43099^..d8efce43099
  589. Revision does not need to be fully specified.
  590.  
  591. git am <patch file>
  592. Applies the patch file generated by format-patch.
  593.  
  594. git diff --no-prefix > patchfile
  595. Generates a patch file that can be applied using patch:
  596. patch -p0 < patchfile
  597. Useful for sharing changes without generating a git commit.
  598.  
  599. Tags
  600. ----
  601.  
  602. git tag -l
  603. Will list all tags defined in the repository.
  604.  
  605. git co <tag_name>
  606. Will checkout the code for a particular tag. After this you'll
  607. probably want to do: 'git co -b <some branch name>' to define
  608. a branch. Any changes you now make can be committed to that
  609. branch and later merged.
  610.  
  611. Archive
  612. -------
  613.  
  614. git archive master | tar -x -C /somewhere/else
  615. Will export expanded tree as tar archive at given path
  616.  
  617. git archive master | bzip2 > source-tree.tar.bz2
  618. Will export archive as bz2
  619.  
  620. git archive --format zip --output /full/path master
  621. Will export as zip
  622.  
  623. Git Instaweb
  624. ------------
  625.  
  626. git instaweb --httpd=webrick [--start | --stop | --restart]
  627.  
  628.  
  629. Environment Variables
  630. ---------------------
  631.  
  632. GIT_AUTHOR_NAME, GIT_COMMITTER_NAME
  633. Your full name to be recorded in any newly created commits. Overrides
  634. user.name in .git/config
  635.  
  636. GIT_AUTHOR_EMAIL, GIT_COMMITTER_EMAIL
  637. Your email address to be recorded in any newly created commits. Overrides
  638. user.email in .git/config
  639.  
  640. GIT_DIR
  641. Location of the repository to use (for out of working directory repositories)
  642.  
  643. GIT_WORKING_TREE
  644. Location of the Working Directory - use with GIT_DIR to specifiy the working
  645. directory root
  646. or to work without being in the working directory at all.
  647.  
  648. Changing history
  649. ----------------
  650.  
  651. Change author for all commits with given name
  652.  
  653. git filter-branch --commit-filter '
  654. if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
  655. then
  656. GIT_COMMITTER_NAME="<New Name>";
  657. GIT_AUTHOR_NAME="<New Name>";
  658. GIT_COMMITTER_EMAIL="<New Email>";
  659. GIT_AUTHOR_EMAIL="<New Email>";
  660. git commit-tree "$@";
  661. else
  662. git commit-tree "$@";
  663. fi' HEAD
  664.  
  665.  
  666.  
  667. $ git reset --hard origin/master
  668.  
  669. will remove all commits not in origin/master.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement