Git and GitHub Tutorial Basics to Advanced 

Syllabus Breakdown:

Git and GitHub Tutorial Basics to Advanced Syllabus Breakdown:

Β·

24 min read

Git & GitHub:

what is Git?

Git Tutorial

Git is a version control system that helps you keep track of changes made to your code over time.

It allows you to collaborate with others on a project, keep track of different versions of your code, and easily revert to previous versions if needed.

what is GitHub?

GitHub is a website that allows you to store your code online and collaborate with others. It uses Git as its version control system, so you can keep track of changes made to your code over time, work with others on the same codebase, and easily share your code with the world.

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.

Version Control:

Git - About 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.

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:

  1. 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.

  2. 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 & Branching Model - CodeProject

    Advantages of Git:

    Git has several advantages, including:

    1. Version Control: Git allows you to keep track of changes made to your code over time, making it easy to revert to previous versions if needed.

    2. Collaboration: Git makes it easy to collaborate with others on a project, allowing multiple developers to work on the same codebase simultaneously.

    3. Branching: Git allows you to create branches, which are separate versions of your code that can be worked on independently. This makes it easy to experiment with new features without affecting the main codebase.

    4. Security: Git provides a secure way to store your code online, with multiple layers of encryption and authentication to protect your data.

    5. Flexibility: Git can be used with a wide range of programming languages and development environments, making it a versatile tool for developers of all kinds.

    Overall, Git is a powerful tool that can help developers work more efficiently, collaborate more effectively, and keep their code safe and secure.

    Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

    https://youtu.be/DthfFMcrQRE

Git Three Stages

Let us see the basic workflow of Git.

Step 1 βˆ’ You modify a file from the working directory.

Step 2 βˆ’ You add these files to the staging area.

Step 3 βˆ’ You perform commit operation that moves the files from the staging area. After push operation, it stores the changes permanently to the Git repository.

Image preview

Git Command

  1. 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

mkdir git_tut_devops_batch

cd git_tut_devops_batch/

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 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 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 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 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

Safia blog

step 2: and then create your local same as

mkdir safia_blog

step 3: and again go to your GitHub and copy the HTTPS code

git clone https://github.com/Safiakhatoon767/safia_blog.git

ls

safia_blog

cd safia_blog/

README.md

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/7xwL0Ttrdqk

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

What is a git repository?

A Git repository is a place where developers store and manage their code files for a software project, allowing them to collaborate, track changes, and work together on the same codebase.

How Git is different from Github?

GitHub vs. Git

Difference between git and GitHub :

Git :

Git is based on CLI (Command line interface)

Git installed locally

Open Source Licence.

GitHub :

GitHub is based on GUI (Graphical user interface)

GitHub hosted in a web

Include a free tier and a pay-for-use tier.

What does git clone do?

It is used to point to an existing repository and make a clone or copy of that repository in a new directory, at another location.

What is a conflict and how will you resolve it?

A conflict in Git occurs when two or more people make changes to the same part of a file or project, resulting in a merge conflict. To resolve it, you need to review the conflicting changes, decide which changes to keep, and manually edit the file to combine the changes. Once the conflict is resolved, you can commit the changes to the repository.

What is the difference between git pull and git fetch?

In easy and short explanation, git fetch downloads the changes from the remote repository to your local repository, but does not merge them with your current branch. Whereas, git pull downloads the changes from the remote repository and automatically merges them with your current branch.

completion.png

Git pull = git fetch + git merge

How will you create a git repository?

To create a Git repository, you can navigate to the directory where you want to store your project, and then use the command git init. This will initialize an empty Git repository in the current directory.

Tell me something about git stash.

git stash is a command in Git that allows you to temporarily save changes that are not yet ready to be committed. It stores the changes in a "stash" and reverts your working directory to the last commit, allowing you to work on a different branch or fix a bug before returning to the original changes.

image.png

Why do we not call git β€œpull request” as β€œpush request”?

We do not call Git "pull request" as "push request" because a pull request is a request to merge changes from one branch to another, while a push request would imply a request to push changes from one branch to another. In Git, pushing changes means sending them to a remote repository, while pulling changes means getting them from a remote repository. Therefore, a pull request is a more accurate term for requesting changes to be merged into a branch.

Untitled design.png

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:

How To Properly Fork a Github Repository

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:

Git Clone - How To Use Git Clone | W3Docs Git Online Tutorial

  1. Cloning is done using Git

  2. Cloning a repository creates a copy of the original repository on our local machine.

  3. Cloning is a process.

  4. 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.

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/mDiyg_DIRtk

ADVANCED GIT COMMAND:

BRANCH

Git Branches: List, Create, Switch to, Merge, Push, & Delete

  1. Most of the time, you have multiple branches in your Git repository. In simple terms, the branch is an independent line of code development.Β 

  2. product is the same so one repository but a different task.

  3. Each task has one separate branch.

  4. changes are present in that particular branch.

  5. 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.

  6. you can create any number of branches.

  7. branch concepts useful for parallel development

  8. 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> or git 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

Question: What is a Git branch, and how do you create and switch between branches?

