ls -la folderName
list of files and folders including hidden files inside folderNametouch fileName.ext
create new filemkdir folderName
create new foldercd
go to Parent directorycd directoryName
change directorycd ..
go back one directorypwd
print present working directoryrm fileName
➡ delete filerm -r folderName
➡ delete foldercp file1.extnsn file2.extnsn
copy file1's content into file2 (OVERRIDES)mv oldName.extn newName.extn
➡ rename oldName to newNamemv file1.ext folder/lol.md
➡ move file1 to folder as lol.mdcat file1
print content of file1cat > file.ext
enter input & (ctrl+d and ctrl+d) to save and exit (OVERRIDES)cat >> file.ext
enter input & (ctrl+d and ctrl+d) to save and exit (APPENDS)
i
Enter insert modeesc
exit insert mode:wq ➡ Save and quit Vim
:w
Save file:q
Quit Vim editor:q!
Quit Vim without saving
- git is a program that tracks changes made to a file
- git stands for global information tracker
- git is a distributed version control system
(other two are: localised vc (own laptop) and centralised vc (in an institute/company))
.git folder
inside a project, tracks all changes made over time, if you delete this folder the information of changes-made will be gone.
- Making changes
- Staging changes
- Commiting changes
-
A concept in git, where the changes are stored before commiting
-
The
index
(a binary file) inside.git/
folder contains the changes added through staging
-
Commits are stored in the
objects
folder in the.git/
folder -
Each commit has a unique ID number or 'SHA'
each commit becomes a snapshot in that point of time which finaly forms a repo history
-
If you made a typing error in the msg during commiting, then it can be corrected by the following:
git commit --amend -m 'typo corrected'
This also applies everything present inside staging area to the last commit
-
The main idea behind branching is to isolate your work into different types of branches
-
There are six different branch types:
Main / Master
(outdated term)Develop
for development of the projectFeature
for adding, refactoring or removing a featureHotfix
for changing code with a temporary solution because of an emergencyBugfix
for fixing a bugTest
for experimenting outside of an issue/ticket
-
Examples
- to add, refactor or remove a feature
git branch feature/issue-42/create-new-button-component
- to fix a bug
git branch bugfix/issue-342/button-overlap-form-on-mobile
- to fix a bug really fast (possibly with a temporary solution)
git branch hotfix/no-ref/registration-form-not-working
- to experiment outside of an issue/ticket
git branch test/no-ref/refactor-components-with-atomic-design
- to add, refactor or remove a feature
-
A commit message should start with a category of change.
-
There are mainly 4 categories:
feat
for adding a new featurefix
for fixing a bugrefactor
for changing code for peformance or convenience purpose (e.g. readibility)chore
for everything else (writing documentation, formatting, adding tests, cleaning useless code etc.)
-
Examples
- git commit -m 'feat: add new button component; add new button components to templates' - git commit -m 'fix: add the stop directive to button component to prevent propagation' - git commit -m 'refactor: rewrite button component in TypeScript' - git commit -m 'chore: write button documentation'
git init
Initializes the folder for git commandsgit clone url
copy a GitHub repo to local machine, other branches hidden except maingit clone -b branchName --single-branch url
clone a specific branchgit status
gives list of tracked(green) and untracked(red) files (staging area)git add .
adds all files to staging areagit restore --staged filename.extension
remove from staging areagit commit -m 'must add message here'
final save as snapshot
git log -5
gives detailed list of last 5 commitsgit log --oneline
gives list of all commits in 1 line formatgit log --oneline main..second_branch
- how far
second_branch
is ahead of themain
branch - displays commits that are in
second_branch
but not inmain
branch -
how far is main branch ==> just swap
second_branch..main
- how far
git branch
gives list of all branchesgit branch -r
gives list of all remote branchesgit branch -a
gives list of all local + remote branchesgit branch branchName
creates a new branchgit branch -d branchName
deletes the branchgit push origin --delete branch_name
delete a branch on GitHub that already deleted locallygit checkout branchName
go inside branchNamedifferent branches for different features => because 1 branch allows only 1 pull request
> a way to temporarily save uncommitted changes to working directory
> useful if you need to switch branches but don't want to lose your changes
> You're working on a feature branch, but need to fix a master branch bug. Stash your feature branch changes, switch to master, fix the bug, switch back to feature branch, and apply stashed changes
> You can stash your changes and then push the stash to the remote repository. Your teammates can then pop the stash and apply your changes to their working directories.
git stash
save to stash area (go backstage)git stash list
view list of stashed filesgit stash pop
come to front from backstagegit stash clear
clear stash area (clear backstage people)
git revert hashvalue
opens editor, creates new commit: undoing changes made by hashvalue(commit)
git revert HEAD
➡ undo last commit after opening editorgit revert HEAD --no-edit
undo last commit without opening editorused when we want to take a previous commit and add it as a new commit, keeping the log intact
git reset hashvalue/commitvalue
used to move the repository back to a previous commit, unstaging any changes made after that commit
- suppose it's Friday and you want to reset the branch as it was on Tuesday
helps to update/change/amend/modify last commit along with new commit hash
This can be useful for fixing commit message or for adding or removing files from the commit
git commit --amend -m 'typo corrected'
- whatever present in staging area gets applied to last commit, if nothing present, only msg gets overridden
- Don't use `git commit --amend`, if already pushed the commit to a shared branch.
> a reference to another Git repository.
It is a way to keep a copy of your repository on a different server.
> a repo can have multiple remotes: To keep backup of the repository on different servers.
This way, if something happens to one server, you won't lose your work.
> Use multiple remotes to keep your development, staging, and production environments separate.
Push changes to each environment independently.
git remote add remoteName url
(default = origin) creates a new remote entry in repository's.git/config file
git remote remove remoteName
deletes a remotegit remote -v
list all remotes along with their URLgit log --remotes
gives list of commits done from different REMOTES
git push remoteName branchName
push commits to the remote(ie GitHub) repository
bring all commits made on the upstream to local repository
> used to fetch changes (commits, branches, tags, etc.) from upstream without automatically merging or applying those changes to current local branch
-
git fetch --all --prune
--all => all branches and all remotes
--prune => deleted also -
Options after fetching:
- Reset:
git reset --hard remoteName/branchName
reset the local branch with remote branch(LOCAL K SAARE COMMITS CHU MANTAR[very dangerous])
- Rebase:
git rebase origin/main
local branch gets rebased to remote branch "origin/main" (if no conflict else JHANJHAT)
- Merge:
git merge origin/main
local branch gets merged with remote branch "origin/main" (if no conflict else JHANJHAT)
git log --oneline main..second_branch
how farsecond_branch
is ahead of themain
branch.
- Reset:
-
git pull remoteName branchName
used to bring changes from remote repository and automatically merge them into local current branch
- Internally it does => "git fetch + git merge"
-
used to combine multiple commits into a single commit
-
useful for feature branches where you might have made many small, incremental commits during development
-
good to squash commits before creating a PR to maintain clean history for the main branch.
-
steps:
-
use log, note down hashvalue of to-be-combined commits
-
git rebase -i HEAD~n
i = interactive mode i.e editor will open
n = how many commits to be combined including HEAD(most recent commit) -
new editor opens, change "pick" => squash but leave first one as it is, save and exit
-
new editor opens, give meaningful commit msg on first line, don't touch other lines, save and exit
-
REBASE success, if no conflicts present
-
force push to remote repo:
git push -f
-
git push remoteName branchName
push commits to the remote(ie GitHub) repository
[REMOTE K SAARE COMMITS CHU MANTAR, VERY DANGEROUS]
-
git push origin branchName -f
- Overwrites remote branch history with local branch history.
- This means that any commits on the remote branch that are not on the local branch will be deleted from the remote branch.
- very useful, Removing a commit from the pull request
- merge vs rebase (two ways of Integration)
+ merge makes commit history dirty
+ rebase keeps commit history clean
- merge conflict
+ multiple people modified same line and requested for pull request
- resolve merge conflict ➡ only using GitHub
- Pull Request ➡ only using GitHub
- fork a repository ➡ only using GitHub
- difference b/w two commits ➡ only using GitHub