Working with GIT

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
git log -p -n 1
git log -p HEAD~1..HEAD

# --graph draw a text based graph of the commits on the left hand side of the commit messages
# --decorate adds the names of branches or tags of the commits that are shown
git log --full-history --all --graph --color --oneline --date-order
git log --full-history --all --graph --color --date-order --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"

# view changesets even though they are not referenced by any branch or tag
git reflog

# shows which files were altered and the relative number of lines added or deleted from each of them
git log --stat
git log --stat -1
git log --stat 57a6586c53bede7334d1be67b40e3c4f9cb63af9 -n 1
gitk --all -> GUI graph (run it from .git parent directory)

# shows both remote and local branches
git branch -a
# shows local branches
git branch
# shows remote branches
git branch -r

# restore file test4.txt to match the current HEAD
git reset HEAD test4.txt
git checkout -- test4.txt

# restore multiple deleted files
git checkout $(git ls-files -d)

# commit directly without first staging
git commit -m 'comment' -- README.txt

# display the information about the source of the clone:
git config --get remote.origin.url
git remote -v
git remote show origin

# git + ssh on port 222 (other than default 22) with ssh user gigi
git clone ssh://adrhc222/********/temp/git-try
# in ~/.ssh/config (create if not exists) put:
Host adrhc222
    HostName adrhc.go.ro
    Port 222
    User gigi
    ServerAliveInterval 30

# view commits on origin only
git fetch origin
git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/readme-edits
git log --full-history --all --graph --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s" master..origin/master
git 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

# Caching your GitHub password in Git
# see https://help.github.com/articles/caching-your-github-password-in-git/
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
git config --global credential.helper 'cache --timeout=31536000'

# create a “shared” repository which allows anyone with “group write” permissions on the folder to push into the repository
mkdir gitProject.git	-> .git it's a good practice for --bare repositories
cd gitProject.git
git init --bare --shared
# see https://git-scm.com/docs/githooks -> post-update
cp -v hooks/post-update.sample hooks/post-update

# generation of info/refs and objects/info/packs files
cd test-bare-repository.git
/usr/lib/git-core/git-update-server-info
man git-update-server-info
cat info/refs
cat objects/info/packs

# create locally a project then upload to remote:
rm -rf .git
git init
git add .
git commit -m "Initial commit"
git remote add origin https://adrhc.go.ro/angular2-quickstart.git
# -u option determines the creation of the remote branch master
git push -u origin master

# /etc/gitconfig
git config --system --list

# global configuration
git config --global --list
git config --global --edit -> at top you'll see the file's path (e.g. $HOME/.gitconfig)

# in a project
git config --list

# for CRLF see core.autocrlf variable
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
# http://stackoverflow.com/questions/3206843/how-line-ending-conversions-work-with-git-core-autocrlf-between-different-operat
# exists in $HOME/.gitconfig
git config --global core.autocrlf input
git config --global --get core.autocrlf

# workflow example:
# https://help.github.com/articles/syncing-a-fork/
git clone https://github.com/adrhc/angular2-webpack-starter.git
git checkout master
git remote add upstream https://github.com/AngularClass/angular2-webpack-starter.git
git remote -v
sudo npm install webpack-dev-server rimraf webpack -g
npm install
git fetch upstream
git merge upstream/master

# create a lightweight tag for HEAD
git tag "tag-name-here" HEAD
# list tags
git tag -n
# list tags for current branch
git describe --tags --abbrev=0
# push all tags
git push origin --tags
# push one tag
git push origin "tag-name-here"
# get all new tags
git fetch --all
# checkout tag named n3.2.2
git checkout tags/n3.2.2

# copy/duplicate repository
# https://help.github.com/articles/duplicating-a-repository/
git clone --bare https://github.com/exampleuser/old-repository.git
cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git

#ERROR
	error: Your local changes to the following files would be overwritten by merge:
		README.txt
		app.sh
		build.prod.sh
		config/webpack.common.js
	Please, commit your changes or stash them before you can merge.
	Aborting
#SOLUTION
	git stash
	git stash pop

#ERROR
	GitCommandFailedException: GitCommandFailedException: Command 'fetch' failed in /ffp/opt/couchpotato (128):
	fatal: unable to access 'https://github.com/RuudBurger/CouchPotatoServer.git/': SSL certificate problem: unable to get local issuer certificate
