Showing posts with label recent. Show all posts
Showing posts with label recent. Show all posts

14 December 2025

On Ubuntu, how to run one of the commands I ran before again?

To rerun a command from your history, use ! followed by its number (e.g., !123), or use !! for the very last command, !-2 for the second-to-last, or press Ctrl+r and type part of the command to search for it, then hit Enter. 
By History Number
  1. View History: Type history and press Enter to see a numbered list.
  2. Re-run: Type ! then the number of the command (e.g., !567 to run command #567). 
bash
# Example:
history
# ... (list of commands with numbers) ...
!123 # Runs the 123rd command
Shortcuts
  • !!: Runs the immediately preceding command.
  • !-2: Runs the second-to-last command.
  • !string: Runs the most recent command starting with string (e.g., !ls runs the last ls command). 
Reverse Search (Ctrl+r) 
  • Press Ctrl+r to enter interactive search mode.
  • Type part of the command you're looking for (e.g., ssh).
  • Press Ctrl+r again to cycle through older matches.
  • Press Enter to run the command, or Esc to exit search without running. 
  • If you have cycled through some older matches, now you want to cycle
    through some new 
    matches, you can add the following line to your  ~/.bashrc file:
stty -ixon
Starting now, you can press   Ctrl+s to cycle through new matches.


In Vim, how to run one of the commands I ran before again?

    To re-run a command from your Vim history, you can use the following methods:
    1. Using the Command-line Window (q:)
    The most common and interactive way is to open the command-line window: 
    1. Press q:. This opens a special window in your Vim buffer which lists all previously executed commands, similar to a regular file buffer.
    2. Use the standard Vim movement keys (jkG, etc.) to navigate to the command you wish to re-run.
    3. Press Enter while your cursor is on the line of the desired command. Vim will execute that command and close the history window.
    4. To exit the window without running a command, use :q or Ctrl-C. 
    2. Using Command-line History Navigation
    You can cycle through your command history directly from the command prompt: 
    1. Type : to start a new command line.
    2. Use the Up Arrow (<Up>) or Ctrl-P to go back through the history of previous commands.
    3. Use the Down Arrow (<Down>) or Ctrl-N to go forward through the history.
    4. When the desired command appears on the command line, press Enter to execute it. 
    3. Using History Search (q/ or q?) 
    If you have a long history and want to find a specific command quickly, you can search for it:
    1. Press q/ (for forward search) or q? (for backward search) to open the search history window.
    2. Type your search pattern (e.g., s/) and press Enter. The window will list commands matching that pattern.
    3. Navigate to the command you want to run and press Enter to execute it. 
    4. Running a Specific History Index 
    You can also run a specific command by its index number if you know it from viewing :history. This is less common but useful if you need an exact, old command: 
    1. Type :@: followed by the number of the history entry you want to execute (e.g., :@:25 to run command number 25).

01 January 2022

How to amend older commit messages in git?

How to amend older commit messages in git?


https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History


Changing Multiple Commit Messages

To modify a commit that is farther back in your history, you must move to more complex tools. Git doesn’t have a modify-history tool, but you can use the rebase tool to rebase a series of commits onto the HEAD that they were originally based on instead of moving them to another one. With the interactive rebase tool, you can then stop after each commit you want to modify and change the message, add files, or do whatever you wish. You can run rebase interactively by adding the -i option to git rebase. You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto.

For example, if you want to change the last three commit messages, or any of the commit messages in that group, you supply as an argument to git rebase -i the parent of the last commit you want to edit, which is HEAD~2^ or HEAD~3. It may be easier to remember the ~3 because you’re trying to edit the last three commits, but keep in mind that you’re actually designating four commits ago, the parent of the last commit you want to edit:

$ git rebase -i HEAD~3

Remember again that this is a rebasing command — every commit in the range HEAD~3..HEAD with a changed message and all of its descendants will be rewritten. Don’t include any commit you’ve already pushed to a central server — doing so will confuse other developers by providing an alternate version of the same change.

Running this command gives you a list of commits in your text editor that looks something like this:

pick f7f3f6d Change my name a bit
pick 310154e Update README formatting and add blame
pick a5f4a0d Add cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


It’s important to note that these commits are listed in the opposite order than you normally see them using the log command. If you run a log, you see something like this:

$ git log --pretty=format:"%h %s" HEAD~3..HEAD
a5f4a0d Add cat-file
310154e Update README formatting and add blame
f7f3f6d Change my name a bit

Notice the reverse order. The interactive rebase gives you a script that it’s going to run. It will start at the commit you specify on the command line (HEAD~3) and replay the changes introduced in each of these commits from top to bottom. It lists the oldest at the top, rather than the newest, because that’s the first one it will replay.

You need to edit the script so that it stops at the commit you want to edit. To do so, change the word “pick” to the word “edit” for each of the commits you want the script to stop after. For example, to modify only the third commit message, you change the file to look like this:

edit f7f3f6d Change my name a bit
pick 310154e Update README formatting and add blame
pick a5f4a0d Add cat-file


When you save and exit the editor, Git rewinds you back to the last commit in that list and drops you on the command line with the following message:

$ git rebase -i HEAD~3
Stopped at f7f3f6d... Change my name a bit
You can amend the commit now, with

       git commit --amend

Once you're satisfied with your changes, run

       git rebase --continue


These instructions tell you exactly what to do. Type:

$ git commit --amend

Change the commit message, and exit the editor. Then, run:

$ git rebase --continue

This command will apply the other two commits automatically, and then you’re done. If you change pick to edit on more lines, you can repeat these steps for each commit you change to edit. Each time, Git will stop, let you amend the commit, and continue when you’re finished.