~/cheatsheets/git
Published on

Git

Checkout single file from another branch

git checkout branch_name -- file.go

Squash commits

git rebase -i HEAD~[NUMBER OF COMMITS]

Find a deleted file in commit history

If you do not know the exact path you may use

git log --all --full-history -- "**/thefile*"

If you know the path the file was at, you can do this:

git log --all --full-history -- {path-to-file}

This should show a list of commits in all branches which touched that file. Then, you can find the version of the file you want, and display it with...

git show {SHA} -- {path-to-file}

Or restore it into your working copy with:

git checkout {SHA}^ -- {path-to-file}

Note the caret symbol (^), which gets the checkout prior to the one identified, because at the moment of <SHA> commit the file is deleted, we need to look at the previous commit to get the deleted file's contents

Remove file that was previous tracked and is now in .gitignore

Single file:

git rm --cached {file}

Whole folder:

git rm -r --cached {folder}

Checkout every repo under a group

Option 1 (one-liner)

for repo in $(curl -s --header "PRIVATE-TOKEN: your_private_token" https://<your-host>/api/v4/groups/<group_id> | jq -r ".projects[].ssh_url_to_repo"); do git clone $repo; done;

To include subgroups, add include_subgroups=true query param

https://<your-host>/api/v4/groups/<group_id>?include_subgroups=true

Option 2 (avoid needing a private token)

On the GitLab UI, navigate to https://<your-host>/api/v4/groups/<group_id>, copy the contents and save to a file (e.g. /tmp/group.json). Then, perform the same steps as before, instead using the file and avoiding the need to curl / a private token.

for repo in $(cat /tmp/group.json | jq -r ".projects[].ssh_url_to_repo"); do git clone $repo; done;

To ignore archived repositories:

for repo in $(cat /tmp/group.json | jq -r '.projects | .[] | select(.archived == false) | .ssh_url_to_repo'); do git clone $repo; done;