This section provides answers to commonly asked questions about Git.
Change the most recent unpushed commit
-
Use the
git commit --amend
command. -
Alternatively, you can directly set the new message with:
git commit --amend -m "Your new commit message"
Setup multiple git identities
Git config with multiple identities and multiple repositories
Git merge
git merge
By default, Git will try to perform a “fast-forward” merge if possible. A fast-forward merge simply moves the source branch reference forward to the target branch’s latest commit, which maintains a linear history.
git merge --no-ff
This option --no-ff
ensures that a merge commit is always created, even if the merge could be performed with a fast-forward. This helps keep the project’s history more explicit and can make later code reviews or debugging easier.
Git rebase
When we use git rebase
and we are already on the branch which we want to rebase, we can skip a second argument. We can do:
and the result will be
Danger
Rebase changes commit hashes, D’ and E’ are new commits with different hashes from D and E.
Remove changes
git reset --hard
removes staged and working directory changes.git clean -fd
removes untracked directories in addition to untracked files.git clean -nfd
does a dry run and tells you what would be deleted before actually deleting it.
Delete branches
When you delete a local Git branch, it does not automatically delete the corresponding remote branch. The local and remote branches are managed separately, and deleting one does not affect the other.
Deleting local branches
Deleting remote branches
Warning
Before deleting a remote branch, ensure that other team members are not using it and approved.
Update Git submodule to latest commit
Updating submodule in the parent repository:
Alternatively, navigating into the submodule directory and pull the changes directly:
Rename a branch
Git stash
By default, running git stash will stash:
- changes that have been added to your index (staged changes)
- changes made to files that are currently tracked by Git (unstaged changes)
But it will not stash:
- new files in your working copy that have not yet been staged
- files that have been ignored
Adding the -u
option tells git stash
to also stash your untracked files:
Note
The stash is local to your Git repository; stashes are not transferred to the server when you push. It’s recommended to use descriptive messages when creating stashes to easily identify them later.
Delete “dangling” commits
While the commits after the reset point are no longer part of the branch’s linear history, they still exist in the Git repository. They become “dangling” commits, which means they are not directly referenced by any branch.
Tip
If the commits are still reachable due to tags, branches, or other references, you need to delete those references first.
For remote repositories, you may need to force-push the changes to ensure the remote history is updated:
Create a tag
To merge your release branch into the main branch and create a tag, follow these steps:
Undo a git rebase
The short answer
First, use git reflog
to find the commit where your branch was before the rebase started.
This command will show a list of recent actions in your repository. Look for the entry that indicates the start of the rebase.
In this example, HEAD@{3}
represents the start of the rebase, and HEAD@{4}
is the commit before the rebase started.
Once you have identified the correct commit, reset your branch to that state:
Use the ORIG_HEAD
pointer
Alternatively, when performing a rebase, git saves your starting point before the rebase as ORIG_HEAD
.
This means that you can also use the following command:
Note
It must be noted however that the
git reset
,git rebase
, andgit merge
commands all save the original HEAD pointer intoORIG_HEAD
. This means that if you have used any of these commands following the rebase, you will instead have to use thegit reflog
to identify how to reset the branch.
Recover a deleted branch
First, use git reflog
to find the commit of the last commit on the deleted branch. The reflog output will look something like this:
Then use the commit hash to create a new branch:
Tip
Using pipelines to filter the output. For example in PowerShell:
git reflog | Where-Object { $_ -match "launch\.json" }
.
Git diff
The git diff
command does not show untracked files by default. This is because untracked files are not part of the version control system until they are added to the staging area.
Make it a habit to run git diff
before committing changes.
-
git diff > diff_output.txt
to save the diff output. -
git diff <commit1> <commit2>
to see the differences between two specific commits. -
git show
-
git log -p
delta: A syntax-highlighting pager for git, diff, and grep output