Answer: A Git branch is a separate line of development that allows you to work on new features or fixes without affecting the main codebase. Creating a branch allows you to experiment with changes without worrying about breaking anything in the main branch.

To create a new branch in Git, you can use the git branch command followed by the name of the new branch. For example, to create a new branch called "feature-branch", you would use the following command:

git branch feature-branch

To switch to the new branch, you can use the git checkout command followed by the name of the branch. For example, to switch to the "feature-branch", you would use the following command:

git checkout feature-branch

Alternatively, you can create and switch to a new branch in one step by using the git checkout command with the -b flag. For example, to create and switch to a new branch called "feature-branch" in one step, you would use the following command:

git checkout -b feature-branch

Once you're working in a branch, you can make changes and commit them as usual. When you're ready to merge your changes back into the main branch, you can use the git merge command to combine the changes.

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/DIUn4RKdWSs

MERGE:(copy-paste)

Git Merge and Merge Conflict - javatpoint

  1. Git merge helps you to integrate changes from two branches into a single branch.

  2. we use a pulling mechanism to merge branches

  3. when you pull your code in the master branch it also exists code in that branch.

  4. 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

Tasks For You :

  1. What is git merge and what does it do?

  2. What is the difference between git merge and git rebase?

  3. What is a merge conflict and how do you resolve it?

  4. Can you use git merge to merge two branches that have diverged significantly?

  5. What is a fast-forward merge and when does it occur?

  6. How do you use git merge to merge a feature branch into the main branch?

  7. What is a three-way merge and how does it work?

  8. Can you use git merge to merge a branch into multiple other branches at once? If so, how?

  9. How do you use git merge to undo a merge that has already been completed?

  10. What are some best practices for using git merge in a team environment?

I hope this helps!

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/DjUu63pjjGQ

Git conflict

How to Resolve Merge Conflicts in Git Tutorial | DataCamp

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.

Tasks For You :

  1. Can you describe a time when you had to resolve a conflict with a coworker or team member? How did you handle the situation, and what was the outcome?

  2. What is your approach to managing conflicts in the workplace? Can you give an example of a successful outcome from a conflict you managed?

  3. How do you handle conflicts with someone who has a different communication style or personality than you?

  4. Can you give an example of a time when you had to navigate a conflict with a superior or someone in a position of authority? How did you approach the situation?

  5. In your opinion, what is the most effective way to resolve conflicts in a team setting?

  6. How do you ensure that all parties involved in a conflict feel heard and understood during the resolution process?

  7. Can you describe a time when you had to negotiate a compromise between two conflicting parties? What was your approach, and how did you resolve it?

  8. How do you handle conflicts that arise due to cultural or generational differences in the workplace?

  9. Can you give an example of a time when you had to apologize for your actions during a conflict resolution process? How did you take responsibility for your behaviour and work towards a solution?

  10. What steps do you take to prevent conflicts from arising in the workplace in the first place?

    Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

    https://youtu.be/LX2cZdAfYQ4

Git Stashing

Git Stash - javatpoint

what is Git Stash?

Git stash is a command in Git that allows you to temporarily save changes that are not yet ready to be committed or pushed to a remote repository. It takes your current working directory and saves it on a stack of unfinished changes that you can return to later. This can be helpful when you need to switch to a different branch or work on a different task without committing incomplete changes. Once you're ready to continue working on the incomplete changes, you can use the git stash and apply the command to restore them to your working directory.

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

TASKS FOR YOU :

  1. What is Git stash and why is it used?

  2. Can you explain the difference between git stash apply and git stash pop?

  3. How can you view the list of stashes that you have saved?

  4. Can you stash changes selectively instead of stashing everything at once?

  5. How can you clear all stashes from the stack?

  6. What happens if you stash changes that conflict with changes in the current working directory?

  7. Can you use git stash to save changes in a remote repository?

  8. How can you create a new branch from a stash?

  9. Can you stash untracked files as well?

  10. What is the command to apply a stash to a different branch?

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/tnWbQ93H_ko

Git Restore:

Git restore is a command in Git that allows you to undo changes made to a file or directory and restore it to a previous state.

The "restore" command helps to unstaged or even discard uncommitted local changes.

git restore <file name>

Advantages of Git Restore :

The advantage of using Git restore is that it allows you to undo changes made to a file or directory and restore it to a previous state.

This can be helpful if you accidentally delete or modify a file and want to revert to a previous version.

Git restore also allows you to selectively restore parts of a file or directory, which can save time and make it easier to manage changes in a Git repository.

Tasks For You :

  1. What does the git restore command do?

  2. What is the difference between git restore and git reset?

  3. How do you use git restore to discard changes in a specific file?

  4. Can you use git restore to restore a deleted file? If so, how?

  5. What is the difference between git restore --source and git restore --staged?

  6. How do you use git restore to unstaged changes?

  7. How do you use git restore to restore a file to a specific commit?

  8. What is the syntax for using git restore to restore a file to a specific commit?

  9. How do you use git restore to restore a file to its state in the previous commit?

  10. Can you use git restore to restore a file that has been modified but not yet committed? If so, how?

