Changing the commit messages that are already pushed

So in this post, we will be talking about the process of changing the commit messages that are already pushed into the remote. But, before talking about changing those messages we must know about the basics commands of GitHub. Please refer to this post, if you have less knowledge about the Git.

Also, it is good to know about the git reset command which is used to reset any changes made by a user in a repo. Use the command git reset –help for more information.

Now let’s dive right into the topic. First thing is to check the changes that you have done in the recent past. For that use the following command.

$ git log 

# This command will show one change in each line
# -5 represent the number of messages that you want to read.
$ git log --oneline -5

Now for changing the commit messages use the amend command. The command is written below.

But before that think that you don’t want to change the last commit message but you want to change a commit message that is somewhat old. Say it is shown at number 5 when you used git log. Use this command to change your head to that commit message.

$ git rebase -i HEAD~5

Next step was hard to figure out but I found it by some proper reading. Now this will open a panel for you where you can make changes for you commit messages. This panel will look something like this.

pick 2f8e279 another message
pick a04482b One more another message 2
pick 97d5145 no new changes available

# Rebase 3bb9b41..a1c796c onto 3bb9b41 (3 command(s))
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
...few other lines

Now if you look at the comments properly you will come to know about how to do this. So as you can see that in the first three lines commit messages are written. In the beginning of each line “pick” is written and in the comments, we can see the description. So, ¬†remove the word pick and change it to the word that you want to use for yourself. I removed pick in all the three lines and changes it with r.

I am not using “e” as it was not required in my case. So, if anyone out there uses it, share it in the comments.

After that, you will be asked to change the commit message one by one in the end.

Now say you only want to change a single commit message (last commit message) it is a bad choice to use something like rebase. You can use this command rather.

$ git commit --amend

This command will open the editor in the terminal. Make the changes and save it. Now is the time to push the changes to the remote. Use the following command. Keep in mind to include that + sign, otherwise, your changes will be ignored.

 $ git push origin +branch_name

Thanks for reading this post. Keep reading and Happy coding.

Basic commands in GitHub

Git is the open source distributed version control system that facilitates GitHub activities on your laptop or desktop. The
commonly used Git command line instructions are:-

Create Repositories
Start a new repository or obtain from an existing URL
$ git init [ project-name ]
Creates a new local repository with the specified name
$ git clone [url ]
Downloads a project and its entire version history

Make Changes
Review edits and craft a commit transaction
$ git status
Lists all new or modified files to be committed
$ git diff
Shows file differences not yet staged
$ git add [file ]
Snapshots the file in preparation for versioning
$ git commit -m “[descriptive message “]
Records file snapshots permanently in version history

Group Changes
Name a series of commits and combine completed efforts
$ git branch
Lists all local branches in the current repository
$ git branch [branch-name ]
Creates a new branch
$ git checkout [branch-name ]
Switches to the specified branch and updates the working directory
$ git branch -d [branch-name ]
Deletes the specified branch

Synchronize Changes
Register a repository bookmark and exchange version history
$ git fetch [bookmark ]
Downloads all history from the repository bookmark
$ git merge [bookmark /[branch]]
Combines bookmarks branch into current local branch
$ git push [alias [branch]]
Uploads all local branch commits to GitHub
$ git pull
Downloads bookmark history and incorporate changes

Some advanced commands

Now let’s talk about some of the advanced commands. So lately I have been working on many of the open source projects. Which gave me a lot of idea about the way the things happen in the open source industry. As much time you spend in this part, more will you learn about the things happening.

From a long time, I was working on a single user project where all the development is done by the single user. So your code remains intact and no damage is done to your commits. But the case is totally different when it comes to the big projects. There are constant commits that are happening in each second. So by the time you are ready to push the code to a repository for merging you are behind the main repository by at least three to four commits.

So the idea is to get the code from the main branch merge the commits into your code and then push the changes back into the main repository. Also in the case big repository you can’t give the excuses like that you are new to the GitHub. If you don’t evolve early, get ready to be ignored by the open-source industry.

So whenever you want to commit a change that you have done to the repository put the changes into the stash by using these commands.

$ git stash 
# read more about git stash by using the following command
$ git stash --help
# The following command will list the saved data in the stash
$ git stash --list

stash is the part where you can store the things in the temporary mode they can be brought back to their earlier position when other changes are committed. It is the time to fetch the data that had been changed the main repository.

But before fetching it is great to make a remote of the forked repository so that we can directly fetch the changes from the terminal else we have to type the link of the forked repository again and again. Obviously, it is not necessary but it will make our work easier.

$ git remote add upstream

The command tells us to add a new remote to the local repository by the name of upstream which is linked to the link provided as the next parameter. Now, whenever we want to know about the changes made in the particular branch we can simply type the name of the branch and we will get the code given by it. Then finally we will use the merge command to merge the code into the local repository. See the example code snippets for more information.

$ git fetch upstream/branch_name

$ git merge upstream

Rest of the information will be shared in the follow-ups.