1/1/1970
Git is a powerful and popular version control system that enables effective tracking of changes in source code.
70% of developers worldwide use git for development. Reasons:
[Untrack] [Unmodified] [Modified] [Stage]
| | | |
|------------------Add File------------------------->|
| | | |
|-Edit the file->| |
| | | |
| | |-Stage the file->|
| | | |
|<-Remov the file-| | |
| | | |
|<------------Commit---------------|
In Git, files in repository can be classified into four different states.
git initgit statusred color - untracked files , Green color - Staging area
git status -S # short/simpleFirst Column (Staging Area): A(New file added), M(Modification), D(Deleted), R(Renamed) Second Column (Working Tree): M(Modified), D(Deleted)
git add
# or
git add -A # --allgit add index.htmlgit commit #open in vim editorgit commit -m "This is my first commit"git add)git commit -a -m
# automatically stage any modified or deleted files(but not new, untracked file) and creates a commit with specified messages.git rm waste.html # removegit rm --cached waste.html # removegit checkout -f # --forcegit checkout about.htmlgit logx commit ( change introduced in commit)git log -p -1 # --patch, -1: no of last commit to seegit diff # differencegit diff --stagedclear:q
what - and -- in git??
-: represents options in git command
--: separate options from arguments explicitly
lstouch)touch about.htmlcreate .gitignore file
touch .gitignoreadd files inside .gitingnore file manually or:
Add index.html into the .gitignore
git rm --cached index.html # file will be removed from the staging area
# or
echo "index.html" >> .gitignore
# or
printf "index.html\n" >> .gitignoreverify ignored files.
cat .gitignoreNote: Git Bash can be used as Unix terminal
In Git, a branch is a light weight movable pointer that represents an independent line of development.
It allows you to work on different features, bug fixes, or experiments without affecting the main project code base.
When you create a git repository, it starts with a default branch Called master. you can create a new branches from this master branch or from other existing branches.
Each branches has its own commit history, which means you can make changes, commit them, and store them independently from other branches
Checkout new branch
git branch feature1git branchGreen - Show that you are in this branch, White color - other branches
git checkout feature1
# or
git switch feature1git checkout -b feature2
#or
git switch -c feature2 # --creategit checkout master
git merge feature`Note: When you switch branches in git, it will remove any uncommitted changes that conflict with the target branch. This is safety measure to prevent potential data loss. However, switching branches would not delete untracked file in repository.
Github is a hosting service for git repositories and if you have a project hosted on Github, you can access and download that project with commands on any computer you have access and make your changes and push the latest version back to Github. Github allows you to store your repo on their platform. It is also comes ability to collaborate with other developers from any location
git remote add origin https://github.com/gauravmeee/git-in-one-video.gitgit remote git remote -v # verbose : additional infor about remote repos, including fetch and push URIsgit push origin mastergit push -u origin master # --set-upstream
git pull -u origin masterNote: The -u flag stands for --set-upstream. This sets the upstream tracking relationship for your local master branch to the origin/master branch. git push and git pull commands can be used without specifying the remote branch because Git will remember the upstream branch.
git push
git pullgit clone https://github.com/gauravmeee/markdown-notes.gitgit clone https://github.com/gauravmeee/markdown-notes.git Folder1git clean # Remove untracked files from working directory
git commit --amend # let you amend the most recent commit
git fetch # fetching downloads a branch from another repository, along with all of its associated commits and files.
git pull # Pulling is the automated version of git fetch
git reset # undoes changes to files in the working directory (that are not pushed)
git revert # Undoes a commit snapshot
git rebase # let you move branches around, helping avoid unecessary merge commits.
git reflog # git keeps track of updates to the tip of branches using a mechanism called reflog