Table of Content
π Introduction
π Git Terminologies
π Setup of Git and Github on System
π Concept of Github and Git
π Staging
π Merging Conflicts
π Resolving Conflicts
π Some Practices to avoid Conflicts
π Pull Request
π 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.
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 π.
Here's is the flow of files in Git-
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.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.
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
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 from Visual Studio Code editor.
π Unstaging changes
Mistakenly staged a change or file. Want to redo it?
git reset <file_path>
Output:
π 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
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 ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: src/App.jsno 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)