Git and Github Tutorial: Beginner to Advanced (Part 2)
Tags: github, git, beginners, tutorial
Published: 2022-06-05 03:17:12

## Table of Content

*<br>*
>
📌 [Introduction](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-1-32m6#Introduction)
>
📌 [Git Terminologies](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-1-32m6#Git%20Terminologies)
>
📌 [Setup of Git and Github on System](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-1-32m6#Setup%20of%20Git%20and%20Github%20on%20System)
>
📌 [Concept of Github and Git](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-1-32m6#Concept%20of%20Github%20and%20Git)
>
📌 [Staging](#Staging)
>
📌 [Beginner commands of Git](#Beginner commands of Git)
>
📌 [Merging Conflicts](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Merging%20Conflicts)
>
📌 [Resolving Conflicts](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Resolving%20Conflicts)
>
📌 [Some Practices to avoid Conflicts](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Some%20Practices%20to%20avoid%20Conflicts)
>
📌 [Pull Request](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Pull%20Request)
>
📌 [Advanced Git Commands](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Advanced%20Git%20Commands)
>
📌 [Thank you](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-3-1i09#Thank%20you)

*<br>*

## Important Note

Hello fellow readers, this is a going to be second part of the series Git and GitHub Tutorial: Beginner to Advanced. Are you a new reader?

Here's the link to previous parts of the series -

👉 [Part 1](https://dev.to/suchintan/git-and-github-tutorial-beginner-to-advanced-part-1-32m6)

Ok now let's move forward with the topic without wasting anymore moments.


![Let's start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us6akoip7dtlm3875haq.png)

*<br>*

## Staging 📁<a name="Staging" />

Hmm... I know many of you can't wait to move forward to the coding part ⭐. I would like to take 2 more minutes from you before moving into that. Believe me this is important to be understood before moving forward 💁.

Now let's understand how files 📁are moved to local system 🖥 to GitHub. Don't worry we will discuss about each step broadly and in simplest way so that no-one can mock you in future interviews in this topic 😉.


![Image to demonstrate the flow] (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cz34p14oppv1uqpi7qjx.png)

Here's is the flow of files in Git-

1. **Untracked Files**: In this stage git is unaware 🤔 of any new files 📁. We use `git add .` command for telling git to track that files for the next commit.

2. **Unstaged Changes**: Now as git is aware of the files for the next commit , they are now in unstaged changes. Git is tracking your modifications on those files ✅. But they are not committed to git.

3. **Staged Changes**: Git now has all the files that are part of next commit, it also had tracked all the changes on those files. The user is now ready for pushing all these to the git. We use `git commit -m "commit message" ` to move the changes to staged by pushing the changes to git ✈.

Here many people will have one doubt in mind. Why is there this unstaged changes right? Is it just for naming a middle stage or it has any significance ❗

Answer is yes 🟢, it has and we will discuss this in merge and conflicts. We will discuss more on that topic.

*<br>*

## Beginner commands of Git 🤟<a name="Beginner commands of Git" />

*<br>*

### 🖇 Git Status
During day to day life we use this command very often to list all unstaged files, staged files and commits ahead of origin.
```
git status
```
**Output**:
>
On branch feat/test
Your branch is ahead of 'origin/test' by 1 commit.
  (use "git push" to publish your local commits)
>
nothing to commit, working tree clean

### 🖇 Create Branch
You want to create a new branch while working over a team. Here's the command for the same.
```
git branch <name_of_branch>
```
**Output**:
>
No Output

### 🖇 Delete Branch
Oops! You created a branch mistakenly. Let's delete it with-
```
git branch -d <name_of_branch>
```
**Output**:
>
Deleted branch name_of_branch (was 8040fb0).

### 🖇 List Branches
Want to list all the branches? Here's the command-
```
git branch
```
**Output**:
>
  main
  *testbranch
  testbranch1

* denotes the current branch. And lists all the local branches.
or,
```
git branch -a
```
**Output**:
>
  main
  *testbranch
  testbranch1
  remotes/origin/main

it denotes all the local and remote branches.

**Note**: I know some of you are worried what is the difference between remote and local branches right?

Here's the difference, remote branches are published branch that means it is created locally and also published on GitHub that means now others can see it. But local branches are not published so not visible to others. 

### 🖇 Branch Checkout
Want to switch to another branch ? Here's the command for it-
```
git checkout <name_of_branch>
```
**Output**:
>
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

### 🖇 Staging changes
Want to move files from unstaged to staged . Use this -
```
git add .
```
**Output**:

![Image of Staged changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmcuifx52r39mvuaqkd7.png)

Image from Visual Studio Code editor.

### 🖇 Unstaging changes
Mistakenly staged a change or file. Want to redo it? 
```
git reset <file_path>
```
**Output**:

![Image of Unstaged changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3femcgomlx2dl07ap6so.png)

### 🖇 Commit changes
Done with staging changes. Want to commit it to git?
```
git commit -m "commit message"
```
**Output**:
>
[testbranch1 e3fc475] test commit
 1 file changed, 1 insertion(+), 1 deletion(-)

### 🖇 Push Changes
Ok now you want to send your local commits with the GitHub. So that everyone can access it? 
```
git push
```
**Output**:
>
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 351 bytes | 87.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/Shaan3110/andriew.git
   e3fc475..3f61702  testbranch1 -> testbranch1

### 🖇 Pull Changes
Your peers made some changes on the branch? And you don't have it on your system. Let's pull it.
```
git pull
```

**Output**:
>
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From ssh://my.remote.host.com/~/git/myproject
 * branch            master     -> FETCH_HEAD
Updating 9d447d2..f74fb21
Fast forward
 app/controllers/myproject_controller.rb |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

### 🖇 Merge Branches

![Demonstration of merging of branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilm3psjaih9gw6aemymy.png)
Now you want to merge changes of two branches ? In case of a feature or bugfix is done and now you want to merge it with main branch. Here's the command-
```
git merge <branch_name>
```

**Output**:
>
Updating 8040fa0..3f61702
Fast-forward
 src/App.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

### 🖇 Stash changes
You are working over a branch but don't want to commit those changes. The best approach would be to use stash which will delete all local changes of that branch and you can move to other branch and continue with your work.
```
git stash
```

**Output**:
>
Saved working directory and index state WIP on main: 3f61702 test commit 2

The output means that it moved to commit 2 (the latest commit) and all the local changes are removed. ( The ones which are not commited)

### 🖇 List of Stashed changes
Oops ! You had some important local changes that you had stashed and now you want it back? Can't remember it though when you stashed it. Let's list all the stashed changes -
```
git stash list
```

**Output**:
>
stash@{0}: WIP on main: 3f61702 test commit 2

### 🖇 Retrieve stash changes
You know when you stashed a change and want it back now? Let's use this command -
```
git stash pop
```

**Output**:
>
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)
>
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/App.js
>
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9b40b4498725d20bd9f82300769547e8e3e03026)


### Thank you

You have made it till the end of this part. This is a series, so there will be more parts coming soon...

If you want to get a notification 🔔 when it would be published , don't forget to tap on the follow button ☝.

And at last I want to say 👇

Keep coding #️⃣ , keep rocking 🚀