Last active
March 14, 2021 19:02
-
-
Save tomarv2/1bf0d86cfd6e648a55122bc9bcb3a756 to your computer and use it in GitHub Desktop.
Git cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ----------------------------------------------------- | |
| # Cleanup git history | |
| # ----------------------------------------------------- | |
| # NOTE: DO NOT DELETE the .git folder it may cause problems in git repository. | |
| # If we want to delete commits history, but keep the code, please try below: | |
| # Check out to a temporary branch: | |
| git checkout --orphan temp_branch | |
| # Add files: | |
| git add -A | |
| # Commit the changes: | |
| git commit -am "Initial commit" | |
| # Delete the old branch: | |
| git branch -D main | |
| # Rename temporary branch to main: | |
| git branch -m main | |
| # Force update to our repository: | |
| git push -f origin main | |
| # ----------------------------------------------------- | |
| # Create new repo using cli | |
| # ----------------------------------------------------- | |
| # Install gh: | |
| brew install gh | |
| # Set credentials: | |
| gh auth login | |
| # Create repo using cli: | |
| gh repo create <repo_name> --private --confirm | |
| # Add README.md and initialize: | |
| git clone https://github.com/<user_name>/<repo_name>.git | |
| cd "<repo_name>" | |
| echo "<repo_name>" >> README.md | |
| git init | |
| git add README.md | |
| git commit -m "first commit" | |
| git branch -M main | |
| git push -u origin main | |
| # ----------------------------------------------------- | |
| # Git merge previous commits into one | |
| # e.g. merge commits to develop branch | |
| # difference between option 1 and 2: | |
| # fixup => like "squash", but discard this commit's log message | |
| # ----------------------------------------------------- | |
| # Option 1: | |
| git rebase -i HEAD~3 | |
| # Output of above command: | |
| # pick 03ba590 . | |
| # pick 8d2bc42 . | |
| # pick 4bfb78e Add changes | |
| # Change lines as below: | |
| # pick 03ba590 . | |
| # squash 8d2bc42 . # change pick to sqash | |
| # squash 4bfb78e Add changes # change pick to sqash | |
| # Save and exit and update commit message | |
| git push -f origin develop | |
| # ----------------------------------------------------- | |
| # Option 2: | |
| git rebase -i HEAD~3 | |
| # Output of above command: | |
| # pick 03ba590 . | |
| # pick 8d2bc42 . | |
| # pick 4bfb78e Add changes | |
| # Change lines as below: | |
| pick f886a55 . | |
| fixup bd35bd0 . | |
| fixup a62df43 . | |
| # Save and exit and update commit message | |
| git push -f origin develop | |
| # ----------------------------------------------------- | |
| # Move from HTTPS to SSH login | |
| # ----------------------------------------------------- | |
| Generate a new SSH key: | |
| ssh-keygen -t rsa -C "[email protected]" | |
| Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings (https://github.com/settings/keys). | |
| Test SSH key: | |
| ssh -T [email protected] | |
| git remote set-url origin [email protected]:username/your-repository.git (ssh url from github) | |
| GIT_SSH_COMMAND='ssh -i id_rsa -o IdentitiesOnly=yes' git clone [email protected]:username/demo.git | |
| # ----------------------------------------------------- | |
| # Configuring multiple github accounts | |
| # ----------------------------------------------------- | |
| # Create two ssh keys an upload one to work github and another to personnal github | |
| # your .ssh/config file should look like this | |
| cat ~/.ssh/config | |
| # Personnal | |
| Host github-personnal # name can be anything | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/personnal_github/id_rsa # path to personnal github private key | |
| IdentitiesOnly yes | |
| # Work | |
| Host github-work | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/id_rsa # path to work github private key | |
| IdentitiesOnly yes | |
| # Update origin of github repo | |
| git remote origin set-url git@github-personnal:<github_user>/<github_repo>.git |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment