Last active
January 26, 2025 02:06
-
-
Save subrotoice/b27f7d20617bb3da04827a223faf6450 to your computer and use it in GitHub Desktop.
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
# Initial git setup || Run For the first time. | |
git config --global user.name "subrotoice" | |
git config --global user.email "[email protected]" // Every space has meaning: git config --global user.email [email protected] will work | |
git config --list (Show all config) || git config user.name //(Show User Name) | |
git config --global core.editor "code --wait" // set default editor to open config | |
git config --global -e // Open in editor | |
git config --global core.autocrlf true // Mac: git config --global core.autocrlf input // End line | |
# Ch-2: Creating Snapshots (do Commit) | https://prnt.sc/zIIkMN39czOk | stage area green , unstated red | https://prnt.sc/9_BUPJ6VX53L | |
1. Working Directory: Where you work on your files locally || "Project Folder" another name | |
2. Staging Area: A temporary holding area - Where proposed change for next commit | |
3. Repository (Store Snapshots Parmanently) | Store what content in the last stage area | |
# Working Directory -> Stageing Area | |
- git add a.txt || git add a.txt b.txt || git add .txt(all text file) || git add . | |
- git ls-files // list of files in Staging area | |
# Stageing Area -> Repository | |
git commit -m "Short MSG" || -git commit // Opens the default editor to type a long message | |
Best practice: 1. Past tense, 2. Average 5 commit in a day | |
git commit -a -m "add & commit" or git commit -am "add & commit" // Skeeping staging area. add+commit | |
# Removing Files | |
git rm file1.js // Removes from working directory and staging area | |
git rm --cached file1.js // Removes from staging area only, but file reamain as untrack file | |
git rm --f waste.html // Remove stage area + Delete File; --f to force removal | |
# Renaming or moving files | |
git mv a.js b.txt | |
# .gitignore | |
node_modules/ //Folder | |
*.log // all log file | |
account_info.js // file | |
# Viewing the staged/unstaged changes (--- define old copy, +++ define new modified copy || https://prnt.sc/gfyzlJfoMLZT) | |
git diff // Working directory vs Last Stage | |
git diff --staged // Shows staged changes which is gonna to be in next commit | |
# Unstaging files (undoing - git add operation | Discard from staging area || It not undoing change in working directory, only take last copy from repo and let not to add current change in staging area) | |
git restore --staged a.txt // Copies the last version of a.txt from repo to stage | Git will go to next environment(here last commit) and take a copy to stage area | |
git restore --staged .txt, git restore --staged . // like git add | Use git status -s // for better understanding | |
# Discarding Local Changes (git will go to next environment(here is from stage area and take a copy to working directory)) | |
git restore e.txt // So if we use restore then it will copy from next environment (Working dir -> Stage Area -> Repo) | |
git clean -fd // Removed untrack files | use git clean -h // for quick help | |
# Restoring a File to an Earlier Version | |
git restore --source=HEAD~1 e.txt // Resoter form one step back from last commit | |
# Creating Snapshots with VSCode | Stated/UnStaged, Commit, Timeline | |
# Ch-3: Browsing History ------ | |
# Viewing the history | |
git log // Full history || Up/Down arrow, Space for next page, q for quit | |
git log --oneline // Summary | |
git log --reverse // Lists the commits from the oldest to the newest | |
git log --oneline --stat // What files changes in every commit || git log --stat // full details | |
git log --oneline --patch // See actual change ie. vscode what we see old vs new copy | |
# Filter the history (by author, date, commit msg, content so on..) | |
git log --oneline -3 // last 3 commits | |
git log --oneline --author="subrotoice" // Filter by author, git log --oneline --author="subr" and git log --oneline --author="ice" both will rork | |
git log --oneline --before="2024-09-24" // By date, git log --oneline --before="tomorrow" | |
git log --oneline --after="2024-09-24" // git log --oneline --after="yesterday" | git log --oneline --after="one week ago" (tow days ago, one month ago) | |
git log --oneline --grep="add e" // by commit msg, NB: Case sensetive | |
git log --oneline -S"content in file" // Search by content: NB: not -S="content in file" | |
git log --oneline -S"4" --patch // to see what change actually come ie. vscode what we see old vs new copy | |
git log --oneline 9f4bdb9..cb5dbb6 // in a range, NB: old..new | Also work: git log --oneline 9f4b..cb5 as long as not ambegious | |
git log --oneline e.txt // those commits that modifyed e.txt file | If not work then use: git log --oneline -- e.txt | |
git log --oneline --patch e.txt // see actual changes in e.txt file ie. vscode what we see old vs new copy | |
# Formatitting the log output (https://git-scm.com/docs/git-log#Documentation/git-log.txt-emHem) | |
git log --pretty=format:"%an committed %h" // %H commit hash, %h abbreviated commit hash, %an author name, %aN author name, %ae author email | |
git log --pretty=format:"%an committed %h on %cd" // %cd commit date | |
git log --pretty=format:"%Cgreen%an committed %h on %cd" // Green coler, if not reset all will be green | |
git log --pretty=format:"%Cgreen%an%Creset committed %h on %cd" // Color, with reset (https://git-scm.com/docs/git-log#Documentation/git-log.txt-emCredem) | |
# Aliases | |
git config --global alias.l "log --oneline" // 'git l' is equal to 'log --oneline' | |
git config --global alias.b "branch" // git b is 'git branch' | |
git config --global alias.lg "log --pretty=format:'%Cgreen%an%Creset committed %h on %cd'" | |
alias g='git' in file C:\Users\HP\.bashrc //Save it | git alias as g | Must be open in git bash command | |
# Viewing a commit | show command do not change anything in working dir like checkout. It just show what file & content in a certain | |
git show 86126b1 //Shows the given commit || git show 861 this also works if not ambegious | |
git show HEAD //Shows the last commit | |
git show HEAD~2 //Two steps before the last commit | |
git show HEAD:a.txt //Shows the contains a.txt in the last commit | |
git show HEAD~2:readme.txt // Show the content of readme.txt 2 step back of last commit | |
git show HEAD --name-status // Can see which file add, modify or delete of a certain commit | https://prnt.sc/Zf45OTb9jT9o | git show HEAD~2 --name-status | |
git ls-tree HEAD~3 // list of files 3 steps before last commit || https://prnt.sc/Yn9kkxGd1Mdr (Helpful to see what a file contains in some commit) | |
git show 98t9 // it link with previous command(git ls-tree HEAD~3) show content of a file after ls tree | |
# Viewing the Changes Accross Commits (same like staged and unstaged change) | |
git diff HEAD~5 HEAD // git diff HEAD~5 HEAD~1 | |
git diff HEAD~5 HEAD~1 c.txt // See on a perticular file | |
git diff HEAD~5 HEAD --name-status // See what file added, changed or deleted in this range | https://prnt.sc/C9pJyuPfXU7f | |
# Checking Out a Commit (Should not do another commit when checking out just watch) | going back in time - No change in repo, easily back to Headsection again)# ওই অবস্থায় গিটে কি ফাইল ছিল, কি কনটেন্ট ছিল | |
git checkout HEAD~2 or git checkout 0f6509e or git checkout 0f6 //use: git log --oneline --all | git checkout master to take back pointer | |
# Finding Bugs Using Bisect (Dividing all commit into half like binary search.) | |
git bisect start | |
git bisect bad // last commit as bad where we find bug but dont know actually in which commit | |
git bisect good 22eb5b0 // id of first commit, but we can set any | |
git log --oneline --all // see what is happining | |
git bisect bad //when 0 revisions | we can see full operation | https://prnt.sc/P-CIevbngd0S | |
git bisect reset // back to master brunch | |
# Finding Contributors using Shortlog | |
git shortlog -n -s -e // output: 10 subrotoice <[email protected]> | |
git shortlog -n -s -e --before="" --after="" | |
# Viewing the History of a File | |
git log a.txt | |
git log --oneline --stat a.txt | |
# Restore a deleting file (ie. accidentally delete a.txt) | |
git checkout 8s8ad8d a.txt // the last commit where a.txt where present | |
git add a.txt + git commit -m "Restore a.txt" | |
# Finding the author of Line Using Blame | |
git blame f.txt // all author of f.txt file | |
git blame -e f.txt // -e for email | |
# Tagging | |
git tag v1.0 d0b8906 // git log --oneline | https://prnt.sc/qS6VDz99Zo-l | |
git checkout v1.0 // like checkout a history | |
git tag // see all the tag | |
git tag -n // tag with commit msg | |
# GitLens — Git supercharged: (GitKraken) VS Code Extension | |
# gitkraken.com (gitkraken.com/download/windows64) More convenient GUI Tool for git | |
# Ch-4: Branch---master(default)------ | |
git branch // show all branch and current branch in green; (for details) git branch -v / git branch -a | |
git branch xyz // Create new branch xyz; git checkout -b newBranch // create+Switch to newBranch | |
git checkout xyz (switch to xyz branch) | |
git branch -m bugfix bugfix/signup-form // rename a branch more specific | |
git branch -d xyz (delete xyz branch but in that time you have to stey another branch otherwise you can not delete) || git branch -D xyz (If problem occured) | |
git checkout -b xyz or git switch -C feature/change-password // (Create + Checkout) | |
git log --oneline master..bugfix/signup-form // all commit between this two branch | |
git diff bugfix/signup-form // Same as before if make this command from master branch | |
git diff --name-status bugfix/signup-form // Which file hase been changed | |
# Stashing (If we change branch then our change might be lost so we can store it in stash it is like repo history but another storage) | |
git stash push -m "New tax rules" // Store current change in stash | Untrack file will not store | |
git stash push -am "My new stash" // If there is a file untrack then use it | |
git stash list // Show all stack | |
git stash show 1 // see what change made in a certain stash | |
git stash apply 1 // apply stash 1 in working directory | Because after change it clean working directory to last commmit | |
git stash drop 1 // drop stash 1 | |
git stash clear // Drop all stash | |
# Merge | git log --oneline --all --graph to see the log in better representation | |
# First-Forword merge e linear path thake no need to do commit. Dyverse merge e 3-way marge thake add staging and commit. Delete branch after merge is a good idea | |
git merge xyz (on master branch)(It keep all the commit log from xyz) | |
git commit -m "Merge msg here" (already added to stage area so just need to commit) | |
git merge --no-ff bugfix/login-form // No fast-forword marge | https://prnt.sc/jWC7h-prZiV5 | If there is chance to fast forword but here we do 3-way merge(diverse) | |
git merge xyz --squash (Bring all changes from xyz branch to master stage area. So you need to run "git add" and It will not added to commmit log) | |
git b --merged // all branch marged into master | safe delete marged branch to avoid confusion | |
git merge --avort // in between conflict if we do not have much time to resolve conflict we can avort merge, It will get back to state before start merge | |
# Reset (Remove Commit from history): Soft(Removes the commit only, keep in staging and working directory), Mixed(Remove from commit history and staging area, keep in working directory | default) and Hard (Discards local changes) reset to copy of certain commit | |
# Reset - Big Picture: https://prnt.sc/9HJL-sae-ULo | |
- git reset --soft HEAD~1 (Removes the commit only, keep in staging and working directory) | |
- git reset 2135fda // git reset --mixed 2135fda (Remove from commit history and staging area, keep in working directory | default) (You can git add . and commit to back to last commit) | |
- git reset 40ce348 --hard (Reset completly No file remain in working directory) || or, git reset --hard 40ce348 (Place --hard previous hash code) | |
- git checkout HEAD -- a.txt //git checkout HEAD -- app/admin/page.tsx Resets only a.txt to the last commit, leaving other files and changes untouched | git reset --hard HEAD: Resets the entire repository | |
# Undoing a faulty marge | |
git reset --hard HEAD~1 //git reset --hard 9e7d88d // recover merge commit again, here 9e7d88d is HEAD | |
git revert -m 1 HEAD // First parent | |
# Picking Files form Another Branch | |
git restore --source=feature/send-email -- toc.txt //make a copy of toc.txt file form feature/send-email to master branch | git add . + git commit | |
# Ch-5: Remote - GitHub ---- | |
# Remote(GitHub) | |
git remote add <UrlName> <URL-www.> | |
git remote -v // See URL; git remote aslo work but show only git name | |
git remote set-url secondURL https://github.com/subrotoice/kst.git // Change URL for second identifier | |
git remote rename upstream base // change remote name from upstream to base | |
git remote rm base // remove base remote | |
git push -u third master // you can push without switching branch, ie. from master branch you can push to other branch; https://prnt.sc/QAaFTy0Lt1_s | |
git push -f second main // Forch Push | |
git clone -b <branchName> <remote-repo-url> // Clone a Specic Branch from remote repo; ie. git clone -b main9 https://github.com/subrotoice/test9.git // Working, Here brunch name main9 | |
git branch -a // After download you to see all branches || then - git checkout <branch_name> | |
# Remote(Pull) | |
git pull origin xyz (Pull a branch) | |
git pull --rebase origin master // if Error in pushing, that case first do this command then push | |
# GitHub Push problems | |
Setting -> Developer Setting -> Personal Access Token -> Token(Classic) -> Create new token and Store it | |
git config credential.helper store // use before push, to avoid providing credential in every push | |
# Mosh Remote - GitHub | |
git log --oneline --all --graph // See entire history with better representation | |
git fetch origin master // download from origin address master branch. git fetch <repo_url> <branch_name> | |
git branch --vv // Show how local and remote branch difference | git b -r // Show only remote branch | |
git merge origin/master // Marge local and remote | use git branch -vv | cat a.txt to set the content of the file | |
# Pulling (pull = fetch + merge) | |
git pull // fetch + Dyverse marge | |
git pull --rebase // To get linear history | https://prnt.sc/2LRqe9vLrxgy | Merge commit just 1 step up the remote | |
# Pushing | |
git push origin master // origin is default, if we are in master branch we can remove it. Good Idea is git pull then push. If any conflict resolve it and then push. | |
# Storing Credintials | |
# Sharing Tags | |
git push origin v2.0 // push tag to remote(origin) | Create tag: git tag v2.0 | |
git push origin --delete v2.0 // Delete tag from origin | But still in local repo. To delete: git tag -d v2.0 | |
# Releases (it not git feature, it is GitHub feature. It is high level task on top of the tag.) | |
# Sharing Branch | |
git push -u origin feature/login // Push feature/login branch | git branch -r // to see remote branch | |
git push -d origin feature/login // Delete remote branch feature/login. Deleted branch from remote but still present in local repo | |
# Collaboration Workflow (add Collaborator email in GitHub) | |
# (User A: subrotoice) | |
1. Create branch from GitHub | 2. git fetch | 3. git b -r | |
4. git switch -C feature/change-password origin/feature/change-password // Switched and 'feature/change-password' set up to track 'origin/feature/change-password'. | |
# (User B: iambiswas) | |
5. git clone repo.git // From another machine | |
6. git branch -r // see all remote branch | |
7. git switch -C feature/change-password origin/feature/change-password // again to attach with remote branch | |
8. Change file + git add . + git commit + git push // | |
# (User A: subrotoice) | |
9. git pull // to bring iambiswas change | git log --oneline --all --graph | |
10. git switch master // switch master for merge | |
11. git merge feature/change-password // merge | it will be Fast-forward merge. If conflict resolve it like pase | https://prnt.sc/L3BY-8YPygEB | |
12. git push | |
# After done task need to delete branch. | |
13. git push -d origin feature/change-password // delete branch form remote repo. carefull: git push -d origin/feature/change-password (error) | |
14. git branch -d feature/change-password // delete local branch | |
# (User B: iambiswas) // pull update and delete both local and remote branch | |
15. git switch master | |
16. git pull // to bring the change | |
17. git branch -d feature/change-password | |
18. git remote prune origin // remove all remote branch that not present in remote | |
--- This is how two or more people can Collaborate on a feature --- | |
# Pull Request (Open a discussion in a team + Merge) | |
# (User B: iambiswas) | |
1. Do some change and push to Github | |
2. From GitHub create pull reaqust and set one or more reviwers | |
# (User A: subrotoice) | |
3. Review code and give some comment on code to expected changes | |
# (User B: iambiswas) | |
4. Do all change and Push and re-pull request again | |
# (User A: subrotoice) | |
5. (GitHub) Comment -> Approve -> Merge -> Delete brunch // So side branch merge to master | |
# (User B: iambiswas) | |
6. Git pull + Delete both local + remote branch | |
# Issues (feature of GitHub) | discussion of bugfix, new feature, enhancement or any kind of idea. | |
# Milestone: Prograce of Issues | We can assign Issues to Milestone | |
# Contributing to an Open-Source Project | |
1. If found a bug or implement a new feature. | |
2. Create a new issue for fixing bug or implement a new feature 0r We can pick a new issue | |
3. Frok Repo to your personal account. | |
4. FixBug in New Branch + Add + Commit + Push | |
5. Create Pull request from frok repo. Owner of the repo get notified | |
6. Conversion between Owner and frok. From can not merge and close pull request. Only maintainer can do this. | |
# Keeping a Frok Up to Date | |
1. git remote add base <Owner repo link> // base is the owner repo identifier | |
2. git fetch base // or git pull base | |
3. git switch master + git merge base/master | |
4. git push origin master | |
5. git merge master // merge with bugfix to up to date with master | |
# Ch-6: Rewriting History - Revert, Rebese, Reset | |
# Rewriting history is little bit dangerious when working in Collaboration. Why we need it? | |
- Poor Commit Messages, Large Commits, Small and scattered commit. And want clean, readable history. | |
# Tools (operation) | |
- Squash small, related commits (Combine commit) | |
- Split large into small commit (Separate commit) | |
- Reword commit messages to make more meaningful | |
- Drop unwanted commits | |
- Modify content of commits. ie. include file | |
# Golden rules of Rewriting history | |
- Don't rewrite public history | |
- Rewriting private history in local is perfectly fine. | |
# Revert commits (undoing things - Add another history along with keep all previous commit history) | |
*In a serario where you delete file d.txt and commit. Then you want to get back that file in a new commit but previous delete d.txt also remain* | |
* We can again revert, We can change commit msg completly, But it is good idea to keep - revert "commit msg" | |
* Revert ke abar revert korle back to previous condition | |
- git add . // first: delete d.txt by mouse, then git add . | |
- git commit -m "removed d.txt" | |
- git revert a440127 (Editor will open change commit msg if you want) | |
- git log --oneline (Add new commit and not remove previous commit from commit history) https://prnt.sc/2UxIh3rIrC_t | |
- git revert HEAD~3..HEAD // same: git revert HEAD~3.. | Revert last 3 commit. HEAD~3 means 4th commit but hear range is last 3 to HEAD. It will generate 3 different commit history in repo. | |
# Generate 1 commit history in repo of reverting last 3 commit | |
- git revert --no-commit HEAD~3..HEAD // Same: git revert --no-commit HEAD~3.. | |
- git status -s | |
- git revert --continue // git log | |
# Recovering lost commit(Very implement) | Supposed reset last 5 commit instaed of 2 that case we need to recover last 3 | |
- git reflog // To see all log, everything | |
- git reset --hard HEAD@{1} // or git reset --hard 16adf5b | |
- git reflog show feature // it will show reflog of feature branch | |
# Rebase: Rewriting History | 1. Rename previous commits, 2. Merging commits into one, 3. Drop Commit, 4. Modify commit content etc. | |
* Rebasing is a distructive operation | |
* Rebase: fix a base(parent) from where modification will start | |
# Amending to the last commit | Committed changes to a repository and then made additional changes that you want to include in that same commit | |
git add <file(s)> // ie. git add README.md | |
git commit --amend // git commit --amend -m "Short msg" | |
git push --force // same: git push --f | Need to force push to the remote repository if you've already pushed the previous commit. | |
# Amending last 5 commit into 1 commit (Squase into one) | |
- git reset --mixed HEAD~5 // all reamain in working dir. | |
- git add . + git commit -m "Combine all last 5 commit" | git clean -fd (remove all untracked file: as know before) | |
# Amending an Earlier commit (add/delete/modify files in the history) | |
- git rebase -i 16adf5b // rebase started | git rebase --abort // it take to stat where rebase start. | |
- "edit" instade of pick -> Do some change(add/edit/delete) -> git add . -> git commit --amend | https://prnt.sc/QV-GSLP9AsQK | |
- git rebase --continue // End rebase and marge. git log --oneline --all --graph | git checkout 49e4e8a - to see change over repo history. ie. add/delete file to 5th last commit and it will remain after all commit of 5th | |
# Drop a commit (If latest commit have file that is depend on previous then you have to resolve conflict) | |
- git rebase -i 16adf5b // rebase started | git rebase --abort // it take to stat where rebase start. | |
- "drop" instade of pick | |
- git rebase --continue // if needed | |
# Reordering Commits | |
- git rebase -i 16adf5b // Start Rebasing | Change order and save and close. Done! | |
# Combine commit into one (https://prnt.sc/3DAXgQP3CaOx) | |
- git rebase -i 16adf5b | |
- pick old one. squash newer commit. // as https://prnt.sc/3DAXgQP3CaOx | Save and Close. Done! | |
* You can use fixup instade of squash that case you need to provide any message. It take oldest commit msg | https://prnt.sc/9Wi5kPsFYNdL | |
# Splitting the commit | |
- git rebase -i 16adf5b | |
* edit -> close editor -> git log --oneline --all --graph | |
- git reset --mixed HEAD^ | Discard change form staging and repo | git status -s | HEAD~1/HEAD^ is same | |
* Perform some operation and then add+commit. Again do this (modify content + add + commit) so generate two history | |
----------------End-------------- | |
# Basic Windows command like cd | |
cd\ = back to root directory c drive does not metter where its current postion | |
cd .. = One step back | |
cd /d D: = C Drive to D drive | |
dir or ls(LS) = List all file and folder of current directory. "ls" is more clear to read | |
mkdir mynewfolder = Create New Folder | |
echo tailwind-landing >> README.md // Create file and put content Best* shor: echo > asdfwe.txt (Create file only no content) | |
cd "folderName" = To enter Folder for doing some task | |
cls = Clear Screen // "clear" in git bash | |
# Work on an existing Project: Add files | |
First you have to download project otherwise it will not work | |
git clone https://github.com/Tilotiti/jQuery-LightBox-Responsive.git // Pull | |
cd folder_name // Need to change to inside folder | |
git add . For all new file and folder (git add file_names.exten it is for single file) | |
git commit -m "committed message" For asingle file(git commit -m "committed message" file_names.exten) | |
git push -u origin master // First time pushing the branch or if you've made changes that conflict with the remote repository, you might need to use the '-u' | |
git pull origin master // Change in github, it take effect in local reprository. 'Synchronization' | |
# Work on existing Project: Delete Some files and add new files | It is possible to rename reposiotry | |
1. Delete files manually using mouse keyboard | |
2. git add . | |
3. git commit -m "Deleted Files" | |
4. git push -u origin master | |
// Another way | |
1. git rm file1.txt file2.txt file3.txt // remove files and here not need to stage(git add) | |
2. git commit -m "Remove files: file1.txt, file2.txt, file3.txt" | |
3. git push -u origin master | |
# Miscellaneous (Code WIth harry) | |
git checkout index.html // Back to last commit | |
git checkout -f // Back to last commit all files | |
git log -p -1 // See one commit; q to exit from git log | |
git remote set-url origin https://<[token]@github.com/[user_name]/[repo_name].git // Asking pass again & again | |
git remote set-url origin https://[email protected]/subrotoice/next-crud.git | |
************* VS Code ****************************************************** | |
https://www.youtube.com/watch?v=2oihkInZ880 (Hindi) | |
1st time step: -----(Local: 1-3, Remote: a-d)-------------- | |
1. Initialize Repository // https://prnt.sc/V7oDXeeOi9CO | |
2. Commit // Visually Commit | |
3. COnfig Git(If ask) | |
a. Add Remote // Visually Commit https://prnt.sc/-IWSFNeadc1H | |
b. Push // Commit and push option ase vscode | |
c. Github Auth | |
d. Push Again (If required) | |
2nd Time (Old Project): | |
1. Pull (clone) // https://prnt.sc/K2us0_eYZFuq | |
2. Commit | |
a. Push | |
# https://prnt.sc/5ii9wCPT9Qut or https://prnt.sc/sapdQ5El7p_9 // Change in github, it take effect in local reprository | |
https://www.youtube.com/playlist?list=PLdHg5T0SNpN1dJt6gv9DGSqk5sBjmG3XI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment