1. Learn Git
    1. Learn Git with Bitbucket Cloud
      1. Create a Git repository
      2. Copy your Git repository and add files
      3. Pull changes from your Git repository on Bitbucket Cloud
      4. Use a Git branch to merge a file
    2. Learn about code review in Bitbucket Cloud
      1. Fork a teammate's repository
      2. Copy your fork and make a change to the repository
      3. Create a pull request
  2. Getting Started
    1. What is version control
      1. Benefits of version control
    2. What is Git
      1. Performance
      2. Security
      3. Flexibility
      4. Version control with Git
    3. Why Git for your organization
      1. Git for developers
      2. Git for marketing
      3. Git for product management
      4. Git for designers
      5. Git for customer support
      6. Git for human resources
      7. Git for anyone managing a budget
    4. Install Git
      1. Install Git on Mac OS X
      2. Install Git on Windows
      3. Install Git on Linux
    5. Setting up a repository
      1. git init
      2. git clone
      3. git config
    6. Saving changes
      1. git add
      2. git commit
    7. Git Stash
      1. gitignore
        1. Inspecting a repository
          1. git status
          2. git log
        2. Viewing old commits
          1. Undoing Changes
            1. git checkout
            2. git revert
            3. git reset
            4. git clean
          2. Rewriting history
            1. git commit --amend
            2. git rebase
            3. git rebase -i
            4. git reflog
        3. Collaborating
          1. Syncing
            1. git remote
            2. git fetch
            3. git pull
            4. git push
          2. Making a Pull Request
            1. How it works
            2. Example
            3. Where to go from here
          3. Using Branches
            1. git branch
            2. git checkout
            3. git merge
          4. Comparing Workflows
            1. Centralized Workflow
            2. Feature Branch Workflow
            3. Gitflow Workflow
            4. Forking Workflow
        4. Migrating to Git
          1. SVN to Git - prepping for the migration
            1. For administrators
            2. Basic Git commands
            3. Git Migration Tools
            4. For developers
          2. Migrate to Git from SVN
            1. Prepare
              1. Convert
                1. Synchronize
                  1. Share
                    1. Migrate
                    2. Advanced Tips
                      1. Advanced Git Tutorials
                        1. Merging vs. Rebasing
                          1. Conceptual Overview
                          2. The Golden Rule of Rebasing
                          3. Workflow Walkthrough
                          4. Summary
                        2. Reset, Checkout, and Revert
                          1. Commit-level Operation
                          2. File-level Operations
                          3. Summary
                        3. Advanced Git log
                          1. Formatting Log Output
                          2. Filtering the Commit History
                          3. Summary
                        4. Git Hooks
                          1. Conceptual Overview
                          2. Local Hooks
                          3. Server-side Hooks
                          4. Summary
                        5. Refs and the Reflog
                          1. Hashes
                          2. Refs
                          3. Packed Refs
                          4. Special Refs
                          5. Refspecs
                          6. Relative Refs
                          7. The Reflog
                          8. Summary
                      Search

                      Setting up a repository

                      This tutorial provides a succinct overview of the most important Git commands. First, the Setting Up a Repository section explains all of the tools you need to start a new version-controlled project. Then, the remaining sections introduce your everyday Git commands.

                      By the end of this module, you should be able to create a Git repository, record snapshots of your project for safekeeping, and view your project’s history.

                      git init

                      The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. Most of the other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.

                      Executing git init creates a .git subdirectory in the project root, which contains all of the necessary metadata for the repo. Aside from the .git directory, an existing project remains unaltered (unlike SVN, Git doesn't require a .git folder in every subdirectory).

                      Usage

                      git init

                      Transform the current directory into a Git repository. This adds a .git folder to the current directory and makes it possible to start recording revisions of the project.

                      git init <directory>

                      Create an empty Git repository in the specified directory. Running this command will create a new folder called <directory containing nothing but the .git subdirectory.

                      git init --bare <directory>

                      Initialize an empty Git repository, but omit the working directory. Shared repositories should always be created with the --bare flag (see discussion below). Conventionally, repositories initialized with the --bare flag end in .git. For example, the bare version of a repository called my-project should be stored in a directory called my-project.git.

                      Discussion

                      Compared to SVN, the git init command is an incredibly easy way to create new version-controlled projects. Git doesn’t require you to create a repository, import files, and check out a working copy. All you have to do is cd into your project folder and run git init, and you’ll have a fully functional Git repository.

                      However, for most projects, git init only needs to be executed once to create a central repository—developers typically don't use git init to create their local repositories. Instead, they'll usually use git clone to copy an existing repository onto their local machine.

                      Bare Repositories

                      The --bare flag creates a repository that doesn’t have a working directory, making it impossible to edit files and commit changes in that repository. Central repositories should always be created as bare repositories because pushing branches to a non-bare repository has the potential to overwrite changes. Think of --bare as a way to mark a repository as a storage facility, opposed to a development environment. This means that for virtually all Git workflows, the central repository is bare, and developers local repositories are non-bare.

                      Git Tutorial: Bare Repositories

                      Example

                      Since git clone is a more convenient way to create local copies of a project, the most common use case for git init is to create a central repository:

                      ssh <user>@<host>
                      cd path/above/repo 
                      git init --bare my-project.git

                      First, you SSH into the server that will contain your central repository. Then, you navigate to wherever you’d like to store the project. Finally, you use the --bare flag to create a central storage repository. Developers would then [clone](/tutorials/setting-up-a-repository/git-clone) my-project.git to create a local copy on their development machine.

                      git clone

                      The git clone command copies an existing Git repository. This is sort of like svn checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.

                      As a convenience, cloning automatically creates a remote connection called origin pointing back to the original repository. This makes it very easy to interact with a central repository.

                      Usage

                      git clone <repo>

                      Clone the repository located at <repo> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.

                      git clone <repo> <directory>

                      Clone the repository located at <repo> into the folder called <directory> on the local machine.

                      Discussion

                      If a project has already been set up in a central repository, the git clone command is the most common way for users to obtain a development copy. Like git init, cloning is generally a one-time operation—once a developer has obtained a working copy, all version control operations and collaborations are managed through their local repository.

                      Repo-To-Repo Collaboration

                      It’s important to understand that Git’s idea of a “working copy” is very different from the working copy you get by checking out code from an SVN repository. Unlike SVN, Git makes no distinction between the working copy and the central repository—they are all full-fledged Git repositories.

                      This makes collaborating with Git fundamentally different than with SVN. Whereas SVN depends on the relationship between the central repository and the working copy, Git’s collaboration model is based on repository-to-repository interaction. Instead of checking a working copy into SVN’s central repository, you push or pull commits from one repository to another.

                      Git Tutorial: Repo to Working Copy Collaboration
                      Git Tutorial: Repo to Repo Collaboration

                      Of course, there’s nothing stopping you from giving certain Git repos special meaning. For example, by simply designating one Git repo as the “central” repository, it’s possible to replicate a Centralized workflow using Git. The point is, this is accomplished through conventions rather than being hardwired into the VCS itself.

                      Example

                      The example below demonstrates how to obtain a local copy of a central repository stored on a server accessible at example.com using the SSH username john:

                      git clone ssh://john@example.com/path/to/my-project.git 
                      cd my-project
                      # Start working on the project

                      The first command initializes a new Git repository in the my-project folder on your local machine and populates it with the contents of the central repository. Then, you can cd into the project and start editing files, committing snapshots, and interacting with other repositories. Also note that the .git extension is omitted from the cloned repository. This reflects the non-bare status of the local copy.

                      git config

                      The git config command lets you configure your Git installation (or an individual repository) from the command line. This command can define everything from user info to preferences to the behavior of a repository. Several common configuration options are listed below.

                      Usage

                      git config user.name <name>

                      Define the author name to be used for all commits in the current repository. Typically, you’ll want to use the --global flag to set configuration options for the current user.

                      git config --global user.name <name>

                      Define the author name to be used for all commits by the current user.

                      git config --global user.email <email>

                      Define the author email to be used for all commits by the current user.

                      git config --global alias.<alias-name> <git-command>

                      Create a shortcut for a Git command.

                      git config --system core.editor <editor>

                      Define the text editor used by commands like git commit for all users on the current machine. The <editor> argument should be the command that launches the desired editor (e.g., vi).

                      git config --global --edit

                      Open the global configuration file in a text editor for manual editing.

                      Discussion

                      All configuration options are stored in plaintext files, so the git config command is really just a convenient command-line interface. Typically, you’ll only need to configure a Git installation the first time you start working on a new development machine, and for virtually all cases, you’ll want to use the --global flag.

                      Git stores configuration options in three separate files, which lets you scope options to individual repositories, users, or the entire system:

                      • <repo>/.git/config – Repository-specific settings.
                      • ~/.gitconfig – User-specific settings. This is where options set with the --global flag are stored.
                      • $(prefix)/etc/gitconfig – System-wide settings.

                      When options in these files conflict, local settings override user settings, which override system-wide. If you open any of these files, you’ll see something like the following:

                      [user] 
                      name = John Smith
                      email = john@example.com
                      [alias]
                      st = status
                      co = checkout
                      br = branch
                      up = rebase
                      ci = commit
                      [core]
                      editor = vim

                      You can manually edit these values to the exact same effect as git config.

                      Example

                      The first thing you’ll want to do after installing Git is tell it your name/email and customize some of the default settings. A typical initial configuration might look something like the following:

                      # Tell Git who you are
                      git config --global user.name "John Smith"
                      git config --global user.email john@example.com
                      # Select your favorite text editor
                      git config --global core.editor vim
                      # Add some SVN-like aliases
                      git config --global alias.st status
                      git config --global alias.co checkout
                      git config --global alias.br branch
                      git config --global alias.up rebase
                      git config --global alias.ci commit

                      This will produce the ~/.gitconfig file from the previous section.