ls -la folderNamelist of files and folders including hidden files inside folderNametouch fileName.extcreate new filemkdir folderNamecreate new foldercdgo to Parent directorycd directoryNamechange directorycd ..go back one directorypwdprint present working directoryrm fileName➡ delete filerm -r folderName➡ delete foldercp file1.extnsn file2.extnsncopy 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 file1print content of file1cat > file.extenter input & (ctrl+d and ctrl+d) to save and exit (OVERRIDES)cat >> file.extenter input & (ctrl+d and ctrl+d) to save and exit (APPENDS)
iEnter insert modeescexit insert mode:wq ➡ Save and quit Vim:wSave file:qQuit 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 folderinside 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
objectsfolder 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)Developfor development of the projectFeaturefor adding, refactoring or removing a featureHotfixfor changing code with a temporary solution because of an emergencyBugfixfor fixing a bugTestfor 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:
featfor adding a new featurefixfor fixing a bugrefactorfor changing code for peformance or convenience purpose (e.g. readibility)chorefor 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 initInitializes the folder for git commandsgit clone urlcopy a GitHub repo to local machine, other branches hidden except maingit clone -b branchName --single-branch urlclone a specific branchgit statusgives list of tracked(green) and untracked(red) files (staging area)git add .adds all files to staging areagit restore --staged filename.extensionremove from staging areagit commit -m 'must add message here'final save as snapshot
git log -5gives detailed list of last 5 commitsgit log --onelinegives list of all commits in 1 line formatgit log --oneline main..second_branch- how far
second_branchis ahead of themainbranch - displays commits that are in
second_branchbut not inmainbranch -
how far is main branch ==> just swap
second_branch..main
- how far
git branchgives list of all branchesgit branch -rgives list of all remote branchesgit branch -agives list of all local + remote branchesgit branch branchNamecreates a new branchgit branch -d branchNamedeletes the branchgit push origin --delete branch_namedelete a branch on GitHub that already deleted locallygit checkout branchNamego inside branchNamedifferent branches for different features => because 1 branch allows only 1 pull request
git switch -c featcreates a new feat branch from your current branch and goes inside it.works in newer versions of Git (2.23+)
git switch -c feat maincreates a new feat branch from main and goes inside it.if you are in different branch (say dev) and want to create a feat branch from main without going inside main.
> 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 stashsave to stash area (go backstage)git stash listview list of stashed filesgit stash popcome to front from backstagegit stash clearclear stash area (clear backstage people)
git revert hashvalueopens editor, creates new commit: undoing changes made by hashvalue(commit)
git revert HEAD➡ undo last commit after opening editorgit revert HEAD --no-editundo 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/commitvalueused 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 filegit remote remove remoteNamedeletes a remotegit remote -vlist all remotes along with their URLgit log --remotesgives list of commits done from different REMOTES
git push remoteName branchNamepush 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/branchNamereset the local branch with remote branch(LOCAL K SAARE COMMITS CHU MANTAR[very dangerous])
- Rebase:
git rebase origin/mainlocal branch gets rebased to remote branch "origin/main" (if no conflict else JHANJHAT)
- Merge:
git merge origin/mainlocal branch gets merged with remote branch "origin/main" (if no conflict else JHANJHAT)
git log --oneline main..second_branchhow farsecond_branchis ahead of themainbranch.
- Reset:
-
git pull remoteName branchNameused 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~ni = 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 branchNamepush 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






