Author: Maxim Vasilyev
Contents:
- Info
- Command line
- Installation
- Lauch
- Main commands
- Usage
- Config
- Init
- Actions
- GitHub
- Registration
- Create repo
- SSH keys
- Connect local and remote repos
- Synchronization
- Navigation
- Hash
- Log
- HEAD
- File status
- Project structure
- README.md
- .gitignore
- Commits
- Commenting
- Time travelling
- Spotting differences
- Git is a program used for version control.
- VCS - Version Control System
- SCM - Source Control Management
- GitHub is a platform where developers store their projects.
⚠ macOS/Linux users already have Git installed, so they skip this part.
Windows users need to instal Git Bash:
- Visit Git official website.
- Download suitable
Standalone installer. - Launch installer.
- Make sure to check
Git Bash Hereoption. - Walk through the installer and click
Finish.
To lauch a terminal or CLI (Command Line Interface) you need to do:
macOS users press
Cmd+Spaceand typeTerminalLinux users press
Ctrl+Alt+TWindows users press
Win+Sand typeGit Bash
/- root directory~- home directory.- current directory..- parent directory⭥- to scroll commands from bufferTab- to autocomplete commandpwd- print working directorycd- change directoryls- list directory contents-a- display hidden content-l- display as list
touch- create filemkdir- make directory-p- make nested directories
cp- copy file(s) to somewherepbcopy- copy data into buffermv- move file(s) to somewherecat- concatenate and print (only text files)rm- remove file-r- recursively remove all items inside directory-f- force to remove
rmdir- remove directoryecho- output parameter to the console> <file>- clear the<file>and write parameter>> <file>- write parameter
&&- execute several commands
To check whether git is correctly installed in terminal type
git versionTo set your name and email and store this information in .gitconfig file run these commands:
git config --global user.name "yourName"
git config --global user.email yourEmailTo check config contents type
git config --listTo keep tracking versions of project or files you need first to initialize Git-repository. To do this you need to:
cdinto neccessary directory- type
git init
Well done! All git-related info will be stored in .git hidden directory.
To remove all git info you just need to delete .git directory. You can do it using this command:
rm -rf .gitTo show information about current states of files in repository type
git statusTo prepare file(s) for saving type
git addwith name(s) of file(s).
To prepare all files you can type
git add --allor
git add .To commit changes type
git commit -m "your comments"-m flag enables adding comments to commit.
To view commit history type
git log- Click
Sign upbutton on GitHun main page. - Fill in the required information.
- Solve captcha.
- Press
Create account. - Enter code sent to yout email.
- Go to
Your profilepage. - Move to
Repositoriesfolder and pressNew. - Fill in required info and press
Create repository.
When computers exchange data on the network, whey follow network protocols - rules of data exchange between computers.
One of the most popular network protocols is SSH (Secure Shell Protocol)
SSH uses a pair of keys:
- private key is stored only on your computer and should not be transferred to anyone else. It is used to decrypt data.
- public key is accessible to everyone and is used to encrypt data. They can be decrypted with a paired private key.
To check whether you already have SSH key or not do the following:
-
cd ~```
-
ls -la .ssh/
If this directory is empty or nonexistent then everything is OK.
To generate SSH key:
-
Type
ssh-keygen -t ed25519 -C "email linked to your GitHub account"In case of error type
ssh-keygen -t rsa -b 4096 -C "email linked to your GitHub account" -
Select place to store SSH key (default is home directory
~) -
(optional) Enter a passphrase if you want
-
Check keys by typing
ls -a ~/.sshYou should see two keys:
- public with
.pub - private
- public with
To link SSH key to your GitHub account:
-
Copy public key into buffer
masOS:
pbcopy < ~/.ssh/id_ed25519.pub
Windows:
clip < ~/.ssh/id_ed25519.pub
-
Go to GitHub
Settings -
Press
SSH and GPG keys -
Press
New SSH key -
Fill in the form and press
Add SSH key -
Check correctness of key by typing
ssh -T git@github.com
You can go to this page to check SHA256 key. If the key is correct type
yes
- Go to your repo on GitHub and copy
SSH URL. - Open local repository and type
git remote add origin pasteSSHURLhere
- Check that repositories are connected
You should see two identical lines of code.
git remote -v
-vis the short form of--verbose.
There may be several branches in the repository at once — parallel change histories. They can also connect to each other.
When you send changes to remote repo for the first time you need to type
git push -u origin mainNext time you can omit -u flag and use just
git pushNow you can see all commits on GitHub by pressing commits button in the top right corner.
Hash is the commit IDentifier. It is printed right after commit word:
Git converts commit information using the SHA-1 algorithm and calculates a unique hash identifier for each of them.
Hash is the main identifier of the commit and allows you to find out its author, date and contents of the committed files.
All hashes, as well as the correspondences table hash → commit information Git stores in the folder .git.
git logprints commits. Each of them consists of
- hash
- author
- date
- comment
You can get short log by typing
git log --onelineIt will contain short hash (its length is calculated the way to be unique in the particular git repository) and comment.
HEAD is a file in .git directory. It points to the latest commit. Technically HEAD file contains link to the latest commit.
You can use HEAD instead of latest commit hash.
File can be in one or two statuses from the list:
untracked: Git knows about the file but doesn't track it.staged: Git places the file in staging area aftergit addcommand. The file is now prepared to be commited.tracked: Git has already tracked the file.modified: Git spotted some changes while comparing the latest version of the file and the current one.
In README.md file developers always store the following info:
- The name of the project and its brief description: authors, goals and problems that are being solved.
- Technologies that are used in the project. What is its difference from similar ones.
- Project documentation — detailed instructions on what the project is.
- Project plans, if any.
README.md file is written in markdown markup language.
Basic usage can be studied here.
.gitignore is a text file in the root directory used by git for ignoring tracking some files.
Parameters:
*- any string?- any symbol[...]- any symbol from the range[...]/- applies to root directory**- any number of directories!- inversion
git log --onelineshows max 72 characters comment. That is why it it important to structure comments correctrly according to the rules:
To correct commit without changing comment use
git commit --amend --no-editto also change comment use
git commit --amend -m "New comment"NB. Correction deals with the latest commit!
To unstage changes use
git restore --staged <file>To travel in time use
git reset --hard <commit hash>NB. Commits made after that very one will be deleted!
To remove unintentional changes use
git restore <file>To spot the difference between the latest commit (HEAD) and modified files use
git diffTo spot the difference between the latest commit (HEAD) and staged files use
git diff --stagedTo spot the difference between two commits use
git diff <hash1> <hash2>
