Git and GitHub
Git and GitHub
Git and GitHub
ü Introduction
ü GitHub Administration (Org/Repo/Users/Team Creation)
ü GIT Commands
ü .gitignore file
ü Create Branch
ü Create Tag / Releases
ü Personal Access Token Generation
ü SSH Key Generation
ü Git Branching Strategy
ü Pull Request Creation and Merging
ü Git Hooks
ü README.md file
ü Git Best Practices
Introduction
---------------------------------------------------------------------------------------------------------------------
GitHub Administration
---------------------------------------------------------------------------------------------------------------------
Git Commands
When you install Git-bash, the first thing you should be doing is setting up your user
details as follows only one time.
You can also check what Git thinks a specific key’s value is by typing git config <key>:
Task 1: Create the git local repository in local machine (Laptop/Desktop), add one file
(DBConnect.java) and update that file, create the github remote repository
(https://github.com) and move the local code to github repository.
# cd ~/Desktop
# mkdir git-practice-commands
#cd git-practice-commands
#git init : Create a local Git empty repository.
Initialized empty Git repository in /Users/MithunReddy/git/git-practice-commands/.git/
#git status : Gives the status of your untracked files.
#touch DBConnect.java
#git status
#vim DBConnect.java
#git add DBConnect.java: Add the files(here DBConnect.java) into your staging area.
#git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
#git status
On branch master
#vim DBConnect.java
On right side top corner click on “+” symbol and click on “New repository” and give the
Repository name and click on Create repository.
#git remote add origin Remote Repo URL : Adding the URL for the remote repository
where your local repository code will be pushed.
# git remote –v :
#git remote show origin : It will give the information on a particular remote (here origin is the
remote name)
# git remote remove origin : It will remove the remote origins.
#git push origin master : Push the changes in your local repository to GitHub remote
repository. (Here push is the git command , origin is the remote name and master is the
branch name)
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 479 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To [email protected]:devopstrainingblr/test.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
#git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
#git show --pretty="" --name-only << Commit ID >> : It will display all the files which are
committed in that particular commit.
#git reset <<File Name>> : To untrack the tracked files (revert back to working area from
staging area.).
#git revert <<Commit ID>> : It will revert the changes committed in that particular commit id
from local repo.
#git push origin master -f: It will revert the changes from remote repo.
When developers are creating something (an application, for example), they are making constant
changes to the code and releasing new versions, up to and after the first official (non-beta)
release.
Version control systems keep these revisions straight, and store the modifications in a central
repository. This allows developers to easily collaborate, as they can download a new version of
the software, make changes, and upload the newest revision. Every developer can see these new
changes, download them, and contribute.
Some times we don’t want to commit the files, which are generated by IDE like .project and
.classpath files or some node module folders like node_module folder into a git repository.
To ignore these files and folders to commit we will create one file called .gitignore and we will
keep the file names or directory names which we don’t want to commit as follows.
https://www.atlassian.com/git/tutorials/saving-changes/gitignore
Branches
Branch
Ø Branches are used to create another line of development.
Ø By default, Git has a master branch, which is same as trunk in Subversion (SVN).
Usually, a branch is created to work on a new feature.
Ø Once the feature is completed, it is merged back with the master branch and we delete the
branch.
Tags
Ø Tags similar to branches, but the difference is that tags are immutable.
Ø It means, tag is a branch, which nobody intends to modify. Once a tag is created for a
particular commit, even if you create a new commit, it will not be updated.
Ø Usually, developers create tags for product releases.
#git branch -v: It will display all the branches in your repo, and also tell you what branch you're
currently in.
Tags
git tag : It will displays the tags.
git tag <<Tag Name>> : It will create the tag.
git push origin tag <<Tag Name>> : It will push the tag to remote repo.
git push origin --tags: It will push all the tags to remote repo.
Note: Tags are not automatically pushed when you push a branch or use the --all option. The --
tags flag sends all of your local tags to the remote repository.
---------------------------------------------------------------------------------------------------------------------
git stash: git stash temporarily shelves (or stashes) changes you've made to your working copy
so you can work on something else, and then come back and re-apply them later on. Stashing is
handy if you need to quickly switch context and work on something else, but you're mid-way
through a code change and aren't quite ready to commit.
git stash show : This command shows the summary of the stash diffs.
The above command considers only the latest stash.
git stash show -p : It will give you the detailed list of differences.
git stash show stash@{1}:
git stash apply stash@{0}:
git stash drop stash@{0} :
git stash list
vim mithun.txt
git stash
git stash list
git stash pop: It apply the latest stash and then immediately drop it from your stack.
git stash pop stash@{1} : It apply the particular stash and then immediately drop it from your
stack.
---------------------------------------------------------------------------------------------------------------------
git cherry-pick: Cherry picking in git means to choose a commit from one branch and apply it
onto another.
git log
git branch
git checkout master
cat mithun.txt
git cherry-pick <<CID>
cat mithun.txt
When you get the above error while committing the code from local repository to remote
repository execute the following command in git bash.
---------------------------------------------------------------------------------------------------------------------
SSH Key
SSH keys are a way to identify trusted computers without involving passwords. You can
generate an SSH key and add the public key to your GitHub account.
#ls -al ~/.ssh ---> To see if existing SSH keys are present in machine.
Generate SSH Key
# ssh-keygen -t rsa -b 4096 -C “MySSHKey”
Here-t --> Specifies the type of key to create. The possible values are "rsa1" for protocol version
1 and "rsa" or "dsa" for protocol version 2.
-b --> Specifies the number of bits in the key to create. For RSA (Rivest, Shamir, and Adelman)
keys, the minimum size is 768 bits and the default is 2048 bits. Generally, 2048 bits is
considered sufficient. DSA (Digital Signature Algorithm)keys must be exactly 1024 bits as
specified by FIPS 186-2.
-C --> Provides a new comment.
cat ~/.ssh/id_rsa.pub
Add SSH key to GitHub
Click on Setting ---> SSH and GPG keys ---> New SSH key or Add SSH key ---> Provide the
name for Title and copy SSH key Key field ---> Click on Add SSH key button
---------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
What is the difference between git fetch and get pull?
Ans) git fetch : It will get the update from git remote repo and will update your local repo. But it
will not merge with Local working copy.
git pull : It will get the update from git remote repo and will update your local repo as well it
will merge with Local working copy also.
---------------------------------------------------------------------------------------------------------------------
Personal Access Token (PAT)
#git commit --amend -m "an updated commit message" : Change most recent Git commit
message.
#git checkout -b <<Branch name>> : It will create the branch from currently using branch and
will switch to new branch.
#git checkout -b feature master: It will create a branch called feature from master branch and
will switch to new branch (featur).
#git checkout -b release-aadhar master: It will create a branch called relese-aadhar from
master branch and will switch to new branch.
git branch -a : It will display all the remote and local repo branches.
git config http.sslVerify false : To disable SSL verification for that singular repository
git config --global http.sslVerify false : To disable the SSL verification for Globally (For all
repositories) --> Not suggested way
git clone <<Git URL>> : To get the code from repository into your local machine.
git log <FileName>: It will display the commits related to the specified file.
git log -2 StringUtilities.java: It will display 2 commit ids related to the StringUtilities.java file.
git log --stat: It will give all the files which are altered and number of lines that were added or
deleted from each item.
git log --graph --decorate: --graph flag draws a text-based graph of commits on left side of
commit message. --decorate adds names of branches or tags of commits shown.
git log --author="[email protected]": It will display all the commits which are
committed by a particular author.
git rm: Removes files from your index and your working directory so they will not be tracked.
-------------------------------------------------------------------------------------------------------------------
What is git rebase?
-------------------------------------------------------------------------------------------------------------------
Git Branching Strategy
-------------------------------------------------------------------------------------------------------------------
Git Hooks
Git Hooks are the scripts which trigger when you perform a specific action in Git.
After you are initialising the git repo, using git init command, it will create a one folder called.
git and it contains some sub folders called hooks, branches, info, objects... etc
There are some predefined client-side hooks are available in hooks folder.
applypatch-msg.sample
post-update.sample
pre-push.sample
prepare-commit-msg.sample
commit-msg.sample
pre-applypatch.sample
pre-rebase.sample
update.sample
fsmonitor-watchman.sample
pre-commit.sample
pre-receive.sample
To enable any hook from above list, remove the “.sample” from each hook.
Ex:
To enable pre-commit.sample hook, rename this file to "pre-commit".
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
-------------------------------------------------------------------------------------------------------------------
README.md
You can add a README file to your repository to tell other people why your project is useful,
what they can do with your project, and how they can use it.
A README is often the first item a visitor will see when visiting your repository. README
files typically include information on:
Project Title
One Paragraph of project description goes here
Getting Started
These instructions will get you a copy of the project up and running on your local machine for
development and testing purposes. See deployment for notes on how to deploy the project on a
live system.
Prerequisites
What things you need to install the software and how to install them
Give examples
Installing
A step by step series of examples that tell you how to get a development env running
Say what the steps will be.
Deployment
Versioning
Use versioning for each release and keep track of versions.
https://semver.org/
Authors
Add here Authors names.
License
https://gist.github.com/PurpleBooth/109311bb0361f32d87a2
-------------------------------------------------------------------------------------------------------------------
Git Best Practices
----------------------
-------------------------------------------------------------------------------------------------------------------
Resources:
---------------
https://github.com/
https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
https://www.atlassian.com/git/tutorials/comparing-workflows/
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
http://www.vogella.com/tutorials/Git/article.html
https://help.github.com/articles/duplicating-a-repository/
https://www.atlassian.com/git/tutorials/git-stash
https://nathanhoad.net/tags/git
http://rogerdudler.github.io/git-guide
http://nvie.com/posts/a-successful-git-branching-model/
https://www.git-tower.com/blog/git-cheat-sheet/