Advanced concepts on Git & GitHub.
This article provides an in-depth knowledge about Git & GitHub, starting from Branches to Merge Conflicts and much more.
Table of contents
Before starting with the advanced concepts let's have a small recap what we learned about Git & GitHub,
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed & efficiency.
GitHub is an online-platform that helps to host your project repository.
Few necessary Terminal Commands to know:
- dir => Shows all directory present in the path.
- cd => Change directory.
- mkdir => Make directory new.
Steps to Setup Git in your desktop:
- Download Git Click here and install it.
- Step 1 =>
git config --global user.name "[your name]"
Use this command to input your name. - Step 2 =>
git config --global user.email "[your valid email]"
Use this command to input your email. - Step 3 =>
git config --global color.ui auto
Use this command to include all the various color configurations available with git commands.
Some Useful Git Commands
git status
To know/show what changes are made/not made.git add names.txt
To add/stage a particular change, eg:- names.text file.git add .
To add/stage all the changes made.git commit -m "initial commit"
To give a name to a commit change.git remote add origin [link]
To add link for the repo to your local folder.git remote -v
To show the url attached to the project.git remote set-url origin [new-repo-link]
To remove old link and add new repo link.git push -u origin [deploy-branch]
To push local changes to remote repo.vi names.txt
To add/edit the file.cat names.txt
Display whatever is present in the file.git restore --staged names.txt
You got something staged and now you don't want it to be committed.git log
To check all the history for changes made.git rm [file]
To delete the file from project and stage the removal for commit.git reset [commit hashId]
Remove all the commits above that particular hashId of the commit.git stash
To keep changes in backstage without loosing it and putting it in log.git stash pop
Changes in the backstage are called in the unstaged area.git stash clear
Deletes all the changes in the staging area.
Stashing Changes:
It's like making a new feature for a project but you don't want to commit it now and want to keep it somewhere but not in the log/history, say backstage.
Branches:
In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, no matter how big or how small, you spawn a new branch to encapsulate your changes.
Uses of branches:
While creating a new feature or resolving a bug it is highly recommended to be done on a new branch.
git branch [name]
To create a new branch.git checkout [branch-name]
New commits made will be added on this branch.git merge [branch-name]
To merge new branch to main branch.
Working with existing Projects:
- Step 1 => Fork a project belonging to other organization.
- Step 2 => Now clone it to work on it.
git clone [forked repo link]
- Step 3 => Now add the upstream link.
git remote add upstream [original-repo-link]
Upstream Url:
The url of the project (original) from where it is forked.
Pull Request:
Pull requests let you tell others about changes you've pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch.
Let's make few commits first.
- Step 1 =>
vi Readme.md
Insert anything inside it. - Step 2 =>
git status
It shows modified changes.
Never make changes on the main branch
- Step 3 =>
git branch dev
Create new branch with the name "dev". - Step 4 =>
git checkout dev
Start working on "dev" branch.
After making all the necessary changes.
- Step 5 =>
git commit -m "[message regarding changes made]"
- Step 6 =>
git log
Check if the commit is on the "dev" branch or not.
Then upload the changes to your forked project/ repo on GitHub
- Step 7 => Now create a pull request,
git push upstream dev
If this command does not execute, it means that you don't have access to the upstream repo. So, you have to push it to the forked repo,git push origin dev
.
Now automatically you will get a Compare and Pull Request button on the repo right corner top. Click on the button.
Write comments, and then start the PR and have discussions.
Removing a commit from the PR by force pushing it.
- Step 1 =>
git log
Copy the hash-code of the below commit that you want to remove. - Step 2 =>
git reset [hash-code]
- Step 3 =>
git add .
Select the file. - Step 4 =>
git stash
Send the unwanted commit or file to stash.
Now after deleting, we have to force push it.
- Step 5 =>
git push origin dev -f
Merging a Pull Request:
This function can be done once a pull request is submitted and the owner of the upstream url/original repo accepts the PR.
Now the forked repo is not updated.
Just like a fork repo cannot change upstream repo, it is also vice-versa.
After the PR gets merged into upstream URL.
You will get a button below code, i.e.; Fetch Upstream.
Making forked project even/equal/same with main project.Fetch upstream
Click on the button, it fetches automatically.
Fetching manually :
- Step 1 =>
git pull upstream main
- Step 2 =>
git push -u origin main
Now you have even commits.
Squashing Commits:
Merging many commits into one.
- Step 1 => Create new branch from head, to be sure to keep main/master branch updated.
git checkout temp
- Step 2 =>
git add .: git commit -m "1"
Touch 1. - Step 3 =>
git add .: git commit -m "2"
Touch 2. - Step 4 =>
git add .: git commit -m "3"
Touch 3. - Step 5 =>
git add .: git commit -m "4"
Touch 4. - Step 6 =>
git log
Contains all the 4 commits. Copy the commit hash below all the commits. - Step 7 =>
git rebase -i [hash]
All commits above it can be picked/squashed. Change the pick to s that you desire to put into single commit.
Now exit out of it pressing Esc and type,:x
Type enter and input new message.
Commits mergedgit push origin temp
Add the commit.
ORgit reset --hard [hash-of-commit]
Remove the merge conflict.
How to resolve Merge Conflicts:
When changes are made on same line by different developers, git gets confused to take which change.
- Step 1 => Make the same changes in the origin repo that is done in upstream repo.
- Step 2 => After making changes and uploading to forked repo. Now the organization sees a merge conflict.
Now they do over-writing, its upon the organization, they take which merge.
Conclusion
If you have finished reading this, you might be feeling overwhelmed about Git and GitHub. Yes it's another big thing you need to learn in tech, but do not feel afraid.
Remember that whenever you start learning something new, at first it can seem like you won't get the hang of it. But after some time and hard work, you'll become more comfortable.
It's the same with Git and GitHub too – if you use it a lot for a while, you will get comfortable with it. Thanks for reading this article. If you enjoyed it, consider sharing it to help other developers.
Happy Learning.