Working with GIT

# -> best visual guide
# -> best interactive guide
# -> graphical clients listed

# 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

# 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
    Port 222
    User gigi
    ServerAliveInterval 30

# view commits on origin only
git fetch origin
git branch -a
* master
  remotes/origin/HEAD -> origin/master
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
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 -> 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
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
# -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
# exists in $HOME/.gitconfig
git config --global core.autocrlf input
git config --global --get core.autocrlf

# workflow example:
git clone
git checkout master
git remote add upstream
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
git clone --bare
cd old-repository.git
git push --mirror

	error: Your local changes to the following files would be overwritten by merge:
	Please, commit your changes or stash them before you can merge.
	git stash
	git stash pop

	GitCommandFailedException: GitCommandFailedException: Command 'fetch' failed in /ffp/opt/couchpotato (128):
	fatal: unable to access '': SSL certificate problem: unable to get local issuer certificate
	#ignore ssl certificate
	git config --system http.sslverify false -> set in /etc/gitconfig
	#or use the Mozilla's CA Bundle certificate:
	wget -nv -O /ffp/etc/ssl/cert.pem
	git config --system http.sslcainfo /ffp/etc/ssl/cert.pem
	# export site certificate chain
	cd $HOME
	openssl s_client -connect -showcerts </dev/null 2>/dev/null | openssl x509 -outform PEM >
	openssl x509 -in -out
	# install site certificate chain
	su root
	root@gigi:~# cat >> /etc/ssl/certs/ca-certificates.crt
	/etc/ssl/certs/ca-certificates.crt is the result of "curl-config --ca"
	sudo bash -c "echo -n | openssl s_client -showcerts -connect 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:
        denyNonFastForwards = false

Use master branch for angular2-websocket library in package.json
    "angular2-websocket": "",
    "angular2-websocket": "git+",

Install own private repository
npm install "git+" --save generates in package.json:
"angular2-websocket": "git+"
npm install "git+" --save generates in package.json:
"angular2-websocket": "git+"

Force update from git when running npm install
npm install "git+" --force

Fork after already cloning
git clone
git remote -v
git remote rm origin
git remote add upstream
git remote add origin
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
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
When current branch is foo:
git branch -u upstream/foo
Or, if local branch foo is not the current branch:
git branch -u upstream/foo foo

Checkout all remote 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
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

Leave a Reply

Your email address will not be published. Required fields are marked *

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