{"id":2099,"date":"2015-08-10T15:10:06","date_gmt":"2015-08-10T13:10:06","guid":{"rendered":"https:\/\/adrhc.go.ro\/wordpress\/?p=2099"},"modified":"2020-11-11T08:41:30","modified_gmt":"2020-11-11T06:41:30","slug":"working-with-git","status":"publish","type":"post","link":"https:\/\/adrhc.go.ro\/blog\/working-with-git\/","title":{"rendered":"Working with GIT"},"content":{"rendered":"<pre>\r\n<a href=\"http:\/\/marklodato.github.io\/visual-git-guide\/index-en.html\" rel=\"noopener noreferrer\" target=\"_blank\">A Visual Git Reference<\/a>\r\n<a href=\"http:\/\/onlywei.github.io\/explain-git-with-d3\/#checkout\" rel=\"noopener noreferrer\" target=\"_blank\">Understand some basic git concepts visually<\/a>\r\n<a href=\"http:\/\/nvie.com\/posts\/a-successful-git-branching-model\/\" rel=\"noopener noreferrer\" target=\"_blank\">A successful Git branching model<\/a>\r\n<a href=\"http:\/\/think-like-a-git.net\/sections\/rebase-from-the-ground-up\/cherry-picking-explained.html\" rel=\"noopener noreferrer\" target=\"_blank\">Cherry-picking explained<\/a>\r\n<a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/learn-git-with-bitbucket-cloud\" rel=\"noopener noreferrer\" target=\"_blank\">Learn Git with Bitbucket Cloud<\/a>\r\n<a href=\"https:\/\/www.devroom.io\/2011\/07\/05\/git-squash-your-latests-commits-into-one\/\" rel=\"noopener noreferrer\" target=\"_blank\">Squash your latests commits into one<\/a>\r\n<\/pre>\n<pre class=\"brush:bash shell;gutter:true;toolbar:false\">\r\n# show diff for last commit only\r\ngit log -p HEAD -1\r\ngit log -p -n 1\r\ngit log -p HEAD~1..HEAD\r\n\r\n# --graph draw a text based graph of the commits on the left hand side of the commit messages\r\n# --decorate adds the names of branches or tags of the commits that are shown\r\ngit log --full-history --all --graph --color --oneline --date-order\r\ngit log --full-history --all --graph --color --date-order --pretty=format:\"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s\"\r\n\r\n# view changesets even though they are not referenced by any branch or tag\r\ngit reflog\r\n\r\n# shows which files were altered and the relative number of lines added or deleted from each of them\r\ngit log --stat\r\ngit log --stat -1\r\ngit log --stat 57a6586c53bede7334d1be67b40e3c4f9cb63af9 -n 1\r\ngitk --all -> GUI graph (run it from .git parent directory)\r\n\r\n# shows both remote and local branches\r\ngit branch -a\r\n# shows local branches\r\ngit branch\r\n# shows remote branches\r\ngit branch -r\r\n\r\n# restore file test4.txt to match the current HEAD\r\ngit reset HEAD test4.txt\r\ngit checkout -- test4.txt\r\n\r\n# restore multiple deleted files\r\ngit checkout $(git ls-files -d)\r\n\r\n# commit directly without first staging\r\ngit commit -m 'comment' -- README.txt\r\n\r\n# display the information about the source of the clone:\r\ngit config --get remote.origin.url\r\ngit remote -v\r\ngit remote show origin\r\n\r\n# git + ssh on port 222 (other than default 22) with ssh user gigi\r\ngit clone ssh:\/\/adrhc222\/********\/temp\/git-try\r\n# in ~\/.ssh\/config (create if not exists) put:\r\nHost adrhc222\r\n    HostName adrhc.go.ro\r\n    Port 222\r\n    User gigi\r\n    ServerAliveInterval 30\r\n\r\n# view commits on origin only\r\ngit fetch origin\r\ngit branch -a\r\n* master\r\n  remotes\/origin\/HEAD -> origin\/master\r\n  remotes\/origin\/master\r\n  remotes\/origin\/readme-edits\r\ngit log --full-history --all --graph --color --pretty=format:\"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s\" master..origin\/master\r\ngit log --full-history --all --graph --color --pretty=format:\"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s\" origin\/readme-edits~1..origin\/master\r\n\r\n# Caching your GitHub password in Git\r\n# see https:\/\/help.github.com\/articles\/caching-your-github-password-in-git\/\r\ngit config --global credential.helper cache\r\ngit config --global credential.helper 'cache --timeout=3600'\r\ngit config --global credential.helper 'cache --timeout=31536000'\r\n\r\n# create a \u201cshared\u201d repository which allows anyone with \u201cgroup write\u201d permissions on the folder to push into the repository\r\nmkdir gitProject.git\t-> .git it's a good practice for --bare repositories\r\ncd gitProject.git\r\ngit init --bare --shared\r\n# see https:\/\/git-scm.com\/docs\/githooks -> post-update\r\ncp -v hooks\/post-update.sample hooks\/post-update\r\n\r\n# generation of info\/refs and objects\/info\/packs files\r\ncd test-bare-repository.git\r\n\/usr\/lib\/git-core\/git-update-server-info\r\nman git-update-server-info\r\ncat info\/refs\r\ncat objects\/info\/packs\r\n\r\n# create locally a project then upload to remote:\r\nrm -rf .git\r\ngit init\r\ngit add .\r\ngit commit -m \"Initial commit\"\r\ngit remote add origin https:\/\/adrhc.go.ro\/angular2-quickstart.git\r\n# -u option determines the creation of the remote branch master\r\ngit push -u origin master\r\n\r\n# \/etc\/gitconfig\r\ngit config --system --list\r\n\r\n# global configuration\r\ngit config --global --list\r\ngit config --global --edit -> at top you'll see the file's path (e.g. $HOME\/.gitconfig)\r\n\r\n# in a project\r\ngit config --list\r\n\r\n# for CRLF see core.autocrlf variable\r\n# https:\/\/git-scm.com\/book\/en\/v2\/Customizing-Git-Git-Configuration\r\n# http:\/\/stackoverflow.com\/questions\/3206843\/how-line-ending-conversions-work-with-git-core-autocrlf-between-different-operat\r\n# exists in $HOME\/.gitconfig\r\ngit config --global core.autocrlf input\r\ngit config --global --get core.autocrlf\r\n\r\n# workflow example:\r\n# https:\/\/help.github.com\/articles\/syncing-a-fork\/\r\ngit clone https:\/\/github.com\/adrhc\/angular2-webpack-starter.git\r\ngit checkout master\r\ngit remote add upstream https:\/\/github.com\/AngularClass\/angular2-webpack-starter.git\r\ngit remote -v\r\nsudo npm install webpack-dev-server rimraf webpack -g\r\nnpm install\r\ngit fetch upstream\r\ngit merge upstream\/master\r\n\r\n# create a lightweight tag for HEAD\r\ngit tag \"tag-name-here\" HEAD\r\n# list tags\r\ngit tag -n\r\n# list tags for current branch\r\ngit describe --tags --abbrev=0\r\n# push all tags\r\ngit push origin --tags\r\n# push one tag\r\ngit push origin \"tag-name-here\"\r\n# get all new tags\r\ngit fetch --all\r\n# checkout tag named n3.2.2\r\ngit checkout tags\/n3.2.2\r\n\r\n# copy\/duplicate repository\r\n# https:\/\/help.github.com\/articles\/duplicating-a-repository\/\r\ngit clone --bare https:\/\/github.com\/exampleuser\/old-repository.git\r\ncd old-repository.git\r\ngit push --mirror https:\/\/github.com\/exampleuser\/new-repository.git\r\n\r\n#ERROR\r\n\terror: Your local changes to the following files would be overwritten by merge:\r\n\t\tREADME.txt\r\n\t\tapp.sh\r\n\t\tbuild.prod.sh\r\n\t\tconfig\/webpack.common.js\r\n\tPlease, commit your changes or stash them before you can merge.\r\n\tAborting\r\n#SOLUTION\r\n\tgit stash\r\n\tgit stash pop\r\n\r\n#ERROR\r\n\tGitCommandFailedException: GitCommandFailedException: Command 'fetch' failed in \/ffp\/opt\/couchpotato (128):\r\n\tfatal: unable to access 'https:\/\/github.com\/RuudBurger\/CouchPotatoServer.git\/': SSL certificate problem: unable to get local issuer certificate\r\n#SOLUTION\r\n\t#ignore ssl certificate\r\n\tgit config --system http.sslverify false -> set in \/etc\/gitconfig\r\n\t#or use the Mozilla's CA Bundle certificate:\r\n\twget -nv http:\/\/curl.haxx.se\/ca\/cacert.pem -O \/ffp\/etc\/ssl\/cert.pem\r\n\tgit config --system http.sslcainfo \/ffp\/etc\/ssl\/cert.pem\r\n#SOLUTION\r\n\t# export site certificate chain\r\n\tcd $HOME\r\n\topenssl s_client -connect adrhc.go.ro:443 -showcerts &lt;\/dev\/null 2>\/dev\/null | openssl x509 -outform PEM > adrhc.go.ro.pem\r\n\topenssl x509 -in adrhc.go.ro.pem -out adrhc.go.ro.crt\r\n\t# install site certificate chain\r\n\tsu root\r\n\troot@gigi:~# cat adrhc.go.ro.crt >> \/etc\/ssl\/certs\/ca-certificates.crt\r\n\tCTRL+D\r\n#SOLUTION\r\n\t\/etc\/ssl\/certs\/ca-certificates.crt is the result of \"curl-config --ca\"\r\n\tsudo bash -c \"echo -n | openssl s_client -showcerts -connect 172.17.20.43:443 2>\/dev\/null | sed -ne '\/-BEGIN CERTIFICATE-\/,\/-END CERTIFICATE-\/p' >> \/etc\/ssl\/certs\/ca-certificates.crt\"\r\n<\/pre>\n<pre>\r\n<strong>Remove last commit from remote repository (e.g. origin)<\/strong>\r\ngit push origin +HEAD^:master\r\ngit reset --hard origin\/master\r\nThis only works if the remote repository has the configuration:\r\n[receive]\r\n        denyNonFastForwards = false\r\n\r\n<strong>Use master branch for angular2-websocket library in package.json<\/strong>\r\n<a href=\"http:\/\/www.devthought.com\/2012\/02\/17\/npm-tricks\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.devthought.com\/2012\/02\/17\/npm-tricks\/<\/a>\r\n<a href=\"https:\/\/github.com\/afrad\/angular2-websocket\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/afrad\/angular2-websocket<\/a>\r\npackage.json:\r\n    \"angular2-websocket\": \"https:\/\/github.com\/afrad\/angular2-websocket.git#8cb292ea25d5306dd516ece4480cf2d3b8263138\",\r\n    \"angular2-websocket\": \"git+https:\/\/172.17.20.43\/angular2-websocket.git\",\r\n\r\n<strong>Install own private repository<\/strong>\r\n<a href=\"http:\/\/stackoverflow.com\/questions\/10386310\/how-to-install-a-private-npm-module-without-my-own-registry\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/10386310\/how-to-install-a-private-npm-module-without-my-own-registry<\/a>\r\nnpm install \"git+https:\/\/172.17.20.43\/angular2-websocket.git\" --save generates in package.json:\r\n\"angular2-websocket\": \"git+https:\/\/172.17.20.43\/angular2-websocket.git\"\r\nnpm install \"git+https:\/\/172.17.20.43\/angular2-websocket.git#master\" --save generates in package.json:\r\n\"angular2-websocket\": \"git+https:\/\/172.17.20.43\/angular2-websocket.git#master\"\r\n\r\n<strong>Force update from git when running <em>npm install<\/em><\/strong>\r\nnpm install \"git+https:\/\/172.17.20.43\/angular2-websocket.git#master\" --force\r\n\r\n<strong>Fork after already cloning<\/strong>\r\ngit clone https:\/\/github.com\/adrhc\/h264-live-player\r\ngit remote -v\r\ngit remote rm origin\r\ngit remote add upstream https:\/\/github.com\/131\/h264-live-player.git\r\ngit remote add origin https:\/\/github.com\/adrhc\/h264-live-player.git\r\ngit branch --set-upstream-to=origin\/master\r\ngit config core.autocrlf input -> linux only\r\ngit fetch --all\r\ngit pull upstream master\r\n\r\n<strong>Checkout a remote branch<\/strong>\r\n<code>git branch -a<\/code>\r\n* master\r\n  remotes\/origin\/HEAD -> origin\/master\r\n  remotes\/origin\/foo\r\n  remotes\/origin\/master\r\n<code>git branch --track foo remotes\/origin\/foo\r\ngit checkout foo<\/code>\r\nor in 1 step\r\n<code>git checkout --track -b foo remotes\/origin\/foo<\/code>\r\n\r\n<strong>Make an existing Git branch track a remote branch<\/strong>\r\nJust switch the remote for the current branch:\r\n<code>git branch -u origin\/current-branch<\/code>\r\nWhen current branch is foo switch the remote while also pushing:\r\n<code>git push -u origin foo<\/code>\r\n\r\n<strong>Checkout all remote branches<\/strong>\r\n<a href=\"https:\/\/stackoverflow.com\/questions\/10312521\/how-to-fetch-all-git-branches\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/stackoverflow.com\/questions\/10312521\/how-to-fetch-all-git-branches<\/a>\r\ngit branch -a\r\nfor b in `git branch -r | grep -v -- '->'`; do echo \"${b##origin\/}\"; done\r\nfor b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin\/} $b; done\r\n# for already existing branches will fail with no undesirable consequences, e.g.:\r\n<em>fatal: A branch named 'master' already exists.<\/em>\r\n\r\n<strong>Show which remote branch is tracked by which local one<\/strong>\r\ngit branch -vv\r\ngit status -sb\r\ngit status | grep 'Your branch is up-to-date with'\r\n\r\n<strong>Create a local branch then the remote counterpart<\/strong>\r\nYou'll have to make sure there's no \"refactor\" branch (but could be refactor\/...).\r\ngit refactor maven-modules    -> using <a href=\"https:\/\/github.com\/tj\/git-extras\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/tj\/git-extras<\/a>\r\ngit push -u origin refactor\/maven-modules\r\n\r\n<strong>Remove a local branch<\/strong>\r\ngit branch -d feature\/app-init\r\n\r\n<strong>Remove a remote branch<\/strong>\r\ngit push origin --delete feature\/app-init\r\n\r\n<strong>Remove all stale branches<\/strong>\r\ngit remote prune origin --dry-run -> report what branches will be pruned\r\ngit fetch -p -> fetches and prunes all origins\r\n\r\n<strong>Find branch first commit or creation date<\/strong>\r\ngit reflog show --no-abbrev branch-name\r\n\r\n<strong>revert pushed commit after a wrong merge from branch X into master<\/strong>\r\nwe already are on master after merging the wrong branch ...\r\n<code>git revert -m 1 last-commit-id-meaning-the-merge-commit<\/code>\r\n\r\n<strong>git stash examples<\/strong>\r\ngit stash list\r\ngit stash save '[2017.11.01] before liquibase'\r\ngit stash show stash@{0}\r\ngit stash apply\r\n\r\n<strong>how to remove a remote repository<\/strong>\r\nshowing the remotes\r\n<code>git remote -v<\/code>\r\ndeleting the remote\r\n<code>git remote rm remote_name_from_showed_above<\/code>\r\n\r\n<strong>merge current branch into another without leaving the current<\/strong>\r\n<code><a href=\"https:\/\/github.com\/tj\/git-extras\/blob\/master\/man\/git-merge-into.md#git-merge-into1----merge-one-branch-into-another\" rel=\"noopener noreferrer\" target=\"_blank\">git merge-into<\/a> target-branch-name<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Visual Git Reference Understand some basic git concepts visually A successful Git branching model Cherry-picking explained Learn Git with Bitbucket Cloud Squash your latests commits into one # show diff for last commit only git log -p HEAD -1 [&hellip;]<\/p>\n<div class=\"link-more\"><a href=\"https:\/\/adrhc.go.ro\/blog\/working-with-git\/#more-2099\" class=\"more-link\">Continue reading &#10142; <span class=\"screen-reader-text\">Working with GIT<\/span><\/a><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[114],"class_list":["post-2099","post","type-post","status-publish","format-standard","hentry","category-programming","tag-git"],"_links":{"self":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/2099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/comments?post=2099"}],"version-history":[{"count":0,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/2099\/revisions"}],"wp:attachment":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/media?parent=2099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/categories?post=2099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/tags?post=2099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}