~cyborg/php-blog

5a9966e55d7da45733b8549429b764a5897420d9 — cyborg 6 months ago fe42f82
update post 028 and add post 035
M _data/metadata.json => _data/metadata.json +12 -1
@@ 1,9 1,20 @@
[
  {
    "id": "035",
    "title": "Changing Git Authors",
    "author": "Christian Cleberg",
    "description": "Learn how to change Git author names and emails with this quick bash script.",
    "tag": "personal",
    "created": "2021-05-30 00:00:00",
    "modified": "2021-05-30 00:00:00",
    "link": "https:\/\/blog.cleberg.io\/post\/changing-git-authors.html",
    "published": "Yes"
  },
  {
    "id": "034",
    "title": "Jumping Back Into Photography",
    "author": "Christian Cleberg",
    "description": "",
    "description": "A quick description of my current photography kit.",
    "tag": "personal",
    "created": "2021-04-28 00:00:00",
    "modified": "2021-04-28 00:00:00",

M posts/028-how-to-clone-all-github-repositories.md => posts/028-how-to-clone-all-github-repositories.md +66 -5
@@ 1,20 1,20 @@
## Use a Bash Script!
## Cloning from GitHub

If you're like me and use a lot of different devices (and sometimes decide to just wipe your device and start with a new
OS), you probably know the pain of cloning all your old code repositories down to your local file system.

If you're using GitHub, you can easily clone all of your code back down in just seconds. First, create a bash script - I
do so by opening a new file in `gedit`, but you can use `nano`, `vim`, or something else:
do so by opening a new file in `nano`, but you can use `gedit`, `vim`, or something else:

```bash
gedit clone_github_repos.sh
nano clone_github_repos.sh
```

Next, paste in the following information. Note that you can replace the word `users` in the first line with `orgs` and
type an organization's name instead of a user's name.

```bash
CNTX={users}; NAME={YOUR-USERNAME}; PAGE=1
CNTX=users; NAME=YOUR-USERNAME; PAGE=1
curl "https://api.github.com/$CNTX/$NAME/repos?page=$PAGE&per_page=100" |
  grep -e 'git_url*' |
  cut -d \" -f 4 |


@@ 33,13 33,74 @@ Now you can run the script and should see the cloning process begin.
./clone_github_repos.sh
```

## Cloning from Sourcehut

I haven't fully figured out how to directly incorporate Sourcehut's GraphQL API into a bash script yet, so this one will
take two steps.

First, log-in to Sourcehut and go to their [GraphQL playground for Git](https://git.sr.ht/graphql). Next, paste the
following query into the left box:

```graphql
query {
  me {
    canonicalName
    repositories() {
      cursor
      results {
        name
      }
    }
  }
}
```

The output on the right side will give you an object of all your repositories. Just grab that text and remove all the
characters such as quotation marks and curly brackets. You will need a single-line list of space-separated values for
the next step.

Now let's create the bash script:

```bash
nano clone_sourcehut_repos.sh
```

Next, paste the following bash script in with the list of repositories you obtained above and replace `cyborg`
with your username.

Note that this uses the SSH-based Git cloning method (e.g. `git@git...`), so you'll need to ensure you have set-up
Sourcehut with your SSH key.

```bash
repos=(sandbox lincoln-incidents color-generator world-incarceration awfice project-401k blog cleberg.io dark-journalstar dark-hackernews dark-omgubuntu dark-sourcehut vox-populi space-wiki rust-cli-game reminiscecleberg.com reminisce-app photos number-game nba michelangelo markdown-journal gemlog deprecated data-science)

# List all sub-directories in the current directory
for repo in "${repos[@]}"
do
    # Clone
    git clone git@git.sr.ht:~cyborg/$repo
done
```

Finally, save the bash script and make it executable.

```bash
chmod a+x clone_sourcehut_repos.sh
```

Now you can run the script and should see the cloning process begin.

```bash
./clone_sourcehut_repos.sh
```

## Moving Repositories to a New Host

Now that you have all of your code repositories cloned to your local computer, you may want to change the remote host
(e.g. moving from GitHub to GitLab). To do this, let's create another bash script:

```bash
gedit change_remote_urls.sh
nano change_remote_urls.sh
```

Past the following information and be sure to change the URL information to whichever host you are moving to. For this

A posts/035-changing-git-authors.md => posts/035-changing-git-authors.md +64 -0
@@ 0,0 1,64 @@
## Changing Git Author/Email Based on Previously Committed Email

Here's the dilemma: You've been committing changes to your git repository with an incorrect name or email (or multiple
repositories) and now you want to fix it. Luckily there's a semi-reliable way to fix that. While I have never
experienced issues with this method, some people have warned that it can mess with historical hashes and integrity of
commits, so use this method only if you're okay accepting that risk.

Okay, let's create the bash script:

```bash
nano change_git_authors.sh
```

The following information can be pasted directly into your bash script. The only changes you need to make are to the following variables:
* `OLD_EMAIL`
* `CORRECT_NAME`
* `CORRECT_EMAIL`

```bash
#!/bin/sh

# List all sub-directories in the current directory
for dir in */
do
    # Remove the trailing "/"
    dir=${dir%*/}
    # Enter sub-directory
    cd $dir

    git filter-branch --env-filter '

    OLD_EMAIL="hello@cleberg.io"
    CORRECT_NAME="cyborg"
    CORRECT_EMAIL="hello@cleberg.io"

    if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    then
        export GIT_COMMITTER_NAME="$CORRECT_NAME"
        export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    then
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags

    git push --force --tags origin 'refs/heads/*'

    cd ..
done
```

Finally, save the bash script and make it executable.

```bash
chmod a+x change_git_authors.sh
```

Now you can run the script and should see the process begin.

```bash
./change_git_authors.sh
```
\ No newline at end of file