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.
.env
node_modules/
Let’s check out some common questions on git below
Ans. You can download it from the git website below.
https://git-scm.com/downloads
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.
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)
A. git add file1, file2, ….
A. To un-stage we can use the git restore or rm command.
git restore — staged index.html
git rm — cached index.html
A. Once your code is staged, we can commit those changes for pushing
git commit -m “Your message”
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>
A. git remote rename <old remote name> <new remote name>
A. git remote set-url <remote name> <remote url>
A. git remote -v
A. Branches are the snapshot of your changes. It would have changed containing fixes for a bug, a feature, etc.
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
A. We can create a new branch by using the git branch and specifying the branch name
git branch dev
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
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>
A. Using the git push command, we can mention the remote name and branch name where you want to push
git push origin main
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
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.
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
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
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.