Friday, 15 October 2021

Git cheat sheet

See branch

# see remote branch
git branch -r

# find remote branches which has release in name and sorted time desc
git branch -r --sort=-committerdate |grep release

Git rebase

# rebase 5 commit into one
git rebase -i HEAD~5

Patch file

git diff develop feature_br > test.diff
patch -p1 < test.diff

Show details of git server

git remote show origin

Show files changes in commits

git show --name-status

Push branch to remote origin and create a PR

git push -u origin feature/my-ticket
//the same as
git push --set-upstream origin feature/my-ticket

Create tag

//list tag by date
git log --tags --simplify-by-decoration --pretty="format:%ai %d"

//list tag by pattern
git tag -l "release-cron*"

//show tag details including tag message
git show release-cron-v2021v05.1

//create tag 2.0.1 in local
git tag -a 2.0.1 -m "create tag message"

//push tag
git push origin 2.0.1


//checkout tag
 git checkout tags/2.0.1 -b mytag_branch

Remote origin

// find remote origin
git config --get remote.origin.url

// set remote origin. In bitbucket, may need username in url such as 
git remote set-url origin [you repo URL>

git remote set-url origin https://lshen@git.testcompany.dev/scm/smp/looksearch.git

The below is a test to switch between ssh and https repo url. This repo has two url

ssh:  git@github.com:revenuewire/translation.git
https: https://github.com/revenuewire/translation.git
git clone  https://github.com/revenuewire/translation.git
//try find url. output: https://github.com/revenuewire/translation.git
git config --get remote.origin.url

//update url
git remote set-url origin  git@github.com:revenuewire/translation.git

//try find url again. output: git@github.com:revenuewire/translation.git
git config --get remote.origin.url

//see all config including origin
git config --list

Other commands

// see if in git repository. If in repository, get true.
//if not in, get "fatal: not a git repository (or any of the parent directories): .git"
git rev-parse --is-inside-work-tree
//create a code review in bitbucket
git push --set-upstream origin branch_name

You are in 'detached HEAD' state

Good explanation

Will get the detached HEAD state by checking out a commit directly

git checkout acb8a20
git status
HEAD detached at acb8a20
nothing to commit, working tree clean

To go back normal. Assume you are at master branch before

git checkout master

Want to stay here, make changes and keep changes

git branch new_branch_name
git checkout new_branch_name

If HEAD is synonymous with the last commit in the current branch, HEAD is attached to a branch.

Create a pull request in GitHub

//push the feature branch
 git push -u origin feature/JFR-3864
 
//Then shoud see something as
//remote: Create a pull request for 'feature/JFR-3864' on GitHub by visiting:
//remote: https://github.com/myconabsbsbsbs/bar/pull/new/feature/JFR-3864

Use the link to create a pull request. 
You can specify a reviewer when create pull request

Move repository from Bitbucket to GitHub

mkdir my_temp

cd my_temp

git clone --mirror https://git.mybitbucket.dev/scm/pay/my-api.git

cd my-api.git/

git remote set-url --push origin git@github.com:my/my-api.git

git push --mirror

Remove bigfile from Git repository

BFG Repo-Cleaner

git clone --mirror https://git.mybitbucket.dev/scm/pay/my-api.git

java -jar bfg.jar --strip-blobs-bigger-than 100M my-api.git

cd my-api.git

git reflog expire --expire=now --all && git gc --prune=now --aggressive

Show when a branch is created

git checkout your_branch_name
git show --summary

No comments:

Post a Comment