GITHUB CHEAT SHEET
Tags: github, git
Published: 2021-12-13 12:41:20

# GITHUB CHEAT SHEET

## STARTING A PROJECT

Forked projects or existing projects
```bash
cd ~/GITHUB/manolosolalinde
git clone <url>
git config credential.helper store
```

Create remote repository
```bash
cd ~/GITHUB/manolosolalinde
mkdir <reponame>
# deprecated: curl -u 'manolosolalinde@gmail.com' -d '{"name":"'"${PWD##*/}"'"}' https://api.github.com/user/repos
# token from https://github.com/settings/tokens
curl -i -H "Authorization: token <GET FROM https://github.com/settings/tokens> " \
    -d '{
        "name": "'"${PWD##*/}"'", 
        "description": "This is your first repository",
        "homepage": "https://github.com",
        "private": true,
        "has_issues": true,
        "has_projects": true,
        "has_wiki": true
        }' \
    https://api.github.com/user/repos
echo "# TODO Readme" >> README.md
```

Start Local Repository and push first commit
```bash
git init
git config credential.helper store
git add .
git commit -m "first commit"
git remote add origin https://github.com/manolosolalinde/${PWD##*/}.git
git push -u origin master
```

Change local repository:
```bash
git remote show origin
git remote set-url origin https://github.com/manolosolalinde/${PWD##*/}.git
```

## DELETING ALL HISTORY
```bash
cd <localrepofolder>
rm -rf .git
Start Repository (as above)
git push -u --force origin master
```
## DELETING A FULL REMOTE REPO
```bash
cd <REPONAME>
curl -X DELETE -H 'Authorization: token replacewithtoken' https://api.github.com/repos/manolosolalinde/${PWD##*/}
```
Get Delete Auth token
<a href="https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization">https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization</a>
```bash
curl -v -u 'manolosolalinde@gmail.com' -X POST https://api.github.com/authorizations -d '{"scopes":["delete_repo"], "note":"token with delete repo scope"}' >> token.json
```
## INITIAL GLOBAL SETUP
```bash
git config --global user.name "Manuel Solalinde"
git config --global user.email "manolosolalinde@gmail.com"
```

## UNTRACK FILES already added to git repository based on .gitignore
```bash
git rm -r --cached .
git add .
git commit -m ".gitignore fix"
```
    rm is the remove command
    -r will allow recursive removal
    --cached will only remove files from the index. Your files will still be there.
    The . indicates that all files will be untracked. 
    You can untrack a specific file with git rm --cached foo.txt (thanks @amadeann).



## GENERAL PURPOSE

http://www.cheat-sheets.org/saved-copy/github-git-cheat-sheet.pdf

`git config --global user.name "Manuel Solalinde"`\
Sets the name you want atached to your commit transactions

`git remote add origin git@github.com:manolosolalinde/newrepo.git`\
Add origin for existing repository

`git config --global user.email "manolosolalinde@gmail.com"`\
Sets the email you want atached to your commit transaction

`git init [project-name]`\
Creates a new local repository with the specified name

`git clone [url]`\
Downloads a project and its entire version history

`git status`\
Lists all new or modified files to be commited

`git add [file]`\
Snapshots the file in preparation for versioning

`git reset [file]`\
Unstages the file, but preserve its contents

`git diff`\
Shows file differences not yet staged

`git diff --staged`\
Shows file differences between staging and the last file version

`git commit -m "[descriptive message]"`\
Records file snapshots permanently in version history

`git branch`\
Lists all local branches in the current repository

`git branch [branch-name]`\
Creates a new branch

`git checkout [branch-name]`\
Switches to the specified branch and updates the working directory

`git merge [branch]`\
Combines the specified branch’s history into the current branch

`git branch -d [branch-name]`\
Deletes the specified branch

## Tags

more info on tags: <a href="https://git-scm.com/book/en/v2/Git-Basics-Tagging">https://git-scm.com/book/en/v2/Git-Basics-Tagging</a>

`git tag -a v1.4 -m "my version 1.4"` \
Creates a tag

`git tag`
List all tags

`git tag -l "v1.8.5*"` \
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
Check all tags with that name

`git show v1.4` \
Show Tag information

`git log --graph --decorate --oneline`
Show Branch History information

## Working with multiple versions of a project

Interesting post: <a href="https://stackoverflow.com/questions/12153405/how-to-manage-multiple-versions-of-a-project">https://stackoverflow.com/questions/12153405/how-to-manage-multiple-versions-of-a-project</a>


### Example 

In my case, I have two version of the same software that the basics are the same but each version has some different features.

So I create two `worktree` that means, create two relevant long-running branches beside the master.
```bash
    $git worktree add -b version-silver ..\version-silver master
    $git worktree add -b version-gold ..\version-gold master
```
Then I have:
```bash
    $git branch
    master  # base stuff here
    version-silver # some normal features
    version-gold # some better features
```
There is one repository, but I have 3 separate folders beside each other for each branch above. And make the common changes in master. then merge it with both other versions.
```bash
    cd master
    vim basic.cpp
    git add .
    git commit -m "my common edit on basic.cpp"
    cd ..\version-silver
    vim silver.cpp
    git add .
    git commit -m "my specific edit on silver.cpp"
    git merge master # here i get the basic.cpp latest changes for silver project
    cd ..\version-gold
    git merge master # here i get the basic.cpp latest changes for gold project
```

### Copy changes of `only last commit` from branch A to branch B

[Stackoverflow - Commit to multiple branches at the same time](https://stackoverflow.com/a/18529576/9831182)

```bash
git checkout A
git commit -m "Fixed the bug x"
git checkout B
git cherry-pick A
```



## Problems

### Problems with vscode and credentials

<a href="https://stackoverflow.com/questions/34400272/visual-studio-code-always-asking-for-git-credentials">https://stackoverflow.com/questions/34400272/visual-studio-code-always-asking-for-git-credentials</a>

You should be able to set your credentials like this:
```
git remote set-url origin https://<USERNAME>:<PASSWORD>@bitbucket.org/path/to/repo.git
```

You can get the remote url like this:
```
git config --get remote.origin.url
```

My problem was solved with:
```
cd "C:\Program Files\Git\mingw64\libexec\git-core"
git-credential-manager.exe uninstall
```