Git Guide voor Terminal - GloryDaysApp/glorydays GitHub Wiki

How To Work With Git Using The Terminal

In this article we will discuss how you can work with Git using your terminal.

Table of Contents

Installing Git

Before you can work with Git in your terminal you first need to install it. You can do this for your operating system as described below.

Installing Git on macOS

The easiest way to install Git on a Mac is to install the Xcode Command Line Tools. On Mavericks (10.9) or above you can do this simply by running git in the terminal the very first time.

git --version

If you haven't installed it yet you will be prompted to install it.

Installing Git on Linux

You can installing Git on Linux through the package management tool that comes with your distribution. If you’re on Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

sudo dnf install git-all

If you’re on a Debian-based distribution, such as Ubuntu, try apt:

sudo apt install git-all

For more options, there are instructions for installing on several different Unix distributions on the Git website.

Installing Git on Windows

To install Git on Windows just go to the Git website and the download will start automatically.

Clone a Git Repository

When you downloaded Git and created a repository (assuming you know how to do this), you first need to clone that remote repository to be able to start coding. To clone a remote repository via your terminal you first need to copy the web URL. This you can find when you go to your repository on GitHub and click on the green "Clone or download" button. Once you clicked on the button you can see the web URL. You'll need to copy that so you can use in the clone command.

After you copied the web URL, you'll need to start your terminal and go to the folder you want store your project. You can go to the folder using the change directory command.

cd Path/To/Folder

When you are in the right folder you can start cloning the repository using the clone command.

git clone https://github.com/GithubUser/yourProject.git

Congratulations you have just cloned you repository! Now you can go to the project folder using the change directory command again.

cd yourProject

Working with Branches

The most recommended way to work with Git is to work in branches. This because then your master branch is always clean and if you make a mistake you always have the last working code in your master branch.

Create a New Branch

To create a new branch you need to run the command for creating a branch.

git checkout -b branch_name

Change branch_name to the name of your branch.

Make sure you are in the correct branch before creating a new one. This because the current branch you're on will form the base of the new branch. So if you want to create a new branch with the master branch as the base you need to make sure you are currently on the master branch.

Switch to Another Branch

Sometimes you want to check what you did in another branch while working in your current branch. You can check the other branch by using the checkout command.

git checkout branch_name

Change branch_name to the name of the branch you want to switch to.

Pay attention: You can't switch to another branch if your current branch already has some changes. If you still need to switch to another branch you need to stash your work or commit your changes.

Delete a Branch

To delete a branch you can use the delete command.

git branch -d branch_name

Change branch_name to the name of the branch you want to delete.

Fetch Remote Branches

When you are working in a team or want to work on another computer you need to fetch the remote branches to be able to work on / switch to them.

To fetch the remote branches you can simply use the fetch command.

git fetch

Committing Changes

When you made some changes in your code you need to commit them when you are finished to be able to push them to the remote repository.

First you need to see which files you changed. You can do this by using the status command.

git status

When files are staged, which means that they will be included in the commit, they are green. If this is not the case then they are red and need to be staged before you can commit them. You can do this two ways. You can add them all at once by using the add . command.

git add .

But if you want to only add some of the files, because you want to for example categorise them in different commits. You can also add files one by one.

git add file.js

Or if you want to add all the files in one directory:

git add directory-name/*

And change directory-name to the name of your directory.

If you are not sure what is changed in a file, you can see this using the diff command.

git diff file.js

After running this command you will see all the changes in that file. You can see the deleted lines in red with a - in advance and added lines in green with a + in advance. And if you want to close it you can simply press the q key.

After adding the files you can commit them using the commit command.

git commit -m "commit message here"

The parameter -m stands for message. Here, between the quotes, you probably want to write what you are committing.

Pushing to Remote Repository

When you committed your changes you can push them to the remote repository using the push command.

git push remote branch_name

Change remote to your remote (most of the time this is origin) and change branch_name to the name of the current branch you want to push.

Create a Pull Request

After you pushed the branch the first time you get a link in your terminal. If you click on this link you will go to Github and create a pull request. When on this page you will see the following buttons:

In the first button you can choose to with branch you want to push your branch, most of the time this is master. And in the second button you need to make sure your current branch you want to push is selected.

After you selected the branches you can create the pull request by clicking on the green "Create pull request" button. When there are no merge conflicts you can merge the branch and delete it if you are sure you don't have to use it anymore in the future.

When there are merge conflicts you first need to solve them. You can do this as described on the page.

If you pushed the branch before, because for example someone else needed to work on it, you can still create a pull request. But this time the easiest way is to go to Github. Here you will see a banner with a green "compare & pull request" button. When you click on this button you will be sent to the pull request page as explained earlier.

Pulling from Remote Repository

When you want to pull all the changes from the remote repository to your local project your can use the pull command. And make sure you are in the branch you want to pull the changes from!

git pull remote branch_name

Change remote to your remote (most of the time this is origin) and change branch_name to the name of the current branch you want to push. Most of the time you want to get the changes from the master branch so the command will look like this:

git pull origin master

Stashing Changes

When you are coding and you want to start over, you can easily put your changes away using the stash save command.

git stash save

or (if you don't want to type that much :wink:):

git stash

When creating a stash, Git will generate a code for it so you can always get it back using the stash apply command.

git stash apply

Then you'll need to inspect the results carefully (with git diff) to see if you want to use them, and if you do, use the stash drop command to drop the stash.

git stash drop

You can, for example, use this when you started working on a new (clean) branch and realized you were doing them in the wrong branch. With this method you can take the changes you have now and "move" them to another branch. In this case you'll want to use stash save on your current branch and the other commands (stash apply and stash drop) on the branch where you want to "move" them to.

Working with Submodules

When you want to link another repository in your repository, because that is a sub-project of your main project, you can use submodules.

To add a new submodule you use the submodule add command with the web URL of the project you would like to start tracking.

git submodule add https://github.com/GithubUser/yourSubProject.git

After you added the submodule you will see that a file named .gitmodules is added to your repository. This is a configuration file that stores the mapping between the project’s URL and the local subdirectory you’ve pulled it into.

Now you can commit and push your changes.

Cloning a Repository with Submodules

When you need to clone a project with submodules, you'll first need to clone the main repository with the clone command.

git clone https://github.com/GithubUser/yourProject.git

After that you'll need to run 2 commands:

  1. submodule init to initialise your local configuration file;
  2. submodule update to fetch all the data from that repository and check out the appropriate commit listed in your main repository.
git submodule init
git submodule update

Another way to do this is to use the clone --recurse-submodules command. But pay attention, this will automatically initialise and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

git clone --recurse-submodules https://github.com/GithubUser/yourProject.git

Change or Add a Git User

If you want to work with 2 git account on 1 computer you can use the config command to define with which user you are working on that repository (local) or on all repositories (globally).

You can see the configuration by running the config --edit command.

git config --edit

And if you want to see it globally:

git config --global --edit

You can quit reading the file you typing :qa!.

When you want to add or change a user, you can use the config user.name and config user.email commands.

git config user.name “githubUsername”
git config user.email email

Change "githubUsername" to your own GitHub username and "email" to the email that is linked to that account.

And if you want to add or change a user globally:

git config --global user.name "githubUsername"
git config --global user.email email

Again change "githubUsername" to your own GitHub username and "email" to the email that is linked to that account.