Today, we'll look into some essential Git commands that are required during a project. For a first time reader this may involve advanced concepts, but I have tried to simplify stuff as much as possible.
Let's get started...
We have two tasks lined up today :
Before the tasks, a little build-up.
Consider a real example of contributing to a project. I'll give you one where I wanted to contribute to a project - "neofetch". Check the actual repository here.
Neofetch is a command-line system information tool. I wanted to add one more feature to it, i.e. to also print the "Display Manager" i.e. sddm, gdm or lightdm, etc along with the other info.
Although I succeeded in doing so, one final step is remaining for it, i.e. the alignment. Rest all the work is done I believe (at the time of writing). It's been months the issue has not been responded though ๐.
The currnet status of the program is :
And this is the link to the issue.
Now, this could be a project in your workplace where you are supposed to contribute/work. Some questions...
How do I start working on an existing project?
Steps to get started:
Go to the URL of the project and fork.
Clone the repository that you have forked.
The forked repository is now visible on your GitHub/GitLab account.
Visit the forked repo URL and click on "Code" in green button.
Then copy the link either SSH or HTTPS as per your preference. If you haven't setup SSH, select HTTPS and copy the link.
Navigate to the directory you want to store your project in. (I created a "temp" directory in my home folder.
Enter the command :
git clone <the_copied_link.git>
Create a branch with a name suitable to the feature you want to add (for convenience).
Now you have a copy of the project on your local machine to work. Naviate to the project.
Check all the branches in the cloned repository with the command :
git branch -a
Here, you might get only one branch in white i.e.
origin/master
ororigin/main
. The second branchremotes/origin/display-manager
is because I have already worked on that branch earlier.To switch to the branch where I worked on, the following command
git checkout display-manager
If you want to work on a new feature altogether from the project on the main/master branch, do the following:
git checkout -b new_branch
In (1), I show that I have switched back to the master/main branch - the central working line of a project.
In (2), I create a branch with all the code that exists on the master branch and checked out from master to the new branch, i.e. after the command I will be on
new_branch
.In (3), I displayed using
git status
that I have switched to thenew_branch
successfully.
Make whatever changes you want to in the project on this branch and push the changes as follows.
Thus, the changes made to this branch will be pushed to the new_branch remote branch on your github/gitlab wherever you cloned it from.
Then there will reflect an option to send a "Pull Request" to the original repo where you forked it from.
Click on
Pull Request
if you want to get the changes merged in the original project.Then the owner of the project will do the next steps of checking and verifying the code, etc.
Congratulations, you have understood how to contribute to an open source project!
What is a commit?
To make a commit:
git commit -m "short description/message of the changes made for reference"
How do I check the commit history?
git log
Okay, now that you have understood the basic commands of using Git and GitHub/GitLab, you might someday want to undo a particular commit or go back to a specific version and just work around something, trust me this will come to you rightโ .
How to undo a commit?
There are several ways to undo git commit(s). The best practice for doing so is to use the git revert
command even if the changes have been pushed to the remote repository. The other popular approach to undo a commit is git reset
. There are slight differences and may be used appropriately to maintain certain coding standards/documentation purposes of the project.
git reset
soft reset
git reset --soft <commit_ID1>
The command resets the HEAD commit on a local branch to a previous commit.
The specified version is now the current commit and the HEAD version in the log.
The commits after the commit_ID1, i.e. the more recent commits are discarded.
Useful to undo accidental commits.
soft reset
is not ideal if the changes have been already pushed to the remote repository.
hard reset
git reset --hard <commit_ID2>
The command resets the HEAD commit on a local branch to a previous commit just like it does for soft reset.
The difference is that any changes made to file(s) after the commit_ID2 are lost. ๐
Use when certain changes are NOT AT ALL required in the future or is to be discarded totally!
This approach is NOT recommended when changes have been already pushed to a remote repository.
git revert
Used to completely revert a commit without deleting it i.e. it doesn't throw away or overwrite any earlier commits.
Point of difference between reset and revert is that,
git revert
undoes the changes with a new commit.Maintains history of all commits while adding a new entry to the commit log to document the reversion.
The
HEAD
of the project is set to this new version that inverted the changes.Useful and recommended when changes have been pushed to remote repository as it is the cleanest way to fix breaking changes.
Can I merge two branches?
Is the question valid? | Yes |
Answer to the question | Yes |
Let's say you are working on a branch featurebranch1
different from the main
branch. You now want to add your local featurebranch1
commits to the HEAD of the main
branch.
There are two ways you can merge the two branches together or attain the above :
git merge
This method creates a new commit that incorporates the changes from both branches.
git checkout main git pull git checkout featurebranch1 git merge main
Makes it difficult to review your code
git rebase
Allows integration of two branches together into one of the two branches.
Logs are modified once the action is completed.
It takes a series of commits from one branch and applies them on top of another base commit basically rewriting a branch's commit history.
Does not change the commit content of your commits, but it
does change the commit hash
used to track your changes.git checkout main git pull upstream main git checkout featurebranch1 git rebase main
- ๐ฅBad idea to use
git rebase
for a commit that somebody else might have based their work on! Only change your own local history - ideally don't rebase anything that you have already pushed to an upstream branch.
Tasks
Task1
The following image is to show branching and setting upstream the remote repository for dev branch.
-
You can see the commits in the following image:
A bit deflection while doing this task:
- Here, I got some sort of conflicts. It is solved in the image itself by aborting the reversion.
Coming back to the actual solution...
Task2
All the branches can be seen using the
git branch -a
command shown early up in the blog.-
Thus, changes have been made to the dev branch by adding the
new_file_for_rebase
file as shown above. -
Successful merging and push to main branch
-
Now, to try out the
git rebase
command, I have deleted the new_file_for_rebase from the dev branch and I want to merge the dev branch with the main branch usinggit rebase
. -
To display that main branch has been rebased(merged using rebase), check the image below:
The command used is
git rebase dev main
-
Thus, the merge using
git rebase
is successful!