DEV Community

Cover image for Some secret git tricks that come in handy
????
????

Posted on

Some secret git tricks that come in handy

here are some secret Git tricks

Amending the Last Commit

When you commit changes to a Git repository, you create a new snapshot of your code. Sometimes, you may realize that you forgot to include a file, made a typo in a commit message, or made other small changes that you'd like to include in the last commit. Git allows you to amend the last commit with the --amend flag:

# amend the last commit with a new message
git commit --amend -m "New message"

# amend the last commit without changing the message
git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Reflog

Git keeps track of all the changes you make to your repository, including commits, merges, and other operations. The reflog is a log of all the changes to the Git repository, including all the commits, branch changes, and other operations. You can use the reflog to recover lost commits, reset to a previous state, or undo a rebase.

To view the reflog, run:

# view the reflog
git reflog

# reset to a previous state using the reflog
git reset HEAD@{N}
Enter fullscreen mode Exit fullscreen mode

Interactive Rebase

Interactive rebase is a powerful tool that allows you to edit, reorder, or delete commits before merging them into the main branch. This is especially useful when you're working on a feature branch and want to clean up the commit history before merging it into the main branch.

To start an interactive rebase, run:

# start an interactive rebase
git rebase -i HEAD~N

# edit a commit message in the rebase
pick 1234567 Old message
reword 2345678 New message

# reorder commits in the rebase
pick 1234567 First commit
pick 2345678 Second commit
pick 3456789 Third commit

# delete a commit in the rebase
pick 1234567 First commit
drop 2345678 Second commit
pick 3456789 Third commit
Enter fullscreen mode Exit fullscreen mode

Git Aliases

Git aliases allow you to create custom shortcuts for Git commands. This can save you time and typing, especially for frequently used commands.

To create a Git alias, run:

# create a Git alias for a command
git config --global alias.ci commit

# use the alias to commit changes
git ci -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

Git Stash

Git stash allows you to temporarily save changes that you're not ready to commit yet, without creating a new branch or committing the changes.

To stash changes, run:

# stash changes
git stash

# apply the most recent stash
git stash apply

# apply a specific stash
git stash apply stash@{N}

# list all stashes
git stash list
Enter fullscreen mode Exit fullscreen mode

These are just a few of the many secret Git tricks that can help streamline your workflow, improve collaboration, and enhance security. With these tips and tricks, you'll be able to take your Git skills to the next level and become a more efficient and effective developer.

Oldest comments (0)