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

Git & GitHub

Uploaded by

sates63656
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Git & GitHub

Uploaded by

sates63656
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Git & GitHub

17 August 2023 18:32

What is Git ?
— It is a free and open source version control system. Version Control System is a special tool
which is used to track changes in the code. They also help us to collaborate.

— GitHub is an online version control system which is based on git and which is used to host our
code.

GitHub Repo
— A GitHub repository (or repo) is a place where you can store and manage your code, files, and
projects using Git. You can create your own repo or fork (copy) an existing one from other
developers.

Commits
— A commit is a snapshot of your changes in a repo. You can use git commit command to save
your changes with a message describing what you did. You can also view the history of your
commits using git log command.

GitHub Branches
— A branch is a parallel version of your repo that you can use to work on different features or
tasks without affecting the main branch (usually called master or main). You can use git
branch command to create, list, or switch branches. You can also use git merge command to
combine the changes from one branch to another.

Using Git
— There are different ways to use git, which are using it from Command Line, using from code
editor or using git GUI. The command line way is the most fast, reliable and have no
limitations that's why we are going to learn git using command line.

Configuring Git
— It means to store our details inside our git.

git config --global user.name "Narayan Bhardwaj"

git config --global user.email "[email protected]"

git config --list

— We can run the same command to update our given information at any time.

Status command
— There are basic 4 statuses of the files inside a git repo which are untracked, unmodified,
modified and staged.

git status

git rm --cached <file>

1. Untracked: These files are not part of the git repository and have not been added to the
staging area or committed yet. They are usually new files that have been created or

Miscellaneous Page 1
staging area or committed yet. They are usually new files that have been created or
copied into the project, but they can also be files that have been ignored by git using
the .gitignore file.

2. Staged: One of the key concepts in Git is the staging area, which is also known as the
index or the cache. The staging area is where you prepare your changes before
committing them to the repository. Staged status in Git refers to the state of files that
have been added to the index (also known as the staging area) and are ready to be
committed. The index is a snapshot of the working directory that Git uses to create a
new commit. Staging files allows you to selectively choose which changes you want to
include in a commit, and also to review and verify your changes before committing
them.

3. Unmodified: Unmodified status in Git is a term used to describe the state of a file that is
tracked by Git and has not been changed since the last commit. It is also known as a
clean file.

4. Modified: A file can be in modified status when it is edited, deleted, renamed, or moved
in the working tree. Modified files are not ready to be committed until they are staged
using git add command.

Init Command
— It is used to create a git repository on local machine. We have to run this command inside the
folder which we want to initialize as git repository.

git init

— Now we will tell the git where to push these files, by providing the origin. We need to create a
repo on GitHub through website and use that link with the following command. After that we
can verify the remote, such that our local repo and GitHub repo are linked to each other. We
can also use git branch to check the branch.

git remote add origin <link>

git remote -v

git branch

— By default name of the branch will be master, but we can also rename our branch. Usually the
branch is named as main.

git branch -M main

— and then finally we can push the code to GitHub by using git push command

git push origin main

— We can also set origin main as default by writing or we can also use "--set-upstream" instead
of "-u". There will be no functional difference.

git push -u origin main

— This will set the upstream default and we can directly use git push. When there is no
untracked file then we can add and commit simultaneously using the command mentioned
below.

git commit -am "some message"

Basic Commands
1. add: Adds new or changed files in your working directory to the Git staging area. It also marks

Miscellaneous Page 2
1. add: Adds new or changed files in your working directory to the Git staging area. It also marks
the file as tracked.

git add <filename>

git add . // stages all the files

2. commit: It is the record of a change.

git commit -m "some message"

3. push: Upload local repo content to remote repo.

git push origin main

Branch Commands
— All these branch actions will be performed locally on machine.

git branch // view branch name

git branch -M main // rename current branch

git checkout <branch name> // navigate to another branch

git checkout -b <new branch name> // create a new branch

git branch -d <branch name> // delete a branch

git push --set-upstream origin feature // use to push the


commit on feature branch

Merging code
— There are two ways of merging 2 branches. First one is using git diff and git merge command.

git diff <branch name> // to compare commits, branches, files


& more

git merge <branch name> // to merge 2 branches

— and the second way is by creating a PR (Pull request). It lets you tell others about changes
you've pushed to a branch in a repo on GitHub.

Clone command
— Clone command is used to clone any repository on our local machine.

git clone <some link>

What is forking
— A fork is a new repo that shares the code and visibility settings with the original upstream
repo. Fork is a rough copy. It creates a new repo in your GitHub account which will be a copy
of forked repo.

Pull Command
— It is used to fetch and download content from a remote repo and immediately update the
local repo or a forked repo to match that content.

git pull origin main

Miscellaneous Page 3
Merge Conflicts
— An event that takes place when Git is unable to automatically resolve differences in code
between two commits.

Fixing Mistakes
— Whenever we have staged unwanted changes, and want to unadd them we use our reset
command.

git reset <file name> // used to unstage the selected file

git reset // used to unstage all the staged files

— We can also undo the last commit using the following command

git reset HEAD~1

— If we want to undo many commits, then we use commit hash

git log // used to show all commits and commit hash

git reset <commit hash> // used to undo multiple commits


using commit hash

git reset --hard <commit hash> // used to remove all commits


after the given commit hash

Miscellaneous Page 4

You might also like