GIT
GITHUB
GIT
GIT stands for global information tracker.
It is a version control system. Version control systems are a category of software tools
that helps in recording changes made to files by keeping a track of modifications done
in the code.
It is free and open source which was developed in 2005.
It is written using C language.
Stages in GIT
working directory
git add
staging area
git commit
repository
working directory : Here we write code and maintain it. the file is present in local
machine. This file will be original/initial copy of the project that will be maintained in git.
Staging area : The files on which we worked will be pushed into staging area. From here it
will be pushed into repository.
Commit : A final set of files that will be pushed into repository.
GIT workflow
files files in staging git push
files to be
to be
area committed github
committed
git pull
other github
repositories
Launch an instance and install git in it using command yum install git.
Consider multiple files in your system out of which few files need to be tracked.
Tracking files means enabling git to monitor those files. For enabling the tracking, get
into the folder in which the file is present to be tracked and initiate git using command
git int.
Here, in cloud directory all the files should be tracked. Hence git is initialized in it. If
you want a single file to be tracked, then move the file to another folder or that file
alone can be tracked from root directory . In root directory git should be initialized.
When git is initialized in folder, a hidden file .git will be created which can be checked
using command ll -a.
Now, the files should be added to staging area, using command git add <filename>.
The staged and unstaged files can be checked using git status command.
The file added to staging area should be committed. The command used is git
commit -m “ description” <file to be committed>
git log command shows all the committed files.
git add* - Adds all the files to staging area.
git committ “ description” . - all the files from staging area will be committed.
Git committ shows who made the commit, when commit was made. But if you want
to know what all data was committed, you use git show command. The syntax is git
show committ id.
Tracking all hidden files using command git add.
Untracking the files - git rm --cached <filename>
Untracking the hiddenfiles - git rm --cached <.filename>
Only commit id and commit message to be displayed - git log --oneline. Commit id
displayed here will be of 6 digits only.
To get entire committ id - git log --pretty=oneline
To get “n” number of committs - git log -n
GIT IGNORE :
Consider there are few files in a directory, out of which 1 file should not be tracked
but remaining all the files should be tracked.
The file which should not be tracked must be inserted into .gitignore file.
Now the remaning files can be committed.
Its mandatory that the file name should be .gitignore to add the files which into it
which should not be tracked.
Out of the ffiles, we will track all the files except docker file. Now a new file .gitignore
should be created
Now, docker file is inserted into .gitignore file which is ignored for tracking.
The remaining all files are tracked.
Now if you want to track ignored file, devops file it should be removed from .gitignore
file and it should be tracked individually.
Consider there are tracked files which should not be staged. Those files should be
unstaged and then added into .gitignore file. Those files will be completely ignored
until removed from .gitignore file.
GIT CONFIG
Git config is used to update the details of a person who works on git which would be
needed to track his/her actions. Few configurations which can be done are shown
below:
git config [Link] “Person name”
git config [Link] “ .........@[Link]”
git config [Link] “9273545821”
GIT AMEND
The committ message and author for the latest committ can be changed using this
amend. The command is git commit --amend -m “ new commit message”
The commit id changes after we change the message for commit.
latest commit with old commit comments
same latest commit with updated commit comments
Author can also be changed using amend.
A file as shown below opens, give :wq and exit from the file.
Author changed for latest commit.
Consider you have staged all the files, but missed to commit 1 file. Using amend, the
missed file can be added to the previous [Link] command is git commit --amend
--no-edit
GIT RESET
deleting commits along with actions done as a part of this commit - git reset --hard
HEAD~N
N denotes the no of commits you want to delete.
deleting commits without deleting their actions - git reset --soft HEAD~N
N denotes the no of commits you want to delete.
Here a commit is made to a docker file as data is added to it.
latest commit is deleted
But the commit which was made for the content is not deleted.
git reset --hard HEAD~n -- N number of commits will not be deleted. (N-1) commits
will be deleted.
git update ref -d HEAD - last commit will also be deleted..
GIT MERGE
Master - It is a branch.
When you commit a file, it will be in master branch by default.
When a new branch is created from master branch, all the files present in master
branch will be passed to the newly created branch.
When changes are implemented from one branch to another, merge is used.
master branch new branch from master branch
10 files testing
10 files
10 new files
10 commits
20 commits
successfull
testing
In real time, when there is application code in master branch, changes are not directly
made to code in master branch. A new branch will be created from this master branch,
required code changes will be made in it. The changes will be tested and after that the
final code will again be merged into the master branch.
All the committed files will be present in master branch.
git branch - shows in which branch we are.
git branch branchname - creating a new branch from one branch
git checkout branchname - moving to one branch from other branch
git merge branchname - merging data from one branch from other branch
git checkout -b branchname - command to create new branch and get into it
git branch -d branchname - deleting branch
When you make changes in new branch and if you dont merge changes to master and
if you try to delete the branch, its not possible with above command. Forecful
deletion is needed. It is done using command - git branch -D branchname
You cannot delete a branch when you are in it.
You cannot delete a master branch, atleast one branch should be there to delete it.
git branch -m old branchname newbranchname - changing branch name.
Colour and * tells that you are in the branch.
MERGE CONFLICTS
A merge conflict in Git occurs when you try to merge two branches that have made
different changes to the same part of the same file. Git cannot automatically determine
which change to keep, so it pauses the merge process and asks you to resolve the conflict
manually.
creating a merge conflict
To create a conflict, we would need 2 files with same name from of different
branches with different data.
Created one file aws with some data and committed to master branch
Created another branch from master branch and committed the aws file with
different data
Do a new commit in master branch in aws file.
Now, try merging the changes which in newbranch to master branch. Conflict
arises.
checking the conflict and resolving it
check the aws file. It will the data which was added into it in other branch
HEAD indicates that data is part of master branch file. TAIL indicates thata
data is part of newbranch/subbranch file.
Open the file in vi editor and remove the conflict messages and replace them
with any other messages if needed.
Conflict messages are <<< HEAD , >>> new branch, ===== .
Now track the file again using git add and commit it.
While commiting file name should should be given.
GIT REVERT
git revert <commit id > - Deleting a action specific commit in the middle.
When you revert the action made under commit, commit will be deleted. Here
automatically a new commit will be created showing that file is deleted/ reverted.
created 3 committs for 3 files
A new commit is created after the action done for pqr file is reverted
git reflog - shows all deleted commits
git cherry-pick commitid1 commitid2 - gives back deleted commit
git cherry-pick commitid - merging only specific commit from one branch to
other.
GIT STASH
Stash is used to remove temporary data present in staging area.
Consider you have done a new change in another branch and want to
checkout to master. Without merging the changes made, you cannot checkout
to master.
By using stash, your new changes will be deleted temporarily and you can
checkout to master and view the data in master.
You can come back to another branch and retrieve data.
git stash - removes data which is not committed and is not merged into
another branch.
git stash apply - retrive the data
git stash list - list of stashes
git stash drop stashi id - dropping a stash id
git stash clear - clearing all stashes
GITHUB
GitHub is a web-based platform used for version control.
It simplifies the process of working with other people and makes it easy to collaborate
on projects.
Teams can work on files and easily merge their changes in with the master branch of
the project.
REPOSITORIES
The files or data available in our system will be pushed into github for further
actions. The files can also be pulled from github to our server or system.
In github we have repostitories which are used to keep our pushed files in it.
Repositories are like folders used to store pushed files. They are of 2 types -
private and public.
A public repository can be made private and private repository can be made
public.
A repository should be created before pushing files into it.
git remote add origin <repository url> - Connecting system to a repository
git remote -v - shows whether system is connected to repository or not.
System can be connected to only one repository at once.
GIT PUSH
git push - u origin <branchname> - Pushing data into a repository from
branch. All the committed files will be pushed to github.
created files and committed those
checked the branch and pushed the code by giving name and token.
Files pushed into repository
TOKEN GENERATION
Click on profile settings.
developer settings
under personal access token, select classic token
Generate new token
Give tokenname , validity and scope as repo.
Generate it and keep it safe.
Its possible to generate a new token.
git remote add origin --all - Pushing data from all branches at once.
git remote push -u origin <branchname1> <branchname2> - Pushing data
from muliple branches at once.
git remote rm origin - Disconnecting system from a repository
A repository can be archived, deleted, ownership can be transferred from
one person to another.
GIT CLONE
Downloading the entire github repository into your system/server is called cloning.
After cloning the repository if any changes are made in github repository, they will not
be visible in your server. You need to pull those newly made changes again into your
server.
git clone <repository url> - command to clone a repository.
GIT FORK
Directly copying the changes from one repository to another.
Go to the repository which should be forked to your git account and click on create
new fork.
Give required description and check the box if you want to fork only main branch.
GIT PULL
The changes made in github will be visible or shown in your system/server when you
pull the changes
git pull origin <branchname> - command to pull the changes..
Created a new branch with new file
pulled file into system
PULL BEFORE PUSH
push cloned 5 files
5 files clone
3 committs
3 committs Github
2 new
repo
trying to pushed commits
push
all 5
DEV -1 new DEV -2
commits commits
without
pulling all
to
the commits github
When DEV1-1 tries to push the commits which were made, it cannot be done because
DEV-1 needs to pull extra commit from github which was pushed into github by DEV-2.
COMPARE AND PULL REQUEST
In compare and pull , git will compare the extra files and pulls them.
main branch having 3 files
Select the base file as the file which will have extra files , which need to be
merged into another file.
Here, there is another branch with name “branch1 “ with 2 files in it. Once the 2 files
were added in branch1, automatically compare and pull request option will be shown.
After pulling the required files from main branch to branch1, the branch1 will have all
the files which were present in main branch.
MORE ABOUT REPOSITORIES :
Repository ownership can be transferred from one person to another.
Repositories can be archived.
System can be connected to only repository at once. You cannot connect to mutliple
repositories at once. You can disconnect to a connected repository using command
git remote rm origin.
In code, under blame you will be able to see all the past committs along with actions.