I hope this helps!

Video Link Here πŸ‘‡πŸ‘‡πŸ‘‡

https://youtu.be/vmOXjRwMZZw

Git Reset

Git Reset - Atlassian git tutorials

Git reset is a command that allows you to move the current branch pointer to a different commit. This can be useful if you need to undo changes that have been made to your code. There are three modes of git reset: soft, mixed, and hard.

  • Soft reset moves the branch pointer to a different commit, but leaves the changes you made in the working directory.

  • Mixed reset moves the branch pointer and resets the staging area to match the commit you specify. This means that changes you made are not lost, but they are no longer staged for commit.

  • Hard reset moves the branch pointer and resets the staging area to match the commit you specify. This also means that any changes you made are completely removed and cannot be recovered.

It is important to note that git reset should be used with caution as it can potentially delete changes that have not been committed yet.

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 (smooth)

git reset --mixed (default)

Tasks For You :

  1. What is the difference between git reset and git revert?

  2. How do you undo the changes made in the last commit using git reset?

  3. What is the difference between soft, mixed, and hard resets in Git?

  4. Can you reset a specific file or directory in Git?

  5. What happens to the commit history when you use git reset?

  6. Can you reset a commit that has already been pushed to the remote repository?

  7. How do you undo a git reset command?

  8. What is the syntax for using git reset?

  9. What are some best practices for using git reset?

  10. How do you use git reset to unstaged changes?

    Git Revert:

    Git revert is a command in Git that is used to undo a previous commit. When you use git revert, a new commit is created that undoes the changes made in the previous commit. This is different from git reset, which removes the previous commit entirely.

    The git revert command can be useful if you have made changes to your code that you want to undo, but you don't want to completely remove the commit from your Git history. By using git revert, you can undo the changes while still keeping a record of the original commit.

    It's important to note that git reverts only undoes changes made in a single commit. If you want to undo changes made in multiple commits, you'll need to use git revert multiple times.

    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>

    Can you explain the difference between git reset and git revert?

    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.

How to delete untracked files

git clean -n (dry run)

git clean -f (forcefully)

Tasks For You :

  1. What is git revert and when would you use it?

  2. Can you explain the difference between git reset and git revert?

  3. What happens when you run git revert on a commit that has already been reverted?

  4. How can you revert a merge commit using git revert?

  5. What is the difference between git revert and git cherry-pick?

  6. How can you revert a range of commits using git revert?

  7. What is the advantage of using git revert over git reset for undoing changes?

  8. Can you use git revert to undo changes made to a file in a specific commit?

  9. What happens if a conflict occurs when running git revert?

  10. How can you view the changes made by git revert before committing the changes?

    https://youtu.be/MM7dFozdZ8w

TAGS:

A tag in Git is like a bookmark that marks a specific point in a project's history, making it easier to return to that point later. It's often used to mark important milestones or specific versions of the code.

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>

https://youtu.be/q1SIaN3rMfY

cherry-pick:(particular pick any commit.)

Git Cherry-Pick - Scaler Topics

Cherry-picking is a feature in Git that allows you to apply a specific commit from one branch to another. This can be useful if you want to incorporate a particular change or fix into another branch without merging the entire branch. To cherry-pick a commit, you simply identify the commit hash and apply it to the target branch using the git cherry-pick command. This creates a new commit in the target branch with the changes from the original commit. Keep in mind that cherry-picking can lead to conflicts if the same changes have already been made in the target branch, so it's important to review the changes carefully before applying them.

Usage

$ git cherry-pick <commit-hash>

Git cherry-pick doesn’t modify the history of a repository; instead, it adds to the history.

Rebase:

Rewriting History with Git 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.

$ git rebase <base>

What is the difference between pull and clone in git?

In Git, "clone" is used to create a copy of a remote repository on your local machine. It downloads the entire repository with all its contents and version history.

On the other hand, "pull" is used to update your local repository with the changes made in the remote repository. It only downloads the changes made since your last update and merges them with your local repository.

So, in simple terms, "clone" is used to download the entire repository for the first time, while "pull" is used to download only the changes made since your last update.

Difference between merging and rebasing

Merging master into the feature branch

Merging and rebasing are two different ways to integrate changes from one branch into another. Merging creates a new commit that combines the changes from the source branch into the destination branch. This results in a merge commit that has two parent commits, one from each branch. Merging is useful when you want to preserve the entire history of the source branch, and when there are multiple contributors making changes to the same codebase.

On the other hand, rebasing moves the entire source branch to the tip of the destination branch, effectively re-writing the history of the source branch. This creates a linear history as if all the changes were made on a single branch. Rebasing is useful when you want to keep the commit history clean and easy to understand, and when you want to avoid merge conflicts by incorporating changes from the destination branch before applying your changes.

Rewriting History with Git Rebase

In summary, merging creates a new commit that combines changes from two branches, while rebasing moves the entire source branch to the tip of the destination branch, resulting in a linear history of changes.

squash commit:

git pull --rebase(remote):

Β