Git and GitHub
Git
Version Control System is a tool that helps to track code changes.
Git is a version control system. It is:
-Popular
-Free and Open Source
-Fast & Scalable
Usage:
i) Track history
ii) Collaborate
GitHub
Website that allows developers to store and manage their code using Git.
https://github.com
Usages:
i) Website to store code/software
ii) Store in folders (repository/repo)
Create an Account on GitHub
Create New Repository
iii) Create-New Repo
iv) .md -> Markdown documentation (lightweight markup language that can be easily converted to text.)
Initial commit – first change (commit mean take screenshot and save)
GitHub Account
Create a new repository: imcg-demo
Make our first commit
Setting up Git
VS Code
Windows (Git Bash)
Download git from website
git --version
Configure git (already sit in github)
Go to bash terminal
git config --global user.name “Name”
git config –global user.name email@email.com (github account)
git config --list
Create Folder and open it in VS Code
Clone & Status
Clone – Cloning a repository on our local machine
git clone <-Link of email->
Status – displays the state of the code
git status
Local -> local system
Remote – Github
-Clone : -
Untracked
New files that git doesn’t yet track
Modified
Changed
Staged
File is ready to be committed
Unmodified
Unchanged
Add & Commit
Add – adds new or changed files in your working director to the Git Staging area.
git add <- File Name ->
Commit – it is the record of change
git commit -m “Some Message”
Add index.html and write command
Git add index.html
git add . (all changes add)
git commit -m “some message”
after this not changed in github yet.
Put on github your local content
Push Command
push – upload local repo content to remote repo
Give permission to push (first time only)
Origion -> copy on github
Main -> branch main (first only one branch)
If first folder is created on local system and than synchronized with github account.
init Command
git init
git remote add origin <-link->
git remote -v (verify remote)
git branch (to check branch)
git branch -M main (to rename branch)
git push origin main
create local foder (check ls and ls -a command in git bash)
there is no git folder means that its not configured
git init
do some work in new local folder
add (git add .) all files are at staged level
git commit -m “some message”
upload this new folder to github
go to github and create repo
ignore readme file
copy https address from current repo
write command git remote add origin “copied relevant repo link”
to verify remote -> git remote -v
check branch ( git branch) -> it shows *master branch (master branch new name is main) so change the name we use the
following command
git branch -M newname -> git branch -M main
now we can apply
git push origin main
we can write git push -u origin main (To avoid writing full command, after this, we can use git push
Branches :
Branch Commands
git branch (to check branch)
git branch -M main (to rename branch)
git checkout <- Branch name -> (to navigate)
git checkout -b <-new branch name -> (to create new branch)
git branch -d <- branch name -> (to delete branch)
To create a new branch, use the command
git checkout -b <new branch name>
to go on with the new branch -> git checkout branch name.
if we delete a branch -> git branch -d <branch name>, it must not be an active branch. If it is active, then go to another branch
then delete it.
to push data and create new branch on github, remain on current created branch and write -> git push origin newBranchName
See new branch is created
Merging Code
Way 1
git diff <-branch name-> (to compare commits, branches, files & more)
git merge <-branch name-> (to merge 2 branches)
Way 2
Create a PR (Pull Request) It lets you tell others about changes you’ve pushed to a branch in a repository on GitHub.
To merge two branches' data, we can use the command -> git diff <branch name to whom which want to compare with active
branch>
And or create Pull request on github
Pull Command
git pull origin main
used to fetch and download content form a remote repo and immediately update the local repo to match that content.
To get data from github to local system
Resolving Merge Conflicts
An event that takes place when Git is unable to automatically resolve differences in code between two commits.
Undoing Changes
Case 1: Staged Changes
git reset <-File name->
git reset
Case 2 : Committed changes (for one commit)
git reset HEAD~1
Case 3: Committed changes (for many commits)
git reset <-Commit hash ->
git reset –hard <-commit hash->
Fork
A fork is a new repository that shares code and visibility settings with the original “Upstream” repository.
Fork is a rough copy.