Git & GitHub Step-by-Step: Part 2
Can we un-stage a file which is already in staging stage?
Yes, we can do so.
Delete the folder of mypanda. Create the git repo for git tuts. Using: git init
Check status of the repo using:
git status
Then use git add . to add files in repo.
Then use git commit -m “Initial commit” to commit.
Check status of the new repo using:
git status
Now make some changes in the first_renamed.txt file. And save it, then again check status of the git tuts repo.
Now, to add first_renamed.txt to staging area we will use:
git add first_renamed.txt
Now, what to do to un stage that file?
git restore — -staged first_renamed.txt
Suppose by mistake we have modified something which is not very required. So we want that file to go again in that previous commit state. Can we do so?
Yes, we can do so.
First make the change in the file first_renamed.txt, then check git status.
Now, to go into the previous commit state, we will use following command:
git checkout — first_renamed.txt
Also, to undo the changes for many files, or say completely getting previous commit stage of the folder (git tuts), we can use the following command: (Before it make changes to any number of files present in that folder)
git checkout -f
Working with remote repositories:
Creating a repository online:
Go to the website www.github.com then click on the “+” icon and then choose “New Repository” , write name of the repository as “GiTutorialdemo” and write some description about it. Choose whether to make it available public or privately. Then click on create.
New window appear as:
We know,
Push: it means to throw the code in the repo
Pull: it means to fetch the code form the repo
So, now in last ss we can see some instructions as:
So, we will use the first row in case we don’t have any existing repo in our PC. But, since we already have the repo, we will use the commands shown in 2nd row.
To start adding the git repo (created online) we will us the command, where origin is used to indicate the name of the git repo.
git remote add origin https://github.com/Rememberful/GitTutorialdemo.git
Now, to check it we will use the command:
git remote
To get the link of the link to fetch and push we will use the command:
git remote -v
Now, how to push the files in this repo?
To do so we need to get the authentication key, or say SSH key. For that go to he settings, then create on “SSH key” and write name of it.
Since, we don’t have any SSH key, we will generate one. Copy & paste the command in git bash:
ssh-keygen -t rsa -b 4096 -C adii.utsav@gmail.com
The above code is copied from the website:
Then,
Now, copy the key using:
Clip < ~/.ssh/id_ed25519.pub
Copy and paste the key to git hub account:
Now, to push the files in our repo we will use the following command:
git push -u origin main
For case (master) is written beside the file path of the repo, replace ‘main’ with ‘master’.
This will push all the files which we have asked to be committed.
Now, suppose we created one more file named forgithub.txt and wrote something in it, then we can also push it to the online git hub, using the command as follows: (before it create the text file)
Commands:
git status
git add .
git commit -m “Added forgithub file to github”
git push -u origin main
This will push that new text file created by us to the repo online:
Setting Alias in Git:
Alias means to use the user specified commands to a certain task, such as we can use git st instead of git status for the same work if we manage to change the meaning of git st.
Let’s first run git st without doing anything, it will give an error.
So, how to make it work?
Use the command:
git config –global alias.st status
Then check if it works or not:
git st
Similarly, we can change the commit command to ci as shown below:
Can we write something like this to un stage the file? Yes, we can.
git config –global alias.unstage ‘restore — -staged — –’
Similarly, we can make an alias to see the what someone committed in last commit:
git config –global alias.last ‘log -p 1’
Can we merge work of two people in git?
Yes, we can.
A and B created a website, and later, C joined the team and was asked to make changes to the website. A and B suggested that C create a branch to continue his work, which would prevent the website from being disrupted and allow them to compare C’s work with the existing work done by A and B. Git enables C’s work to be merged with the work completed before C joined, along with the work that A and B have continued since then.
So, what’s the master branch?
The master branch is the most important branch, allowing the creation of different branches without disturbing the main branch.
Let’s see example:
Suppose in our current repo, git tuts, we want to make some changes, but we wish that it shouldn’t disturb the main/master branch. Then what we can do?
To develop a new branch and switch to it:
git checkout -b develop
Now, suppose we made some changes where we renamed `first_renamed.txt` to `renamed.txt`, then checked the git status. After that, we used `git add .` followed by `git status` to check the staged state, and then committed the changes.
Now, commands:
Now, suppose due to some reason we want the repo to be in exact same state as it was earlier. For that we will use the command:
git checkout main
It will automatically bring us to the main state of the repo. Where the first_renamed.txt is not renamed.txt.
How to see the branch name of the repo?
git branch
Branches are not committed by default to the git hub.
Working on a project using the git, git hub, VS code:
Install VS code and create a folder named Mywebsite inside the git repo, git tuts, since remove the git tuts as git repo using the command rm-rf .git
Now, open VS Code and create a file named `index.html` in the MyWebsite folder. Install the extension called Live Server, then save the file and run it using the “Go Live” button.
Next, manually open the MyWebsite folder, right-click while holding the Shift key, and select the option for Git Bash. Check the status; since it’s opened for the first time, it will show that it’s not a Git repository. To make it a repository, use the following command to initialize the repo and commit the files inside it.
Now, suppose we want to create a branch named ‘tryCleanup’ to make some changes in it. Command:
git checkout -b tryCleanup
Now, suppose we made some changes in the index.html, then we will save it and go to the live server again. But, suppose now we want to undo those changes.
Then? Can we undo it?
git checkout main
We will observe that the changes we made in the index.html are no longer there, we are back again to the last commit or say main branch.
So, can we again go to the tryCleanup branch? (Since we had exited from there to the main branch recently)
Yes, we can do so. Using the command:
Get Aditya Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
git checkout tryCleanup
We will again back in the tryCleanup branch and we will find that the change we had done in that branch are still present there.
Don’t forget to commit before you change the branch!!!!!!!!
Now, suppose we made some changes in the tryCleanup branch, now we want to skip the staging area and merge the tryCleanup branch to the main branch.
So, we made changes, skipped the staging area, and switched to the `main` branch. Now, to merge the `main` branch with the `tryCleanup` branch, use the following command:
git merge tryCleanup
It’s possible that the changes made in `tryCleanup` may conflict with the code in the `main` branch, but VS Code allows us to choose between the original version and the incoming changes. However, in our case, we didn’t encounter any conflicts.
Now, check the git status:
If there is something at the unmerged path, use the following commands to stage and commit the changes:
git add .
git commit
However, since the `index.html` file in both branches doesn’t conflict, we haven’t encountered anything in the unmerged path.
What is the merge conflict?
A merge conflict in Git occurs when Git is unable to automatically merge changes between two branches. This usually happens when two branches have made changes to the same part of a file or when one branch has made changes to a file that the other branch has deleted.
When a merge conflict arises, Git marks the conflicting parts in the file and pauses the merge process until you manually resolve the conflict. You need to decide which changes to keep (or create a combination of both) before completing the merge.
For example, if we edit a file in different branches and attempt to merge them, and if the same portion of the file has been edited in both branches, Git will notify us of a conflict and will not allow the merge. At this point, we will have the option to choose how to resolve the conflicting changes. These changes are marked by conflict resolution markers, such as `<<<<<`.
How to manage merge conflict? We will see later!
What is branch management?
Branch management in Git refers to the practice of organizing, creating, maintaining, and managing different branches in a repository to streamline development processes, collaboration, and version control. Proper branch management helps developers work on multiple features, bug fixes, or experiments simultaneously without disrupting the main codebase. This ensures that changes can be isolated, reviewed, tested, and merged in an organized way.
Git allows us to create as many branches as we want without taking up much space because it uses pointers to keep track of the changes. Instead of duplicating files, Git stores changes incrementally and uses these lightweight pointers to reference commits, making branching highly efficient.
Let’s see the example: First let’s create git repo in the Git tuts folder and commit as:
Now, in VS code, under git tuts folder, make a file name index.html and save it.
Then check status:
Now track it and commit:
Now, suppose we made some changes in index.html as:
Then again checked the status:
Now, add it and commit:
Similarly, we edited the index.html for 2nd time, and added as well as committed the changes:
Now, let’s create a branch named ‘issue1’ and make some changes in the index.html and commit it without keeping it in the staging process as:
Similarly, done for c5, c7 with some changes in index.html for c5 and c7.
Now, switch to ‘main’ branch:
Made changes in index.html and committed:
Now, we try to merge the issue1 branch and main branch, merge conflict will be observed:
In VS code this issue will be pointed as:
We have few options; we can select what we want or manually delete the unwanted things. Suppose we selected the ‘Accept Incoming change’ option.
Now, when we use see the status of the repo we will see that it is written ‘unmerged path’:
To manage it we will add the index.html as:
git add index.html
Now, let’s merge the issue1 with main:
We can see and verify it as well using:
git log
How to work with the branch management?
To see the name of the existing branches:
git branch
Suppose we want to create one more branch:
Then made some changes in the index.html, and then added and committed:
Now, if we want to go back to the main branch, we can do as:
git checkout main
To get the has value of the commit as well as the comment of the commit:
git branch -v
To see the merged branches:
git branch –-merged
To see the non-merged branches:
git branch –-no-merged
To delete the branch:
git branch -d issue2
But, since issue2 is not merged anywhere, it will give a warning/hint to be sure for it.
Since, issue1 is merged so we can delete it:
Branching workflow:
Primarily there be two types of branches in a project:
1. Long running branches: Exists till project exists
2. Topic branches: Works until the topic get completed
Pushing Git Branches To Remote Repositories:
Let’s first create a new branch in git tuts, as:
Now, delete a file from the folder, say we deleted forgithub.txt. Then check status, followed by adding and then commit.
Now, change the branch to main:
Now, check remote using:
git remote
Since, it didn’t shown any output it means that, there is no remote over there, now open github.com and create one new repo.
Now, copy the link which appears as:
Now, check name of the remote using:
git remote
Then,
Now, to push use the command:
git push -u origin main
In the demorepo at github.com we can find:
Now, check status:
Now, in the remote repo, we can see that the branches are not available as sections, so we will separately push them. Now, to push the ‘bugfix’ branch we will first get inside it, using:
git checkout bugfix
Now, to push bugfix as a branch in the remote repo, we will use the command:
git push origin bugfix
See, on the remote rep, we will be able to see that there are now two branches:
In order to push the bugfix as mybugfix we will use:
git push origin bugfix:mybugfix
Now, to delete the bugfix from the remote repo we can use:
git push -d origin bugfix
Contact Me:
📧 Email: adii.utsav@gmail.com
🔗 LinkedIn: https://www.linkedin.com/in/aditya-kumar-3241b6286/
💻 GitHub: https://github.com/Rememberful








