0% found this document useful (0 votes)
6 views

Git

The document provides a comprehensive guide on using Git and GitHub, including installation, configuration, and essential commands for version control. It covers topics such as staging, committing, branching, merging, and pushing files to GitHub, as well as managing repositories and handling errors. Additionally, it explains the differences between Git and GitHub, the use of .gitignore files, and the process for forking and cloning repositories.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Git

The document provides a comprehensive guide on using Git and GitHub, including installation, configuration, and essential commands for version control. It covers topics such as staging, committing, branching, merging, and pushing files to GitHub, as well as managing repositories and handling errors. Additionally, it explains the differences between Git and GitHub, the use of .gitignore files, and the process for forking and cloning repositories.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Git & GitHub

----------------

Git
-----
-> Git is a popular version control system i.e. the practice of tracking and
managing changes to software code.
-> Git was created by Linus Torvalds in 2005.
-> Git is used for:
- Tracking code changes.
- Tracking who made changes.
- Code collaboration.
-> Git does the following:
- Manage projects with Repositories.
- Clone a project to local machine.
- Control and track changes with Stagging and Committing.
- Branching and Merging to work on different parts and versions of a project.
- Pull the latest codes.
- Push the local changes.

Installing Git
-----------------
-> Use the following page to download git: https://git-scm.com/downloads
-> Verify git is installed successfully by entering following command in cmd: git
--version
-> After you enter this command you should see a message like as follows: git
version X.Y
-> If you get an error message "unable to recognize git", then git is not installed
properly.

Configuring Git
-------------------
git config --global user.name "YOUR_USERNAME"
git config --global user.mail "YOUR_EMAIL"

-> Before you configure make sure that you have an account in GitHub or create one
on github.com
-> After you have your account replace "YOUR_USERNAME" & "YOUR_EMAIL" with your
credentails.
-> Here we used --global to make sure that the configuration applies to all
repository's in our computer.
-> If we simply want to set the configuration to the current repo, then remove '--
global'.
-> Without Configuring git if we try to push projects to GitHub, they may be pushed
successfully but your
contributions are not displayed in your profile.

Stagging & Commiting in Git


-------------------------------------
-> In Stagging Environment, Stagged files are there i.e. the files that are ready
to be committed.
-> In Committing Envrionment Commits takes place. Commits are just like save
points,
they keep track of our work along with a provided required message.

git help
----------
-> If we encounter any error while working with any git command, then we can enter
the below commands:
- git command -help: to see the availble options of specific command.
- git help --all: to see the available options for all commands.

Git Branches
-----------------
-> In Git, a branch is a new/separate version of main repository.
-> Branches allow us to work on different parts of a project without impacting the
main branch.
-> When the work is done, a branch can be merged with the main branch.
-> We can easily switch between different branches.
-> To create a new branch in git, enter the below command:
- git branch BRANCH_NAME
-> To switch to an Existing branch in git, enter the below command:
- git checkout BRANCH_NAME
-> To create and switch to a new branch, enter the below command:
- git checkout -b BRANCH_NAME
-> To merge an Existing branch to the main branch, enter the below command:
- git merge BRANCH_NAME
-> To remove an Existing branch, enter the below command:
- git branch -d BRANCH_NAME

git pull
---------
-> Let us imagine a scenario where you along with your team are working on a
project and everyone has to complete
their own piece of work in slots, now whenver you start working on the project
by cloining it to your local machine
you need to get the updated files every time when you start working and for
this purpose we can use git pull.
-> Git pull is a combination of two commands:
- fetch: It gets all the changed history of a branch/repository.
- merge: It combines the branch with present branch.
-> To implement the above scenario of working on a project and detecting changes we
need to use the following:
- fetch
- merge
-> Using git pull this two steps can be implemented as a single step:
git pull == git fetch + git merge

git status
------------
It displays the current status of the repository including tracked/untracked files
and changes staged for commit.

git log
--------
This command displays the commit history of thr repository.

git diff
--------
This command shows the differece between changes in the working directory and
stagging area of recent commit.
Git Fork
----------
-> As the heart of git it colloboration, git does not allow us to add code to
someone else's repository without access.
-> A fork is a copy of a repository and it is useful when we want to contribute to
someone else's code.
-> Fork is not a git command, but it is offered in GitHub.
-> Now we have our own fork, but only on GitHub and we also want a clone on our
local computer.
-> A clone is a complete copy of a repository and it can be done by getting the
repository URL.
-> Command to clone: git clone https://[email protected]/username/repository.git
-> After cloning, add few changes and try to push the changes to git by using "git
push origin" command.
-> This will create a pull request in the main repository and the owner can accept
the pull request
or reject the pull request to implement changes.

