Git
Version control system is a tools that helps to track changes in code .
It is used for
- It used to track whole history of code , when the code inserted and when is deleted and when is
modified .
Git is Version Control System .
- It is one of the most popular version control system
o Most of companies use GIT version control system for track it.
o It is free and open source
o It is fast and scalable . Large scale and small scale both projects can be tracked on GIT.
- GIT is mostly used for 2 reason.
1) Track the history.
2) To collaborate.
Github
- Website that allows developers to store and manage their code using Git.
- Website :- https://github.com
- It used to store the code on website , manage it (means add , delete or modify
the code
- It is used to reflect the changes made on system on uploaded code too.
- The code is uploaded on github in a container (means folder) named as
Repository .
- For finalizing the changes there is option in GITHUB called as commit
- README.md file is used to give description about the code .
- It also have some special command like html(basic html should be sufficient to make changes in it).
- md means mark down .
__________________________________________________________________________________
Setting up Git
Download following things for completing setup of Git.
o Visual Studio Code
o Windows(Git bash)
o Mac (Terminal)
- For checking the version of Git.
git –version is used
- For checking on which directory we are working
pwd is used.
- For clearing all the things
clear is used.
Config the git
After the setup is done . we have configure the git means by which account we
are making changes on Github too
- For config account do following thing-
1- git config –global user.name “name of account”
2- git config –global user.email gmail_name@gmail.com
3- git config –global –list
Last one is used to see the configuration details we had entered
Basic Command of Git
Clone and Status
1) Clone – Cloning a repository to a local machine
- There are 2 spaces on which we work
1- Remote
2- Local
- This command is used to duplicate the github repository on local
machine(laptop) .
- Syntax –
git clone <some links>
In github there is green option of text code click on it and copy the https link and
paste it in vscode terminal / git terminal in above format
REMOTE LOCAL MACHINE
[GITHUB] [LAPTOP]
COPYING
cd( Change Directory ) –
to change the directory means from outside folder to inside folder
ls(List Files) –
For checking files present in folder
ls –a –
For checking hidden files present in folder
mkdir –
It is used to make a new directory on local machine
2) status –
This command is used to check the status the which is one git repository and local
machine.
If there is changes it show the commit else it will show up to date
types of status
1- untracked
new files that doesn’t tracked yet.
2- modified
Changed.
3- staged
file is ready to be committed
4- unmodified
unchanged.
-when we make changes or add files in the repository it will set to status modified
or untracked respectively.
-We have to add this files on add . After adding the status of file changes from
untracked \ modified to staged(Ready to commit)
-After adding we commit the changes and status will turn into unchanged
Add and commit
3) Add –
adds new or changed files from your working directory to Git staging
area.
Syntax –
git add<file name> -- this is for particular file
git add . – this is for adding all files
4) commit –
It is the record of change.
Syntax –
git commit –m “some message”
Changes in file Adding files
Commit the
(Engaging to them) changes
(Setting up
things
This is staging area (Wedding)
between
couple and
families before
engagement)
git log is used to see all the commits we made.
5) Push
push - upload local content to remote repo.
syntax –
git push origin main
git push – local command used to push
origin- by default remote repo on github(cloned from the repo)
main – it is name of branch
6) init command
init – used to create a new repo on git
Syntax-
git init
git remote add origin <link>
git remote –v (to verify remote)
git branch (to check branch)
git branch –M main (to rename branch)
git push origin main / git push -u origin main (It means all the files
will be added to this branch and next time when we add we use only git
push command.)
Example –
Suppose we create a new directory on local file . We first make it
git repo using git init command then it will be initialized to git repo.
Now we will make a repo on github and will add the remote repo on local
machine by git remote add origin <link> command . Now we can
check the repo by git remote –v command . We can check it branch by
git branch command . we can change the current branch name we use
git branch –M branchname .After this we can modify the file and will
push by git push origin main / git push –u origin main command.
(-u means set upstream . from now all changes will be done in this
branch).
WorkFlow –
local git
Github repo Clone Change
Push Commit Add
Git Branches –
Commands for branches –
1) git branch (To check current branch)
2) git branch –M main (To rename the branch)
3) git checkout <branch name> (to navigate from one
branch to another)
4) git checkout –b <new branch name> (To create a new
branch)
5) git branch –d <branch name> (To delete a branch)
For merging code
WAY 1
1) git diff <branch name>
suppose we are on one branch and we want to see the
difference between this and another one . Then we run a
command git diff another_branch
2) git merge <branch name>
WAY 2
- Create a PR (Pull request)
pull request – It lets you to tell others about changes
you’ve pushed to a branch in a repository on Github.
When we merge branches by pull request and want it on local machine’s
another branch to then after confirming the pull request we run command git
pull origin main (main is a branch name).
git pull origin main -
used to fetch and download content from a remote repo and immediately
update the local repo to match the content.
Resolving Merge Conflicts
- when we do changes in both sames files or same lines in different
branches then get confused which changes should kept .In this case
we use this event.
- An event that takes place when git is unable to automatically resolve
differences in code.
git merge main is used to merge branches by terminal.
Undoing changes
Case 1 : Staged changes
git reset <file name> (for removing changes for one file
after adding / staging)
git reset (for removing changes for all files after adding /
staging)
Case 2: Committed changes (For one commit)
git reset HEAD~1
When we commit the files internally all the commits are stored in
git by name HEAD and the HEAD~1 is remove the latest change at keep
changes upto last change
Case 3:commited changes (For many )
git reset <commit hash>
when we do git log the number in yellow is a commit hash
for particular commit
git reset --hard <commit hash>
Is is used to remove commits / changes from local and git
repo too.
Fork -
A fork is a new repository that shares code and visibility setting
with the original ‘upstream’ repository.
Fork is a rough copy.