Git and Github
Git and Github
What is Git?
Git is the most commonly used version control system. Git
tracks the changes you make to files, so you have a record
of what has been done, and you can revert to specific
versions should you ever need to. Git also makes
collaboration easier, allowing changes by multiple people
to all be merged into one source.
Features of Git
Tracks history
Creates backups
Scalable
Supports collaboration
Branching is easier
Distributed development
Git Workflow
Git and GitHub
Note: We should write the changes of our files or projects in commit when
we use the commit command in git.
Ex: Git commit -m “Write changes of file for your better appearance”
What is GitHub
Git and GitHub
GitHub is an increasingly popular programming
resource used for code sharing. It's a social networking
site for programmers that many companies and
organizations use to facilitate project management and
collaboration. According to statistics collected in
October 2020, it is the most prominent source code
host, with over 60 million new repositories created in
2020 and boasting over 56 million total developers.
Git vs GitHub:
The main Git vs GitHub difference is in their
functionality. While they both provide source code
management (SCM) and make merging and sharing
code easier, this is pretty much where their similarities
end. Think of Git as a single computer and GitHub as a
network of multiple interconnected computers, all with
the same end goal but a wildly different role for how to
get there.
Branch in Git
Branch in Git is used to keep your changes until they
are ready. You can do your work on a branch while the
main branch (master) remains stable. After you are
done with your work, you can merge it with the main
office.
Git and GitHub
Untracked
Unmodified
Modified
Staged
1. Untracked state : Files are present in the local directory,
but not added in the github repository index.
2. Unmodified state : Files are already present in directory
or added using $git add command. If some changes did,
then it not get tracked. Also after commiting the
changes file status become unmodified.
3. Modified state : When previously tracked file is edited,
but not commit the changes.
4. Staged state : When files committed and ready to push
in git repository, then they have staged status.
Git and GitHub
Git Revert
Git and GitHub
In Git, the term revert is used to revert some changes. The git
revert command is used to apply revert operation. It is an
undo type command. However, it is not a traditional undo
alternative. It does not delete any data in this process;
instead, it will create a new change with the opposite effect
and thereby undo the specified commit. Generally, git revert
is a commit.
Hard reset
You can also use the reset command to undo your last commit.
But be careful – it will change the commit history, so you
should use it rarely. It will move the HEAD, the working
branch, to the indicated commit, and discard anything after:
git reset --soft HEAD~1
The --soft option means that you will not loose the
uncommitted changes you may have.
If you want to go at any status and you realize that what ever
I changed after that state those are not useful and you want
to roll back at that working stage and delete all commit after
that working stage than you can use --hard option.
1. Run git log command : for watching all commit.
2. Copy the commit id to which stage you want to go
back.
3. Type git reset --hard and paste the commit id
Note: before pushing your code on github make sure that you
are in that branch which you want to push on github. And also
check that your working tree is clean or nothing to commit
otherwise first you will have to commit them.
Steps:
Go to your local directory and open git bash there.
Now we have 2 options, 1: If we have not created a git
repository in our system then follow the first method, 2:
We have a git repo in our local system we just want to
push it to the remote system then follow the method 2.
We are following Method 2 Here.
So let’s copy the code on git’s website which looks like
this “git remote add
origin [email protected]:yourusername/repositoryname.git
” and paste it to the git bash and press enter.
Now we will paste the second command which pushes
the files onto the remote server which is “git push -u
origin master”.
It will give you an error that access is denied if you are
doing it first time. Now let’s tackle this.
To fix it, go to your account settings on github, then SSH
and GPG keys. Then click on the new SSH key, add a
title. Now for the key, open git bash and follow the
simple steps.
On git bash type the following edit your email and press
enter: “ssh-keygen -t rsa -b 4096 -C
Git and GitHub
"[email protected]" ”
Now just simply press enter 3 times.
Now run this command “eval $(ssh-agent -s)”.
Now run this “ssh-add ~/.ssh/id_rsa”.
Now on git bash type this to reveal your key “tail
~/.ssh/id_rsa.pub”.
Now it will show you the ssh key. Just simply copy it.
And now let’s head back to the github SSH key page and
paste the key to the key field, press add ssh key. And
done!
Now if you run “git push -u origin master” it will run and
push your files to github.
Note: if it will show an fatal error (remote: Repository not
found), than you need to switch in SSH.
1. Click on SSH and copy URL.
2. Open git and type: $ “git remote set-url origin
Paste_URL” and press enter.
Now you can make changes on your local git repo and
push the changes to the remote repo swiftly.
Branches in git
Basic Branching
First, let’s say you’re working on your project and have a
couple of commits already on the master branch.
2. GIT DIFF
3. GIT CHECKOUT
6. GIT RESET
Topic Branches:
Topic branch is a short lived branch which was created for a
single particular feature or related work. Topic branches can
Git and GitHub
be deleted after the particular feature is added to the project.
Example:
Master
Idea C1
4 Issue solve
implement
C1
C1
1
3
C6
C1 C8
2
C1
C5
0 C7
C9
C4
C3
C2
Git and GitHub
Git alias
Git aliases are a powerful workflow tool that create shortcuts
to frequently used Git commands. Using Git aliases will make
you a faster and more efficient developer. Aliases can be used
to wrap a sequence of Git commands into new faux Git
command. Git aliases are created through the use of the git
config command which essentially modifies local or global Git
config files.
For example:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
commit hash
%h
tree hash
%t
parent hashes
%p
%an
author name
%aN
author name
%ae
author email
Git and GitHub
%aE
author email
%al
author local-part
%ad
committer name
Git and GitHub
%cN
committer name
%ce
committer email
%cE
committer email
%cl
committer local-part
%cd
committer date
%d
ref names
%D
Basic Snapshotting
Description Command
Check status git status
git add <file-
Add a file to the staging area
name.txt>
Add all new and changed files git add -A or
Git and GitHub
Description Command
to the staging area git add .
git commit -m
Commit changes
"<commit message>"
git rm -r <file-
Remove a file (or folder)
name.txt>
Git GitHub
Provides a desktop
Provides a desktop interface called
interface called
git GUI
GitHub Desktop.
GIT SVN
SVN is a centralized
Git is a distributed decentralized
version control
version control system
system.
The git pull origin master fetches all the changes from
the master branch onto the origin and integrates them
into the local branch.
files.
Tries to merge
remote changes with
Command - git fetch origin
your local ones.
git fetch --all
Command - git pull
origin master
Here are the steps that will help you resolve conflicts in
Git:
Bare repositories
store git revision
A .git subfolder is created with all history in the root
the git-related change history. folder of your
repository instead of
the .git subfolder.
Reverting Resetting