Chris.luChris.lu header image, displaying an 80s style landscape and sunset

git

two super heros pointing at each other, it represents two versions of a file getting compared in a git diff

git is an open-source version control system used for tracking changes in source code during software development. Git gives every developer a local copy of a project. After a developer has made his changes, they create a new commit and then push it to a central repository. Other developers can then pull that commit from the central repository into their local copy. Git makes branching (creating a separate line of development) and merging (combining changes from different branches) easy.

Installation

on Windows

Go to git-scm.com/download/win and click on Click here to download which will automatically download the latest (64bit) windows version

Double-click on the exe file when the download has finished to start the installation.

You will get asked a bunch of stuff. I usually keep the default settings but feel free to change the default values to fit your needs.

on Linux

If you are on a distribution (like Fedora or CentOS) that comes with dnf, then use this command:

sudo dnf install git-all

For distributions (like Debian or Ubuntu) that come with apt, then use this command:

sudo apt install git-all

on macOS

If you have the Xcode command line tools installed on your machine, then you already have git.

You can check if it is already installed using this command:

git --version

If it is not yet installed, you can visit git-scm.com/download/mac to see all the options. My favorite way of installing git is via brew using the following command:

brew install git

check what version is installed

Use the following command in your VSCode terminal (or favorite command line tool):

git --version

initialize git

To initialize a git project locally, you first need to use the following command:

git init

I will use GitHub as the origin, but you could use any other Git cloud service, like GitLab.

Next, go to GitHub and create a new repository by clicking on the "+" icon on the left of your user avatar (in the top right section of the page), then select "new repository".

Then use the following command to add the origin to your local git setup:

git remote add origin https://github.com/GITHUB_USER_NAME/GITHUB_REPOSITORY_NAME.git

If you are using the git credential manager and have multiple users, then you may want to set the default. To do so, add your DEFAULT_GIT_USER_NAME to the URL like this:

git remote add origin https://DEFAULT_GIT_USER_NAME@github.com/GITHUB_USER_NAME/GITHUB_REPOSITORY_NAME.git

Cloning a repository

VSCode has a source control tool that makes it easy to clone repositories, but if you prefer to do it manually via the command line, then use this command:

git clone https://github.com/GITHUB_USER_NAME/GITHUB_REPOSITORY_NAME.git

If you are using the git credential manager and have multiple users, then you may want to set the default. To do so, add your DEFAULT_GIT_USER_NAME into the URL like this when cloning:

git clone https://DEFAULT_GIT_USER_NAME@github.com/GITHUB_USER_NAME/GITHUB_REPOSITORY_NAME.git

If you have already cloned the repository and want to add or change the default user, then use this command:

git remote set-url origin https://DEFAULT_GIT_USER_NAME@github.com/GITHUB_USER_NAME/GITHUB_REPOSITORY_NAME.git

git status

To list the local files that differ from the ones in the remote repository and list files that are not tracked, use the following command:

git status

switch to another branch

To switch to another branch, use the following command:

git checkout BRANCH_NAME

Or you can use the new git switch, like so:

git switch BRANCH_NAME

shortcut to return to the previous branch

There is a shortcut to switch back to the previous branch:

git checkout -

Or using the new switch:

git switch -

Stash your changes before switching

If you want to switch to another branch but have uncommitted changes, then either commit them or stash them away using this command:

git stash

Now, if you want to see the changes that are currently stashed, use this command:

git stash show

Finally, to restore the files, use the following:

git stash apply

Create a new branch

To create a new branch (and stay on the current branch), use the following command:

git branch BRANCH_NAME

To create a new branch and also switch to it, we use checkout with the -b option, like so:

git checkout -b BRANCH_NAME

Or we can use the new git switch with the -c option, like so:

git switch -c BRANCH_NAME

renaming a branch

rename a local branch

Renaming a local branch is easy if you haven't pushed the branch yet. Just use the following command in your terminal (command line tool):

git branch --move OLD_BRANCH_NAME NEW_BRANCH_NAME

Renaming a remote branch

To rename a remote branch (a remote branch that has already been pushed), first use the command from the previous chapter to rename it locally.

Then use the following command in your terminal (command line tool) to push it:

git push -u origin NEW_BRANCH_NAME

The -u (alias for --set-upstream-to) option tells the local branch to track changes from the remote branch.

Now, the branch with the new name is also available remotely, but git has copied the branch to rename the old one, meaning the old branch is still present remotely.

At this point, letting everyone on the team know that you renamed the branch is recommended, as they might still have open commits. Tell them to execute the following commands so that they, too, switch to the new branch (those are the commands GitHub recommends after you rename a remote branch on GitHub):

git checkout OLD_BRANCH_NAME
git branch -m NEW_BRANCH_NAME
git fetch origin
git branch -u origin/NEW_BRANCH_NAME

First, they use checkout to switch to the old branch, then they rename it locally too using branch -m, then they fetch the origin to fetch the ref of all branches in origin, then they need to use the branch -u (which is an alias for set-upstream we used earlier) to tell the local branch to track the origin/NEW_BRANCH_NAME.

If it is the default branch that got renamed, also update origin/HEAD:

git remote set-head origin -a

After the other members of your team have updated their local environment, too, then you can go ahead and delete the old branch using this command:

git push origin --delete OLD_BRANCH_NAME

Update the list of remote branches (locally)

To update/refresh the list of remote branches locally, use this command:

git remote update origin --prune

Get an overview of all local and remote branches

To get an overview of all branches that exist remotely and locally, what local branches track remote branches, ... use this command:

git remote show origin

Delete a local branch

git branch --delete BRANCH_NAME