Git – Config Username & Password – Store Credentials

To connect to a Git repository with authentication over HTTP(S), every time it needs to set a username and password.

You can configure Git to remember a username and password by storing them in a remote URL or by using Git credential helper.

In this article i am showing how to clone Git repository by setting a username and password on the command line, how to save a username and password in Git credentials storage and how to configure different usernames and passwords for different repositories on the same Git server. (more…)

Git – Create New Branch

Instead of committing directly in local master branch, a good developer creates a new branch each time he starts working on a new bug or feature.

To create a new branch there is a git branch command.

Below i will show the examples of how to create a new local branch in Git from another branch (e.g. current branch, master, develop, etc.), how to create a new branch from commit or tag and how to push a new branch to the remote Git repository (create remote branch). (more…)

Git – Squash Commits: Merge All Commits in Branch Into One

Each time you are working on a bug or a feature, you create a branch for it.

Usually, while working in such temporary branches, you make multiply commits without bothering a lot about commit messages and simply comment the changes with something like “work in progress” or just “WIP”.

Before merging such branch into master it is a good practice to squash all commits in one with a single commit message that describes the summary of the changes.

Below i will show you how to squash commits in Git before merging a branch into master. (more…)

Git – Checkout Previous Branch

While working with Git, it’s very common to move back and forth between two branches.

If you want to checkout the previous branch you was working on, there is no need to type the name of this branch each time.

In this short note i’ll show the easy way to switch between two branches in Git. (more…)

Git – Diff Between Branches

When you are working with multiple branches in Git, it’s important to be able to compare them and contrast the differences.

In this short note i will show how to compare two branches in Git using the git diff command.

I will show how to git diff between any two branches, e.g. current branch and master or git diff between master and staging and how to list only files that are different between two branches (without changes themselves). (more…)

Git – Diff Staged and Unstaged Files

While working with Git it is often required to check the changes between different areas.

Probably everyone knows the git diff, that shows the changes between the Working Directory and the Staging Area (git diff unstaged).

But it is also often needed to shows the changes between the Staging Area and the HEAD (git diff staged) or between the Working Directory and the HEAD (git diff staged and unstaged). (more…)

Git – Revert File to Previous Commit

Sometimes it happens that you make some changes to a file but later realize that it was a mistake and these changes have to be discarded.

In Git you can revert the changes made to a file if you haven’t committed them yet, as well as you can revert a file to any previous commit.

Here i will show how to revert a single file to a specific revision and how to reset an uncommitted file to the initial master’s state. (more…)

Git – Edit Last Commit Message

If you have suddenly noticed that the last commit message is unclear or contains some incorrect information – don’t worry, as the most resent commit message can be easily changed if you haven’t yet pushed the commit to a remote server

To edit the last commit message you have to repeat the git commit command with the --amend option.

The most recent commit message can be changed in a text editor or simply through the command-line interface. (more…)

Git – Undo Last Commit

There can be a lot of situations when you may want to undo the last commit in Git, that you haven’t pushed to a remote server yet.

For example, you have mistakenly committed the wrong files and want to revert the last local commit but keep the changes you made.

Or you may want to revert the last commit and cancel the changes.

Below i will show the both solutions of how to rollback the last commit but keep the changes you made and how to delete the last commit together with the last changes. (more…)