DEV Community

Cover image for Git and Github Tutorial: Beginner to Advanced (Part 2)
SUCHINTAN DAS
SUCHINTAN DAS

Posted on • Edited on

Git and Github Tutorial: Beginner to Advanced (Part 2)

Table of Content


πŸ“Œ Introduction

πŸ“Œ Git Terminologies

πŸ“Œ Setup of Git and Github on System

πŸ“Œ Concept of Github and Git

πŸ“Œ Staging

πŸ“Œ Beginner commands of Git

πŸ“Œ Merging Conflicts

πŸ“Œ Resolving Conflicts

πŸ“Œ Some Practices to avoid Conflicts

πŸ“Œ Pull Request

πŸ“Œ Advanced Git Commands

πŸ“Œ Thank you


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

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

Let's start


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

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.


Beginner commands of Git 🀟


πŸ–‡ 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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Output:

No Output

πŸ–‡ Delete Branch

Oops! You created a branch mistakenly. Let's delete it with-

git branch -d <name_of_branch>
Enter fullscreen mode Exit fullscreen mode

Output:

Deleted branch name_of_branch (was 8040fb0).

πŸ–‡ List Branches

Want to list all the branches? Here's the command-

git branch
Enter fullscreen mode Exit fullscreen mode

Output:

main
*testbranch
testbranch1

  • denotes the current branch. And lists all the local branches. or,
git branch -a
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

Output:

Image of Staged changes

Image from Visual Studio Code editor.

πŸ–‡ Unstaging changes

Mistakenly staged a change or file. Want to redo it?

git reset <file_path>
Enter fullscreen mode Exit fullscreen mode

Output:

Image of Unstaged changes

πŸ–‡ Commit changes

Done with staging changes. Want to commit it to git?

git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 ..." to update what will be committed)
(use "git restore ..." 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 πŸš€

Top comments (0)