Git
"git" command examples
Information
Remote branches:
git remote show origin
All remote branches:
git ls-remote origin
Commit history using one line format:
git log -3 --pretty=oneline
48936b6c818df7335a2c879ec74c0d7f7ec443b1 (HEAD -> feature/JIRATASK-9, origin/feature/JIRATASK-9) JIRATASK-9 UI refactoring
59f206103dd2f4895bb13f54c35846ab8d3ce4f8 (origin/master, origin/HEAD) JIRATASK-4 Server side changes
9c4b9859f5a84af0ad92dd62f93a8947aa8cb37e JIRATASK-1 Initial commit
Commit history using one line format without branches information:
git log -3 --pretty=oneline --no-decorate
48936b6c818df7335a2c879ec74c0d7f7ec443b1 JIRATASK-9 UI refactoring
59f206103dd2f4895bb13f54c35846ab8d3ce4f8 JIRATASK-4 Server side changes
9c4b9859f5a84af0ad92dd62f93a8947aa8cb37e JIRATASK-1 Initial commit
Commit history using custom format:
git log -3 --pretty=format:"%h %ci /%ae/ (%p) : %s" | sed -E "s|/(.*)@.*/|/\1/|"
48936b6 2018-04-10 19:17:30 +0300 /john.smith/ (59f2061) : JIRATASK-9 UI refactoring
59f2061 2018-04-09 18:58:30 +0300 /ivan.ivanov/ (9c4b985) : JIRATASK-4 Server side changes
9c4b985 2018-04-09 14:20:21 +0300 /john.smith/ (g9c030a) : JIRATASK-1 Initial commit
Find changed modules in local non-pushed commits:
git cherry -v |
awk 'NR == 1 {print $2 "^"} END {print $2}' |
xargs | sed "s/ /../" |
xargs git diff --name-only |
awk -F "/" '{print $1}' |
sort -u
Actions
Create bare repository:
git init --bare sandbox.git
Clone and checkout a specific branch:
git clone -b remoteBranch git@git:myproject
Fetch remote branches:
git fetch
Fetch remote branches and checkout remote branch to local branch with the same name:
git fetch
git checkout remote-branch-name
Fetch and checkout specific remote branch to local branch with a different name:
git fetch origin pull-requests/11/from:localBranchName
git checkout localBranchName
Pull updates with rebase before push:
git fetch && git pull --rebase
Pull updates with rebase from master branch before push:
git fetch && git pull --rebase origin master
Make changes to last commit:
git add .
git commit --amend
Create new local branch from current local branch:
git checkout -b localBranchName
Associate current local branch with remote master branch:
git branch -u origin/master
Put Tag
git fetch && git pull --rebase
git log -1
git tag -a r-16.4-some -m 'R-16.4 Some' 66cb232
git show r-16.4-some
git push origin r-16.4-some
Release with Tag
# switch to master branch and refresh it, just in case
git checkout master
git fetch && git pull
# switch to release branch and refresh it
git checkout release
git fetch && git pull
# merge from master to release and push it
git merge master
git push origin release
# get merge commit id, put a tag with description on it and push it
git log -1
git tag -a r-16.2 -m 'R-16.2' 6a6e311
git show r-16.2
git push origin r-16.2
# switch to master branch, change target version and push it
git checkout master
vi logic.gradle
git commit -a -m '[MYPRO] logic.gradle: targetProductVersion: 16.2 -> 16.3'
git push origin master
Throw away local changes with reset to master branch:
git reset --hard origin/master
Throw away local changes with reset to specific commit:
git reset --hard 5d6524030a69
Merge example:
git merge origin/master --no-ff
git commit --amend
Merge conflicts resolution:
git pull --rebase
git mergetool
git rebase --continue
Change specific commit:
git rebase --interactive 9bc24^
# choose commit and replace "pick" with "edit" and then Save
git add .
git commit --amend --no-edit
git rebase --continue
Gerrit
Checkout:
git clone git@git:myproject
cd myproject
scp -p -P 29418 bob@git:hooks/commit-msg .git/hooks/
Checkout to submitted parent commit:
git checkout `wget -O- http://gerrit/changes/CHANGE_ID/submitted_together?o=NON_VISIBLE_CHANGES |
grep "current_revision" | tail -1 | awk -F\" '{print $4}'`^
Push changes:
git push ssh://bob@git:29418/myproject HEAD:refs/for/master
Change non-subbmitted commit:
git fetch http://gerrit/myproject refs/changes/19/8719/2 && git checkout FETCH_HEAD
git checkout -b temp
git fetch origin master
git rebase origin/master
git push ssh://bob@git:29418/myproject HEAD:refs/for/master
Force push to own repository:
git push git@git:bob HEAD:master --force
Create new branch:
git push ssh://bob@git:29418/myproject HEAD:develop
Configuration
git config --global user.name "John Smith"
git config --global user.email john.smith@db.com
git config --global core.longpaths true
git config --global mergetool.keepBackup false