DEV Community

Nestor Solalinde
Nestor Solalinde

Posted on

GITHUB CHEAT SHEET

GITHUB CHEAT SHEET

STARTING A PROJECT

Forked projects or existing projects

cd ~/GITHUB/manolosolalinde
git clone <url>
git config credential.helper store
Enter fullscreen mode Exit fullscreen mode

Create remote repository

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
Enter fullscreen mode Exit fullscreen mode

Start Local Repository and push first commit

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
Enter fullscreen mode Exit fullscreen mode

Change local repository:

git remote show origin
git remote set-url origin https://github.com/manolosolalinde/${PWD##*/}.git
Enter fullscreen mode Exit fullscreen mode

DELETING ALL HISTORY

cd <localrepofolder>
rm -rf .git
Start Repository (as above)
git push -u --force origin master
Enter fullscreen mode Exit fullscreen mode

DELETING A FULL REMOTE REPO

cd <REPONAME>
curl -X DELETE -H 'Authorization: token replacewithtoken' https://api.github.com/repos/manolosolalinde/${PWD##*/}
Enter fullscreen mode Exit fullscreen mode

Get Delete Auth token
https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization

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
Enter fullscreen mode Exit fullscreen mode

INITIAL GLOBAL SETUP

git config --global user.name "Manuel Solalinde"
git config --global user.email "manolosolalinde@gmail.com"
Enter fullscreen mode Exit fullscreen mode

UNTRACK FILES already added to git repository based on .gitignore

git rm -r --cached .
git add .
git commit -m ".gitignore fix"
Enter fullscreen mode Exit fullscreen mode
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).
Enter fullscreen mode Exit fullscreen mode

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: https://git-scm.com/book/en/v2/Git-Basics-Tagging

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: https://stackoverflow.com/questions/12153405/how-to-manage-multiple-versions-of-a-project

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.

    $git worktree add -b version-silver ..\version-silver master
    $git worktree add -b version-gold ..\version-gold master
Enter fullscreen mode Exit fullscreen mode

Then I have:

    $git branch
    master  # base stuff here
    version-silver # some normal features
    version-gold # some better features
Enter fullscreen mode Exit fullscreen mode

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.

    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
Enter fullscreen mode Exit fullscreen mode

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

Stackoverflow - Commit to multiple branches at the same time

git checkout A
git commit -m "Fixed the bug x"
git checkout B
git cherry-pick A
Enter fullscreen mode Exit fullscreen mode

Problems

Problems with vscode and credentials

https://stackoverflow.com/questions/34400272/visual-studio-code-always-asking-for-git-credentials

You should be able to set your credentials like this:

git remote set-url origin https://<USERNAME>:<PASSWORD>@bitbucket.org/path/to/repo.git
Enter fullscreen mode Exit fullscreen mode

You can get the remote url like this:

git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode

My problem was solved with:

cd "C:\Program Files\Git\mingw64\libexec\git-core"
git-credential-manager.exe uninstall
Enter fullscreen mode Exit fullscreen mode

Top comments (0)