Git .gitignore
------------------
-> When sharing our code with others, there may be some files which we don't want
to share.
-> In git we can specify which files to be ignored by using a .gitignore file.
-> Git will not track the files specified in .gitignore file.
-> To create a .gitignore file, enter the following command: touch .gitignore
-> In this file we can directly specify the filename, extension of filenames,
directories, etc to be ignored.
-> For example, if i want to ignore a file named as "index.html" then simply by
specifying the filename is enough.

Git Revert
--------------
-> revert is the command that is used to take a previous commit and add it as a new
commit.
-> First we find the logs by using the below command:
- git log --oneline
-> This will display all the commits line by line with a 7 digit commit hashkey.
-> We simply want to revert to the previous commit, then use the below command:
- git revert HEAD --no-edit
-> Here "--no-edit" is used to have the same commit message of the previous one.
-> If you want revert back to some xth previous commit, then use below command:
- git revert HEAD~x

Git Reset
------------
-> Reset is the command that we use when we want to move the repository to previous
commit.
-> Enter this command to get all commits: git log --oneline
-> Get the 7-DIGIT_COMMIT_VALUE for your commit that you want to reset.
-> Enter the following command: git reset 7-DIGIT_COMMIT_VALUE.
GitHub
---------
-> GitHub is not same as Git.
-> GitHub makes tools that use Git.
-> GitHub is the largest host of source code.
-> GitHub is owned by Microsoft since 2018.

Pushing Files To GitHub


-----------------------------
-> If your project directory / folder has very limited number of files, then you
can follow below steps:
- open github.com in web browser and sign-in to it.
- click create repository and name it as per your wish.
- If interested add a "readme.md" file as it is optional.
- Click creat e repository and then directly drag and drop your project
folder.
- Wait for some time until all files are uploaded.
- Edit the commit value or leave it default.
- click commit changes to save all the files in your repository.
- verify your files are uploaded or not.

Note-1
---------
-> "readme.md" file is a file that explains about your files that you have added in
your repository,
here you can add all the information about project files, etc.

-> The above method of "drag and drop" works only when the file count limit is
below 100.

-> If your project directory / folder have more than 100 files, then follow below
steps:
- open command prompt or terminal.
- Navigate to the project directory using "cd" command.
- cd C:\Users\DELL\Desktop\SDP\Portfolio
- After navigating enter below commands:
- git init
- git add .
- git commit -m "SOME COMMIT NAME"
- git remote add origin https://github.com/username/repository
- git push -u origin master
- After executing all the above commands without any errors, your folder is
added to GitHub.

Note-2
--------
-> Here Portfolio is the Project folder which is added by navigating to that folder
using following cd command:
cd C:\Users\DELL\Desktop\SDP\Portfolio.
-> "git init" is used to initialize a new repository in your project folder.
-> "git init" creates a .git file which consists of all information about commits
and file metadata, etc.
-> "git add ." is used to add all the files to the repository.
-> "git commit -m "SOME COMMIT NAME"" is used to create a snapshot of changes.
-> "git remote add origin https://github.com/username/repository" is used to
connect to our repository which we created.
-> To get this "https://github.com/username/repository" simply open the repo and
copy the address bar url of your browser.
-> "git push -u origin master" is used to push the folder to github reposiory.

Note-3
--------
-> The above execution of commands with create a "Master" branch in repository and
your folder files are added to that master folder.
-> By default GitHub repository consists of a "Main" branch.
-> If you want to make your "Master" branch as default, then follow below steps:
- click settings button, you will be redirected to settings.
- Under Default branch you will see a birectional arrow symbol "<=>" somewhat
like this.
- Click on it and you can switch to the branch you want.
- Now when you open your instead of "Main" branch your "Master" branch will
appear as default.
- If you want to delete "Main" branch then you can find a button as branches
with branch count.
- click on it and you can delete the "Main" branch.

Steps to Delete / Clear folder from Repository


---------------------------------------------------------
-> Navigate to the project directory using "cd" command: cd C:\Users\DELL\Desktop\
SDP\Portfolio
-> Enter anyone of below command based on your utility:
Remove-Item -Path .git -Recurse -Force (For Windows PowerShell)
rmdir /s /q .git (For Command Prompt)
rm -rf .git (For bash)
-> This command is used to remove the intialized git repository from our folder.
-> After that open GitHub.com and go to settings of your project and scroll down to
last,
you will find a button to delete repository.

Steps to update new changes to an existing Repository


-----------------------------------------------------------------
----
-> enter "git status", this will show the updated changes.
-> enter "git add ." to add the changes into repository.
-> enter "git commit -m "SOME COMMIT" to save all the changes.
-> enter "git push -u origin master" to update the repository.

You might also like