Learn Git in a Practical Way


Git is a free and open-source version control system created by Linus Torvalds. Git is powerful enough to track all changes that went to the project and when combined with the provider such as Github, Bitbucket, etc anyone in the world can collaborate and contribute to the project. It is more popular and used by most of the industry. We will learn git in a practical way, by taking an example where you work as a developer and you help your new team member John who is not familiar with Git by helping in providing knowledge of Git. In the end, we will see common questions and answers related to Git.

John: I have created a simple HTML project and wanted to move it to GitHub he already created a remote repository in GitHub so what are the steps for it?

You: The first step is to convert your normal folder/directory to a git repository so initializing our folder as a git repository

cwm@cwm$ git init
Initialized empty Git repository in /home/cwm/Documents/Blogs/git-github-guide/.Git

Next, need to add the files you need to move to GitHub

cwm@cwm$ git add index.html styles.css

John: What if I want to move all the files?

You: Instead of files you can mention “.” after git add

cwm@cwm$ git add .

Next, commit the changes with the commit message using the git commit

git commit -m “added index.html styles.css”

(John already created a new empty repository in Github. Using the remote URL you can add the remote for git)

git remote add <remote-name> <remote-url>
git remote add origin https://github.com/CodeWithMarish/awesome-project.git

Now push your code to a remote repository Github using git push the remote name and branch

git push <remote-name> <branch>
git push origin main

John: I also need to work on another code which is created by Tom he provided the remote URL and I want to contribute my changes to this repository without changing the main branch.

You: For this first, you need to clone the repository and use the git branch feature to separate the changes from the main branch. So we use checkout to create and switch to the new branch if you already have a branch then we can use git switch.

git clone https://github.com/CodeWithMarish/awesome-project.git
git checkout -b new-feature
Make changes
git add .
git commit -m “New Feature”
git push origin feature-001

Later Tom can review the changes and merge the code to the main branch

git diff main new-feature
git merge new-feature

John: Tom made some changes and pushed them to GitHub but how do I keep my local repository updated with the latest code?

You: We can use git pull to download all changes from the remote repository and update it in our local repository.

git pull origin main

John: I created and pushed a new branch named test123 yesterday but I want the code to merge into the dev branch

You: First need to switch to the dev branch and then git merge test123 if any commits are not made ahead of the changes done for test123 it will merge otherwise you will see a conflict. If you see a conflict, you can decide which changes to keep and again commit and push.

git switch dev
git merge test123

John: I don’t want this branch test123 anymore in my local and remote repository how can I delete it?

You: We can use the branch -d git command to delete locally and push -d to delete remote branch

git branch -d <branch-name>
git branch -d test123

git push origin -d <branchname>
git push origin -d test123

John: How can I ignore some files/folders which is not required to be staged?

You: You can create a .gitignore file and mention files or folders you want to ignore for staging.


Let’s check out some common questions on git below

1. Where to download Git?

Ans. You can download it from the git website below.

2. How to initialize a folder/directory as a Git repository?

Ans. git init — It is the first command to run before running other git commands. It will initialize an empty git project after that we can make and commit our changes.

3. How to move the code from my local git repository to a remote repository like GitHub?

A. Pushing the code from our local repository to GitHub is a multi-step process.
a. Stage your local changes (git add)
b. Commit the staged changes (git commit)
c. Add the remote for the repository (git remote)
d. Push the code (git push)

4. How to stage the changes using git?

A. git add file1, file2, ….

5. How to unstaged the changes which were staged?

A. To un-stage we can use the git restore or rm command.
git restore — staged index.html
git rm — cached index.html

6. How to commit the changes?

A. Once your code is staged, we can commit those changes for pushing
git commit -m “Your message”

7. How to add/remove remote in git?

A. Using git remote we can provide the link to our remote repository like Github where we can push our local code.

git remote add <remote name> <remote url>
git remote remove <remote name>

8. How to rename the remote?

A. git remote rename <old remote name> <new remote name>

9. How to update the remote URL for an existing remote?

A. git remote set-url <remote name> <remote url>

10. How to list all the available remotes?

A. git remote -v

11. What is a branch in git?

A. Branches are the snapshot of your changes. It would have changed containing fixes for a bug, a feature, etc.

12. What is a default branch?

A. main is a default branch and it is mostly considered a production version. It is not recommended to commit directly on main branch if it is your production branch

13. How to create a new branch?

A. We can create a new branch by using the git branch and specifying the branch name

git branch dev

14. How to switch between the git branches?

A. Using the git switch command we can switch between branches. Also we can use git checkout when we want to create a new branch and switch to it.

git switch dev
git checkout -b stage

15. How to rename a git branch?

A. You can rename your current branch, so you need to switch to the branch you need to rename and then use the -m option of the git branch

git branch -m <old branch name> <new branch name>

16. How to push the code from the local repository to the remote repository?

A. Using the git push command, we can mention the remote name and branch name where you want to push

git push origin main

17. What is the use of the git status command?

A. It is used to check the current status of the local repository, it will show which branch we are in and files that have been staged but not committed, and a list of files that are changed but not staged.

git status

18. How to download all the updates on the remote repository?

A. We can use the git fetch command to only download the changes made in the remote repository and not to update it with the local repository.

19. How to show the list of commits made?

A. We have a git log command to show the list of commits made for the git repository.

git log

To get the commits summary in one line we can use — oneline option

git log — online

20. How to add a release tag to your changes?

A. We have a git tag command to create a tag and use push — tags to push to tag to a remote repository

git tag tagname
git push origin — tags

21. What is the use of git config?

A. It is the configuration command for git using which we can list and update configurations. Generally, there are two configurations one is local-specific for a repository and another is global

To list
git config — list
Above command will list configuration in a key-value pair your, to update any you can use that key and follow below syntax

git config <key> <name>
Eg: git config user.name cwm

Thanks for reading this post, if you found this post helpful please share maximum. Will be back again with amazing content. Stay tuned.

Also please don’t forget to subscribe to our youtube channel codewithmarish for all web development-related challenges.

Code With Marish | Youtube

Related Posts