Bash-git-commands

From I Will Fear No Evil
Jump to navigation Jump to search

Git commands that are useful for bash scripts.

  • Retrieve the hash for the current commit inside whatever branch we are in
  • This also works for submodules as long as you are within the submodule directory in question
git rev-parse HEAD
  • Change the git remote from http(s) to git@ can be done this way:
sed -i 's|https://URL/|git@URL:|g' ./.git/config
  • If you want to update the submodules
sed -i 's|https://URL/|git@URL:|g' ./.gitmodules
git submodule sync --recursive
sed -i 's|https://URL/|git@URL:|g' ./.git/modules/PATH/config

If you want to create a PR/MR in gitlab via the CLI

  • you must have write privs
  • Make very sure you are in a NEW branch so you do not screw things up.
    • Using a date or epoch timestamp is reasonable
git branch foo
git checkout foo
--- Make your changes ---
git add -A
git commit -m "Generic commit message"
git push -o merge_request.create -o merge_request.title="A spiffy title" -o merge_request.description="A short description"

Find the dates associated with your git branches

for k in `git branch | perl -pe s/^..//`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r

Retrieve the current commit hash (to use in scripts)

git rev-parse --verify HEAD | awk '{print $1}'

Reset a single file:

git checkout HEAD -- my-file.txt