Git Tutorial

Git is a distributed version control system because it has a remote repository which is stored on a server and a local repository which is stored on the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers computers. A version control system like Git helps developing teams to merge code of individual team members to produce software.

Installation

Here is a tutorial how to install Git.

Workflow

Create working copy

Use git clone to clone an existing remote repository into your computer to create a local working copy.

git clone /url/to/repository

Stage changes

Before committing the code, it has to be in the staging area. The staging area is there to keep track of all the files which are to be committed. Any file which is not added to the staging area will not be committed. This gives the developer control over which files need to be committed.

git add demo.txt

If you want to add all the files inside your project folder to the staging area, use the following command:

git add .

Commit changes

Committing is the process in which the code is added to the local repository.

git commit -m "Initial Commit"

“Initial Commit” is the commit message here. Enter a relevant commit message to indicate what code changes were done in that particular commit.

Push changes

In order to push all the code from the local repository into the remote repository, use the following command:

git push -u origin master

Pull changes

Git pull is used to pull the latest changes from the remote repository into the local repository. Git pull is necessary if the remote repository code is updated continuously by various developers.

git pull origin master

Git Ignore

In order not to push all files in a directory (and still be able to use "git commit"), it's a good idea to tell Git which files to ignore. For this a so-called ".gitignore" file can be created. Basically every file consists of patterns, which Git checks. If a file matches one of these patterns, Git ignores it. An example .gitignore could look like this:

# Intellij
.idea/
*.iml
*.iws

# Maven
log/
target/

Other useful commands

Git help

List all commands from git:

git --help

Git init

Create a new git-Repository in the current directory

git init

Git config

Configure your username and password etc.

git config

Git status

Show the status of tracked and untracked files

git status

Git merge

Join two or more development histories together

git merge

Source:

results matching ""

    No results matching ""