Git: Tutorial & Best Practices

A version control system

Git is a free and open-source distributed version control system (VCS) designed to track changes in source code and other text files. It is a powerful tool that allows developers to efficiently manage and track changes to their codebase, and it is widely used in software development projects around the world.

Git works by storing versions of files in a repository, which is a central location where changes are tracked. When a developer makes a change to a file, they can commit the change to the repository, along with a message describing the change. Git stores a history of all the changes that have been made to the repository, and it allows developers to easily view, compare, and revert changes as needed.

Git also supports branching, which allows developers to create copies of the repository and work on them independently. This is useful for developing new features or fixing bugs without affecting the main codebase.

Git is a powerful and flexible tool for managing code changes, and it is widely used in software development projects to improve collaboration, track changes, and maintain version history.

To use Git on the command line, you will need to follow these steps:

  • Install Git: To install Git on a Debian-based system (such as Ubuntu), you can use the following command:

    sudo apt-get update && sudo apt-get install git
    

    To install Git on a Red Hat-based system (such as CentOS), you can use the following command:

    sudo yum install git
    
  • Configure Git: Before you can use Git, you will need to configure your name and email address. You can do this using the following commands:

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    

    You can also configure other options such as the default text editor, the default merge tool, and the default push behavior.

  • Create a repository: To create a Git repository, you will need to create a directory and run the git init command inside it. For example, to create a repository in the current directory, you can use the following command:

    git init
    

    This will create a new repository in the current directory, and it will create a hidden .git directory that stores the repository metadata.

  • Add and commit files: To add files to the repository, you can use the git add command. For example, to add all the files in the current directory, you can use the following command:

    git add .
    

    To commit the changes to the repository, you can use the git commit command. For example, to commit the changes with a message "Initial commit", you can use the following command:

    git commit -m "Initial commit"
    
  • View the commit history: To view the commit history of the repository, you can use the git log command. This will show a list of all the commits that have been made to the repository, along with the commit message, author, and date.

  • Clone a repository: To clone an existing repository, you can use the git clone command. For example, to clone a repository from GitHub, you can use the following command:

    git clone https://github.com/user/repo.git
    

    This will create a local copy of the repository on your machine, and it will automatically create a remote called "origin" that points to the original repository.

  • Push and pull changes: To push changes from your local repository to a remote repository, you can use the git push command. For example, to push the changes to the "origin" remote, you can use the following command:

    git push origin master
    

    To pull changes from a remote repository to your local repository, you can use the git pull command. For example, to pull the changes from the "origin" remote, you can use the following command:

    git pull origin master
    
  • Branching and merging: Git supports branching, which allows you to create a copy of the repository and work on it independently. To create a new branch, you can use the git branch command. For example, to create a new branch called "feature", you can use the following command:

    git branch feature
    

    To switch to a different branch, you can use the git checkout command. For example, to switch to the "feature" branch, you can use the following command:

    git checkout feature
    

    To merge changes from one branch into another, you can use the git merge command. For example, to merge the changes from the "feature" branch into the "master" branch, you can use the following command:

    git merge feature
    
  • Resolving conflicts: If you try to merge two branches that have conflicting changes, Git will report a merge conflict. To resolve a merge conflict, you will need to edit the conflicting files and remove the conflict markers. Once you have resolved the conflicts, you can use the git add command to mark the files as resolved, and then use the git commit command to commit the changes.

git initial commit

Execute this command on the server:

git init --bare projectdir

Execute these commands on the client:

git init
git add .
git remote add origin ssh://git@server:port/absolutepath
git commit -m "initial commit"
git push origin master

To convert from an SVN repository:

git svn clone --stdlayout --no-metadata --authors-file=users.txt https://server/path destination

Use --stdlayout if the repository uses trunk branches and tags, otherwise you can omit it.

In the users file you can specify the mapping of usernames:

user = firstname lastname <email>

That's it! These are the basic Git commands that you will need to know to use Git on the command line. There are many other Git commands and options that you can use to manage your repository, and you can learn more about them by consulting the Git documentation or online resources.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA