Git & GitHub
code management
version control system
collaboration: many developers work on the same project
Git and GitHub are different things Git s a tool and GitHub is a platform where we can store anything
Why it is important?
Tracking the changes and updates. We can see who made which changes. Git also provides when and why a change was made.
Allowing them to work collaboratively. Software development projects usually require many people to work together. Git provides developers with a systematic way of doing that. Thus, the developers focus on the project instead of extensive communication sessions between the other developers.
There are other version control Some of the reasons that make Git the preferred one are:
Speed
Scalability
Being free and open source
Version Control:
In the git version control system if you put anything it will have a version it will have the time it will have an author it will have a hash_id.
Git is a version control system.
Hash - when you delete a file in git and one day you need that file that you have deleted so, in that case, u retrieved the file hash is a number and that number is stored in git that's why you retrieved the file.
Types Of Version Control Systems?
versions controls systems are two types:
Distributed version control system(DVCS)
Git is a Distributed Version Control System.
In DVCS every contributor has a local copy or clone of the main repository i.e everyone maintenance a local repo of their own which containers all the files & metadata present in the main repository.
Centralized version control system(CVCS)
GitHub is a Centralized Version Control System
suppose u have a repository and everybody came and distribute the code and suddenly the server is crash, in that case, everything in your code is gone.
Git Three Stages:
Files in a repository go through three stages before being under version control with git:
Untracked: the file exists, but is not part of git's version control
Staged: the file has been added to git's version control but changes have not been committed
Committed: the change has been committed.
Git Command:
- Repository meaning is a simple folder
so firstly you install git
You can check the current version of git with git --version command.
git --version
git init
git version 2.34.1
The Git config
the command is super helpful. Especially when you are using Git for the first time, or you have a new Git installation. This command will set up your identity - Name and Email address. And this information will be used with every commit.
git config --global user.name "Dev_Safia"
git config --global user.email "safiakhatoonami@gmail.com"
git config list
the command will show all Git config properties
git config --list
user.name=Dev Safia
user.email=safiakhatoonami@gmail.com
mkdir git_dev_tut
cd git_dev_tut/
mkdir project{1..3}
project1 project2 project3
cd project1/
This is probably the first command you use to start a new project in Git. This command will create a blank new repository, and then you can store your source code inside this repo.
git init
ls -a
. .. .git
vim dev_safia.txt
one thing is very important your repo is not blank otherwise it is not added and committed.
Git status
is one of the important git commands.
Mainly git status shows
Your current branch.
List of files that are committed or pending changes.
git status
git add
the command will be used to add new files to the staging area.
git add dev_safia.txt
(it will add a single file to your staging area)
git add *
( this option will add all the modified and new files to the staging area)
the git commit
will add your changes to your local repository.
git commit -m "commit this file"
delete this file
rm -rf dev_safia.txt
git status
and again restore the file in this case one thing is proven when you delete something then you retrieved again this thing with the help of the git restore command
Note: one question in my mind I delete without staging the file and then can retrieve the file so the answer is no u can't
git restore dev_safia.txt
git log
the command helps you to see all previous commit messages.
git log
git log --all
View only one line changes. It helps to have a quick look at previous commits.
git log --one line
You can view all differences of changes of each commit with -p option
git log -p
step 1: And now you create a repo in GitHub
dev_safia_blog
step 2: and then create your local same as
mkdir dev_safia_blog
step 3: and again go to your GitHub and copy the HTTPS code
git clone
https://github.com/Safiakhatoon767/dev_safia_blog.git
ls
dev_safia_blog
cd dev_safia_blog/
git init vs. git clone
A quick note: git init
and git clone
can be easily confused. At a high level, they can both be used to "initialize a new git repository." However, git clone
is dependent on git init
. git clone
is used to create a copy of an existing repository. Internally, git clone
first calls git init
to create a new repository. It then copies the data from the existing repository.
How to push our local files and folders in GitHub:
create a new repo in GitHub and local
mkdir DevOps
cd DevOps
git init
create some files in your local repo
touch safia{1..3}.txt
ls
safia1.txt safia2.txt safia3.txt
git add.
(all files will add )
git commit -m "add this files"
show remote origin URL
git remote -v
Git remote command acts like a border, and If you need to connect with the outside world, you have to use the Git remote command. This command will connect your local repository to the remote.
git remote add origin
https://github.com/Safiakhatoon767/safia-blog-1.git
git remote -v
origin github.com/Safiakhatoon767/safia-blog-1.git (fetch)
origin github.com/Safiakhatoon767/safia-blog-1.git (push)
In simple words, the git push command is used to send your local changes to the remote repo.
git push origin master
so in my case, I find the error such that remote: Support for password authentication was removed on August 13, 2021. remote: Please see docs.github.com/en/get-started/getting-star.. for information on currently recommended modes of authentication. fatal: Authentication failed for 'github.com/Safiakhatoon767/Devops.git' maybe you didn't find an error
How to solve this error:
step 1: go to GitHub settings
step 2: go to the developer setting
step 3: personal access token
step 4: Tokens classic
step:5 generate a new token
step 6: generate a new token classic
step 7: copy your access token now.
step 8: go to your local terminal and paste a URL like this
git remote set-url origin https://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@github.com/Safiakhatoon767/DevOps.git
git push origin master
Then your file is pushed locally to the server
what is a fork?
A fork is a copy of a repository.
How do I completely disconnect a local Git repository from all remote branches?
git remote rm origin
Difference between git clone and git init?
git clone
the command will use an existing repository to copy. There is one main difference between the git init and git clone.
You will use the Git clone when you need to make a copy on an existing repository. The git clone command internally uses the git init command first and then checks out all its contents.
Difference between a GitHub fork and a Git clone?
FORK:
1. Forking is done on the GitHub Account.
2. Forking a repository creates a copy of the original repository on our GitHub account.
3. Forking is a concept
4. Forking is just containing a separate copy of the repository and there is no command involved
CLONE:
Cloning is done using Git
Cloning a repository creates a copy of the original repository on our local machine.
Cloning is a process.
Cloning is done through the command git clone and it is a process of receiving all the code files to the local machine.
What is the difference Between Main Branch and Master Branch??
when a repo is initialized in Git a branch will be created by default. This default branch is called MASTER while Github has a default branch when we create any repository which is called MAIN.
ADVANCED GIT COMMAND
BRANCH:
Most of the time, you have multiple branches in your Git repository. In simple terms, the branch is an independent line of code development.
product is the same so one repository but a different task.
Each task has one separate branch.
changes are present in that particular branch.
The default branch is "Master"
NOTE: The file Created in the workspace will be visible in any of the branch workspaces until you commit once you commit then that file belongs to that particular branch.
you can create any number of branches.
branch concepts useful for parallel development
when creating new branch data from the existing branch is copied to a new branch.
command:
all commit is shown
git log --one line
show branch
git branch
create a new branch
git branch <branch name>
orgit checkout -b<branch_name>
To switch branch
git checkout <branch name>
delete branch
git branch --delete <branch name>
delete branch
git delete -D <branch name>
(forcefully delete)
git status
(it is showing currently what is your branch)
if you want to switch and create a new branch, you can use the checkout command with the -b option.
git checkout -b feat/add_Devops
(create a new branch)
touch Devops.txt
git add Devops.txt
git commit -m "add this file"
git status
git push origin feat/add_Devops
(New branch is created on GitHub)
here feat is for feature it is my branch name you can choose according to you
switch to the master branch
git checkout master
MERGE:(copy-paste)
Git merge helps you to integrate changes from two branches into a single branch.
we use a pulling mechanism to merge branches
when you pull your code in the master branch it also exists code in that branch.
you can't merge branches of the different repositories e.g like your friend repo
for merge
git merge <branch name>
to verify the merge
git log
to push central repo like GitHub
git push origin master
Git conflict:
suppose you have two branches and you create a file of the same name in both branches but whenever you merge these branches in that time
the important thing is data are different in both files in that case conflict occurs.
conflicts occur when you merge branches.
Q: What is a merge conflict in Git and how do you resolve it?
Ans: A merge conflict occurs when Git is unable to automatically merge changes from two different branches. This can happen when two or more people are working on the same file at the same time and make different changes to it. When you try to merge these changes, Git will notify you of the conflict and ask you to resolve it manually.
To resolve a merge conflict, you need to open the file(s) that have conflicts and manually edit them to include the changes you want to keep. You can use a diff tool to compare the conflicting versions of the file and decide which changes to keep. Once you've resolved the conflict, you need to stage the changes and commit them to complete the merge.
Git Stashing:
This Git command temporarily stores your modified files. You can work in stashed with the following Git command.
A stash is a temporary storage.
To stash, an item only applies to modified files, not new files.
To stash an item
git stash
To see stashed items list
git stash list
To apply stashed items
git stash apply stash {0}
{0} --------- current files
{1}----------last files
Then you can add and commit to clear the stash item
git stash clear
Git Restore:
The "restore" command helps to unstage or even discard uncommitted local changes.
git restore <file name>
Git Reset:
The git reset command is used to remove a file from the staging area.
To reset the staging area
git reset <filename>
git reset.
To reset the changes from both the staging area and working directory at a time
git reset --hard
(conflicts)
git reset --soft
(very important command) (smooth)
Diffrence between revert and reset
before committing you to use the reset command after committing you to use the revert command
revert is used for public files and reset is used for private files.
Git Revert:
The revert command helps you undo an existing commit.
it doesn't delete any data.
it is reverted to its previous stage and generates a new commit id.
your version control history moves forward while the state of your file moves backwards.
To revert a change that you have committed:
git revert <commit 1> <commit 2>
How to delete untracked files:
git clean -n (dry run)
git clean -f (forcefully)
TAGS:
Tag operation allows for giving meaningful names to a specific version in the repository,
To apply tag
git tag -a <tag_name> -m <message> <commit-id>
To see the list of tags
git tag
To see particular commit content by using tag
git show <tag_name>
To delete a tag
git tag -d <tagname>
GitHub Clone:
The git clone command will use an existing repository to copy.
git clone <url of GitHub >
Git diff:
Most of the time, you need to compare two git files or branches before you commit or push. Here is a handy command to do that.
i) to compare the working directory with the local repo:
$ git diff HEAD <filename>
ii) to compare two branches:
$ git diff <source branch> <target branch>
cherry-pick:(particular pick any commit.)
Git cherry-pick is a helpful command. It's a robust command and allows you to pick any commit from any branch and apply it to any other branch.
Usage
$ git cherry-pick <commit-hash>
Git cherry-pick doesn’t modify the history of a repository; instead, it adds to the history.
Rebase:
Git rebase is similar to the git merge command. It integrates two branches into a single branch with one exception. A git rebase command rewrites the commit history.
You should use the Git rebase command when you have multiple private branches to consolidate into a single branch. And it will make the commit history linear.
$ git rebase <base>
squash commit:
squash commit means multiple commits are squashed in one commit.
git merge --squash <branch_name> && git commit -m " "
What is the difference between pull and clone in git?
git clone: Get a copy of a project from a remote location (e.g GitHub) with this command. It downloads a complete copy, and unless you want several copies of a project, you can only use it once per repository.
git pull: This command is used to pull changes from a repository. It normally updates a project's local copy. It downloads the changes from the remote location to the local computer.
git pull --rebase(remote)
Most of the time, you need to do rebase (and no merge) when you use Git pull.
In that case, you can use the option
$ git pull --rebase
git rebase --continue
It will help you to keep the history clean. Also, you can avoid multiple merges.
Difference between merge and rebase:
The first thing to understand about git rebase
is that it solves the same problem as git merge
. Both of these commands are designed to integrate changes from one branch into another
Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way.
The rebase moves all of the commits in master
onto the tip of feature
.
feature ko master mai merge:master k sare commits jayenge features main aur fir features ko merge Kiya jyega master mai.
master---commits-----feature
feature---commits-----master
Pull from another repo and push in my repo
>$ git remote show origin
>$ git remote rm origin
>$ git add .
>$ git commit -m "First commit"
>$ git remote add origin Copied_origin_url
>$ git remote show origin
>$ git push origin master
thank you for reading this blog! I hope you like it if you like then please like my blog **********
Safia khatoon