#SOLUTION
	#ignore ssl certificate
	git config --system http.sslverify false -> set in /etc/gitconfig
	#or use the Mozilla's CA Bundle certificate:
	wget -nv http://curl.haxx.se/ca/cacert.pem -O /ffp/etc/ssl/cert.pem
	git config --system http.sslcainfo /ffp/etc/ssl/cert.pem
#SOLUTION
	# export site certificate chain
	cd $HOME
	openssl s_client -connect adrhc.go.ro:443 -showcerts </dev/null 2>/dev/null | openssl x509 -outform PEM > adrhc.go.ro.pem
	openssl x509 -in adrhc.go.ro.pem -out adrhc.go.ro.crt
	# install site certificate chain
	su root
	root@gigi:~# cat adrhc.go.ro.crt >> /etc/ssl/certs/ca-certificates.crt
	CTRL+D
#SOLUTION
	/etc/ssl/certs/ca-certificates.crt is the result of "curl-config --ca"
	sudo 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"
Remove last commit from remote repository (e.g. origin)
git push origin +HEAD^:master
git reset --hard origin/master
This only works if the remote repository has the configuration:
[receive]
        denyNonFastForwards = false

Use master branch for angular2-websocket library in package.json
http://www.devthought.com/2012/02/17/npm-tricks/
https://github.com/afrad/angular2-websocket
package.json:
    "angular2-websocket": "https://github.com/afrad/angular2-websocket.git#8cb292ea25d5306dd516ece4480cf2d3b8263138",
    "angular2-websocket": "git+https://172.17.20.43/angular2-websocket.git",

Install own private repository
http://stackoverflow.com/questions/10386310/how-to-install-a-private-npm-module-without-my-own-registry
npm install "git+https://172.17.20.43/angular2-websocket.git" --save generates in package.json:
"angular2-websocket": "git+https://172.17.20.43/angular2-websocket.git"
npm install "git+https://172.17.20.43/angular2-websocket.git#master" --save generates in package.json:
"angular2-websocket": "git+https://172.17.20.43/angular2-websocket.git#master"

Force update from git when running npm install
npm install "git+https://172.17.20.43/angular2-websocket.git#master" --force

Fork after already cloning
git clone https://github.com/adrhc/h264-live-player
git remote -v
git remote rm origin
git remote add upstream https://github.com/131/h264-live-player.git
git remote add origin https://github.com/adrhc/h264-live-player.git
git branch --set-upstream-to=origin/master
git config core.autocrlf input -> linux only
git fetch --all
git pull upstream master

Checkout a remote branch
git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/foo
  remotes/origin/master
git branch --track foo remotes/origin/foo
git checkout foo
or in 1 step
git checkout --track -b foo remotes/origin/foo

Make an existing Git branch track a remote branch
Just switch the remote for the current branch:
git branch -u origin/current-branch
When current branch is foo switch the remote while also pushing:
git push -u origin foo

Checkout all remote branches
https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
git branch -a
for b in `git branch -r | grep -v -- '->'`; do echo "${b##origin/}"; done
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
# for already existing branches will fail with no undesirable consequences, e.g.:
fatal: A branch named 'master' already exists.

Show which remote branch is tracked by which local one
git branch -vv
git status -sb
git status | grep 'Your branch is up-to-date with'

Create a local branch then the remote counterpart
You'll have to make sure there's no "refactor" branch (but could be refactor/...).
git refactor maven-modules    -> using https://github.com/tj/git-extras
git push -u origin refactor/maven-modules

Remove a local branch
git branch -d feature/app-init

Remove a remote branch
git push origin --delete feature/app-init

Remove all stale branches
git remote prune origin --dry-run -> report what branches will be pruned
git fetch -p -> fetches and prunes all origins

Find branch first commit or creation date
git reflog show --no-abbrev branch-name

revert pushed commit after a wrong merge from branch X into master
we already are on master after merging the wrong branch ...
git revert -m 1 last-commit-id-meaning-the-merge-commit

git stash examples
git stash list
git stash save '[2017.11.01] before liquibase'
git stash show stash@{0}
git stash apply

how to remove a remote repository
showing the remotes
git remote -v
deleting the remote
git remote rm remote_name_from_showed_above

merge current branch into another without leaving the current
git merge-into target-branch-name

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.