Git
Some useful links
- Git Cheatsheet
- Cheatsheet by GitHub
- Cheatsheet by GitLab
- Cheatsheet by Atlassian
- LearnGitBranching
- Pro Git book (2nd Edition)
Branches
CREATE
Create a branch
git branch <branch-name>
Create a branch and switch to it
common
git checkout -b <branch-name>
new
git switch -c <branch-name>
If there is a remote branch, but you need to create a local one and link to the remote one
git branch --track <local-branch> origin/<remote-branch>
git branch <local-branch> origin/<remote-branch>
git checkout -b <local-branch> origin/<remote-branch>
If there is already a local one with the same name, then it will link to the remote one
git branch -f <local-branch> origin/<remote-branch>
If there is a local branch and you need to create a remote one with tracking
git push -u origin <branch-name>
If there are both local and remote branches and you need to make tracking
git branch -u origin/<remote-branch-name> <local-branch-name>
If you need to remove tracking
git branch --unset-upstream <branch-name>
MOVE
Move branch pointer to a different commit (destination-commit can be a branch name)
git reset --hard <destination-commit>
git branch –f <branch-name> <destination-commit>
DELETE
DELETE remote branch (remote-name usually origin)
git push -d <remote-name> <branch-name>
Don’t forget to do a git fetch --all --prune or git fetch --all -p on other machines after deleting the remote branch on the server.
After deleting the local branch with git branch -d and deleting the remote branch with git push -d origin other machines may still have “obsolete tracking branches” (to see them do git branch -a). To get rid of these do git fetch --all --prune
DELETE local branch
git branch -d <branch-name>
The -D option is an alias for –delete –force, which deletes the branch “irrespective of its merged status”
git branch -D <branch-name>
DELETE local remote-tracking branch
git branch -dr <remote>/<branch>
long
git branch --delete --remotes <remote>/<branch>
RENAME
Rename current branch
git branch -m <branch_name>
Rename another branch
git branch -m <old_branch_name> <new_branch_name>
Rename branch on the remote
Push the local branch and reset the upstream branch
git push origin -u <new_branch_name>
Delete the remote branch
git push origin --delete <old_branch_name>
VIEW
View branches that was merged with the current branch
git branch --merged
View remote branches that was merged with the current branch
git branch -r --merged
View branches that was’t merged with the current branch
git branch --no-merged
PRUNE
git remote prune origin
short
git fetch -p
long
git fetch --prune
Changes
STAGE
Stage all changes
git add .
Stage changes in interactive mode
git add -i
Stage changes in patch mode
git add -p
UNSTAGE
Unstage some changes (MOVE from Index (staging area) to Workspace)
new
git restore --staged <file>
old
git reset <file>
git reset HEAD <file>
Unstage all changes
new
git restore --staged .
common
git reset
old
git reset HEAD
DELETE
Delete changes in Workspace
new
git restore <file>
old
git checkout -- <file>
git checkout <file>
Delete all changes in Workspace
new
git restore .
old
git checkout .
Delete all changes in Index and Workspace
git reset --hard
git reset --hard HEAD
REMOVE FROM GIT
Remove file from Git tracking but keep it on disk
git rm --cached <file_name>
Remove file from the git repository and from the local disk
git rm <file_name>
Commits
Make an empty commit
git commit --allow-empty -m "empty commit"
Undo the last commit and move changes to Working Directory (changes in staging area will also move to Working Directory) (–mixed by default)
git reset HEAD^
Undo the last commit and move changes to the Index
git reset --soft HEAD^
Undo the last commit and move changes to the Working Directory
git reset --mixed HEAD^
Undo the last commit and DELETE changes (uncommitted ones too)
git reset --hard HEAD^
CHERRY PICK
Copy one commit to current branch
git cherry-pick <SHA>
Copy range of commits to current branch
git cherry-pick <SHA>..<SHA>
Config
Open global config file for editing
git config --global --edit
Show global config file content in terminal
git config --global --list
Configure name and email
git config --global user.name "Your Name"
git config --global user.email test@email.com
Ignore
Line endings
The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.
git config --global core.autocrlf input|true|false
| autocrlf | commit | checkout | When to Use |
|---|---|---|---|
| input | CRLF → LF | - | Unix, MacOS |
| true | CRLF → LF | LF → CRLF | Windows |
| false | - | - | Single platform |
Log
Filter
By number
git log -<number>
By date
git log --since=yyyy-mm-dd
git log --until="3 days ago"
git log --after=2.weeks --before=3.days
By author
git log --author="Name"
By commit message pattern
git log --grep="pattern"
By commits range
git log <SHA>..<SHA>
By file name (only shows commits that changed that file)
git log <file_name>
Decoration
Print commit with the names of changed files
git log --name-only --oneline
Remotes
Add remote
git remote add origin <repository_url>
Change remote
git remote set-url origin <repository_url>
View remote
View remote name
git remote
View remote name and url
git remote -v
View more info about remote
git remote show <remote_name>
Delete remote
git remote rm origin
SSH
Create SSH key
-t specifies the key type
-b specifies the key size (in bits)
-C for comment to help identify your ssh key (optional)
-f for the file name where ssh key get saved
ssh-keygen -t rsa -b 4096 -C "<your email>" -f $env:USERPROFILE/.ssh/<key_name>
Add SSH key to SSH Agent
Add SSH private key to SSH Agent
ssh-add $env:USERPROFILE/.ssh/<key_name>
If previous command doesn’t work try the following or see this link
Check if SSH Agent service is running
Get-Service ssh-agent
Stop SSH Agent service
Stop-Service ssh-agent
Start SSH Agent service
Start-Service ssh-agent
Add SSH public key to the Github
- Copy content of
<key_name>.pub - Goto Settings > SSH and GPG keys > New SSH Key
- Paste your copied public key and give it a Title of your choice
Create SSH Config file and add Host Entries
If config file not already exists then create one ($env:USERPROFILE/.ssh/config)
Host github.com-<username>
HostName github.com
User git
IdentityFile C:/Users/<user name>/.ssh/<key_name>
IdentitiesOnly yes
Host github.com-<username>
- This is the hostname for which these settings apply.
- Every time you connect to this host via SSH, the following configuration will be applied.
- You can use different settings for other hosts (for example, for other servers).
HostName github.com
- Specifies the actual hostname to connect to.
User git
- GitHub requires the user to be specified as
gitfor all SSH connections.
IdentityFile C:/Users/<user name>/.ssh/<key_name>
- Specifies the path to your private SSH key, which will be used to authenticate to GitHub.
IdentitiesOnly yes
- Causes SSH to only use the keys specified in the configuration file (via IdentityFile) and ignore any other keys that may be loaded into the SSH agent.
- This helps if you have multiple keys and want to use a specific key for a specific host.
Cloning existing repository or adding remote for the local one
Clone existing repository
git clone git@github.com-<username>:<username>/<repo_name>.git
Add remote to local repository
git remote add origin git@github.com-<username>:<username>/<repo_name>.git
Change remote in local repository
git remote set-url origin git@github.com-<username>:<username>/<repo_name>.git
Finally
Don’t forget to specify user.name and user.email in every repository freshly cloned or existing before.
git config user.name "Your name"
git config user.email "your@email.com"
If you set GPG signing globally you can remove this for the local repository
git config commit.gpgSign false
If you have new GPG key you can set it for the local repository
git config user.signingkey <new-gpg-key-id>
Useful
Commands
Show SSH key list
ssh-add -l
Delete SSH key from SSH Agent
ssh-add -d $env:USERPROFILE/.ssh/<key_name>
Check if your SSH client can connect to GitHub
ssh -T git@github.com
or more verbose
ssh -vT git@github.com
Links
- (Stack Overflow) Multiple GitHub accounts on the same computer?
- (GitHub Gist) How To Work With Multiple Github Accounts on a single Machine
Stash
SAVE
new
git stash
git stash push
git stash push -m "message"
old
git stash save "message"
VIEW
git stash list
EJECT
Get content from stash (and delete from there)
git stash pop
Get content from stash (and leave it there)
git stash apply
DELETE
git stash drop
Tags
CREATE
Create lightweight tag
git tag <tag_name> <SHA>
Create annotated tag
git tag -am "message" <tag_name> <SHA>
Create a remote tag when local exists
git push origin <tag_name>
Push all local tags to remote
git push origin --tags
VIEW
View tag info
git show <tag_name>
View tags list
git tag -l
View tags list by pattern
git tag -l "pattern"
View tags list with first line of each annotation
git tag -n
DELETE
Delete local tag
git tag -d <tag_name>
Delete remote tag
git tag -d origin <tag_name>
Useful Git Commands
Show the branch on which a specific commit was originally created
git reflog show --all | FINDSTR <HASH>
Worktrees
CREATE
Create a worktree for an existing branch using the same name as the working directory
git worktree add path/to/folder/<existing-branch-name>
Create a worktree for an existing branch using a different name than the working directory
git worktree add path/to/folder/ <existing-branch-name>
Create a worktree with a new branch using the same name as the working directory
git worktree add -b <new-branch-name> path/to/folder/<new-branch-name>
Create a worktree with a new branch using a different name than the working directory
git worktree add -b <new-branch-name> path/to/folder/
VIEW
Show all worktrees
git worktree list
DELETE
Remove a worktree
git worktree remove path/to/folder/
or (link)
rm -rf path/to/worktree
git worktree prune
git branch -D <branch-name>
IDE
Visual Studio
Visual Studio Main Shortcuts
Editing
Ctrl+K,C – comment lineCtrl+K,U – uncomment lineCtrl+D – duplicate lineCtrl+Shift+L – delete line without adding it to the clipboardAlt+<Up/Down arrows> – move line up/down
Ctrl+K,F – format the selected blockCtrl+K,D – format the entire fileCtrl+K,E – clean up the code in the file (according to the rules you set, for example, removing unnecessary usings)
Ctrl+K,S – insert snippet (if, try, for block, etc.), or enclose the selected code in a blockAlt+Shift+<arrows> (Alt+<mouse selection>) – edit by columnsCtrl+Alt+<place the cursor with the mouse> – multi-cursor input (edit in several places at once)
Navigation
Ctrl+G – go to lineCtrl+K,K – leave bookmarkCtrl+K,N – go to next bookmark
Ctrl+, (Ctrl+T) – search by code in projectCtrl+Q – search by IDE tools
F12 (Ctrl+<mouse click>) – go to definitionAlt+F12 – peek definition (definition hint in inline window)Ctrl+F12 – go to implementationShift+F12 – find all references
Ctrl+Shift+space – method arguments hintCtrl+[,S – mark current file in solution explorerCtrl+; – search in solution explorer
Ctrl+M,O – collapse all methods to definitionsCtrl+M,P – expand all methodsCtrl+M,M – collapse/expand the current methodCtrl+M,L – collapse/expand all outlining
Shift+Alt+. – Add the next matching text as a selectionShift+Alt+; – Add all matching text as selectionsShift+Alt+, – Remove last selected occurrenceShift+Alt+/ – Skip next matching occurrence
For ASP.NETCtrl+M,G – go from a controller method to a view or vice versa
Refactoring
Ctrl+. (Alt+Enter) – refactoring hintsCtrl+R,R – renameCtrl+R,M – extract selected code to methodCtrl+R,I – extract interface from classCtrl+R,V (Ctrl+R,O) – reorganize method parameters
Snippets
"prop",Tab,Tab – create a property"ctor",Tab,Tab – create a constructor
Tab,Tab also works for other popular keywords (if, for, switch, etc.)
Debugging
F9 – create a breakpointCtrl+Alt+B – open window with all breakpointsCtrl+Shift+F9 – delete all breakpoints
SQL
DML (Data Manipulation Language)
DML commands are used for storing, retrieving, modifying, and deleting data.
SELECT - retrieve data from a database
INSERT - insert data into a database
UPDATE - updates existing data within a database
DELETE - delete records from a database
DDL (Data Definition Language)
DDL commands are used for creating, modifying, and dropping the structure of database objects.
CREATE - create database objects such as tables and views
ALTER - alters the structure of the existing database
DROP - delete objects from the database
TRUNCATE - delete all rows from the database
DCL (Data Control Language)
DCL commands are used to implement access control on database objects.
GRANT - give a user access privileges on database objects
REVOKE - withdraw user privileges that were previously given using GRANT
Tutorials and docs
SQL Server
Data Types
String Data Types
| Data type | Description | Max char length | Storage |
|---|---|---|---|
| char(n) | Fixed-length non-Unicode character data (n must be between 1 and 8000) | 8,000 | n bytes (uses one byte for each character) |
| varchar(n) | Variable-length non-Unicode character data (n must be between 1 and 8000) | 8,000 | n bytes + 2 bytes |
| varchar(max) | Variable-length non-Unicode character data | up to 2 GB | |
| nchar(n) | Fixed-length Unicode character data (n must be between 1 and 4000) | 4,000 | 2 * n bytes (uses two bytes for each character) |
| nvarchar(n) | Variable-length Unicode character data (n must be between 1 and 4000) | 4,000 | 2 * n bytes + 2 bytes (uses two bytes for each character) |
| nvarchar(max) | Variable-length Unicode character data | up to 2 GB | |
| binary(n) | Fixed-length binary data (n must be between 1 and 8000) | 8,000 | n bytes |
| varbinary(n) | Variable-length binary data (n must be between 1 and 8000) | 8,000 | actual length of data entered + 2 bytes |
| varbinary(max) | Variable-length binary data | 2GB |
Numeric Data Types
| Data type | Description | Storage |
|---|---|---|
| bit | Integer that can be 0, 1, or NULL | |
| tinyint | Allows whole numbers from 0 to 255 | 1 byte |
| smallint | Allows whole numbers between -32,768 and 32,767 | 2 bytes |
| int | Allows whole numbers between -2,147,483,648 and 2,147,483,647 | 4 bytes |
| bigint | Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 | 8 bytes |
| decimal(p,s) | Fixed precision and scale numbers. Allows numbers from -10^38 +1 to 10^38 –1. The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1 to 38. Default is 18. The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0 | 5-17 bytes |
| numeric(p,s) | Fixed precision and scale numbers. Allows numbers from -10^38 +1 to 10^38 –1. The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1 to 38. Default is 18. The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0 | 5-17 bytes |
| smallmoney | Monetary data from -214,748.3648 to 214,748.3647 | 4 bytes |
| money | Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 | 8 bytes |
| float(n) | Floating precision number data from -1.79E + 308 to 1.79E + 308. The n parameter indicates whether the field should hold 4 or 8 bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53. | 4 or 8 bytes |
| real | Floating precision number data from -3.40E + 38 to 3.40E + 38 | 4 bytes |
Date and Time Data Types
| Data type | Description | Storage |
|---|---|---|
| datetime | From January 1, 1753 to December 31, 9999 with an accuracy of 3.33 milliseconds | 8 bytes |
| datetime2 | From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds | 6-8 bytes |
| smalldatetime | From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute | 4 bytes |
| date | Stores a date only. From January 1, 0001 to December 31, 9999 | 3 bytes |
| time | Stores a time only to an accuracy of 100 nanoseconds | 3-5 bytes |
| datetimeoffset | The same as datetime2 with the addition of a time zone offset | 8-10 bytes |
| timestamp | Stores a unique number that gets updated every time a row gets created or modified. The timestamp value is based upon an internal clock and does not correspond to real time. Each table may have only one timestamp variable |
Other Data Types
| Data type | Description |
|---|---|
| sql_variant | Stores up to 8,000 bytes of data of various data types, except text, ntext, and timestamp |
| uniqueidentifier | Stores a globally unique identifier (GUID) |
| xml | Stores XML formatted data. Maximum 2GB |
| cursor | Stores a reference to a cursor used for database operations |
| table | Stores a result-set for later processing |