diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /doc/gitlab-basics | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'doc/gitlab-basics')
-rw-r--r-- | doc/gitlab-basics/README.md | 1 | ||||
-rw-r--r-- | doc/gitlab-basics/add-file.md | 2 | ||||
-rw-r--r-- | doc/gitlab-basics/create-branch.md | 4 | ||||
-rw-r--r-- | doc/gitlab-basics/create-project.md | 1 | ||||
-rw-r--r-- | doc/gitlab-basics/create-your-ssh-keys.md | 1 | ||||
-rw-r--r-- | doc/gitlab-basics/fork-project.md | 1 | ||||
-rw-r--r-- | doc/gitlab-basics/start-using-git.md | 552 |
7 files changed, 276 insertions, 286 deletions
diff --git a/doc/gitlab-basics/README.md b/doc/gitlab-basics/README.md index c815842480c..488d86f129d 100644 --- a/doc/gitlab-basics/README.md +++ b/doc/gitlab-basics/README.md @@ -1,5 +1,6 @@ --- redirect_to: 'index.md' +remove_date: '2021-05-11' --- This document was moved to [another location](index.md). diff --git a/doc/gitlab-basics/add-file.md b/doc/gitlab-basics/add-file.md index 33db7d74949..6dd0c608983 100644 --- a/doc/gitlab-basics/add-file.md +++ b/doc/gitlab-basics/add-file.md @@ -33,7 +33,7 @@ cd <destination folder> to the default branch should be avoided unless your project is very small and you're the only person working on it. -You can also [switch to an existing branch](start-using-git.md#work-on-an-existing-branch) +You can also [switch to an existing branch](start-using-git.md#switch-to-a-branch) if you have one already. Using your standard tool for copying files (for example, Finder in macOS, or File Explorer diff --git a/doc/gitlab-basics/create-branch.md b/doc/gitlab-basics/create-branch.md index 8135c16bc1e..176189298c8 100644 --- a/doc/gitlab-basics/create-branch.md +++ b/doc/gitlab-basics/create-branch.md @@ -9,9 +9,9 @@ type: howto A branch is an independent line of development in a [project](../user/project/index.md). -When you create a new branch (in your [terminal](start-using-git.md#create-a-branch) or with +When you create a branch (in your [terminal](start-using-git.md#create-a-branch) or with [the web interface](../user/project/repository/web_editor.md#create-a-new-branch)), -you are creating a snapshot of a certain branch, usually the main `master` branch, +you are creating a snapshot of a certain branch, usually the main branch, at its current state. From there, you can start to make your own changes without affecting the main codebase. The history of your changes is tracked in your branch. diff --git a/doc/gitlab-basics/create-project.md b/doc/gitlab-basics/create-project.md index 18886120c63..2d9e458408a 100644 --- a/doc/gitlab-basics/create-project.md +++ b/doc/gitlab-basics/create-project.md @@ -1,5 +1,6 @@ --- redirect_to: '../user/project/working_with_projects.md' +remove_date: '2021-05-05' --- This document was moved to [another location](../user/project/working_with_projects.md). diff --git a/doc/gitlab-basics/create-your-ssh-keys.md b/doc/gitlab-basics/create-your-ssh-keys.md index 9cbaca91f7d..a99307e6dbf 100644 --- a/doc/gitlab-basics/create-your-ssh-keys.md +++ b/doc/gitlab-basics/create-your-ssh-keys.md @@ -1,5 +1,6 @@ --- redirect_to: '../ssh/README.md' +remove_date: '2021-07-04' --- This document was moved to [another location](../ssh/README.md). diff --git a/doc/gitlab-basics/fork-project.md b/doc/gitlab-basics/fork-project.md index adb49c6970f..f006f8b7ad6 100644 --- a/doc/gitlab-basics/fork-project.md +++ b/doc/gitlab-basics/fork-project.md @@ -1,5 +1,6 @@ --- redirect_to: '../user/project/working_with_projects.md' +remove_date: '2021-05-04' --- This document was moved to [another location](../user/project/working_with_projects.md). diff --git a/doc/gitlab-basics/start-using-git.md b/doc/gitlab-basics/start-using-git.md index a3c8946700a..f9623586e55 100644 --- a/doc/gitlab-basics/start-using-git.md +++ b/doc/gitlab-basics/start-using-git.md @@ -8,280 +8,254 @@ description: "Introduction to using Git through the command line." # Start using Git on the command line **(FREE)** -[Git](https://git-scm.com/) is an open-source distributed version control system designed to -handle everything from small to very large projects with speed and efficiency. GitLab is built +[Git](https://git-scm.com/) is an open-source distributed version control system. GitLab is built on top of Git. -While GitLab has a powerful user interface from which you can do a great amount of Git operations -directly in the browser, you'll eventually need to use Git through the command line for advanced -tasks. +You can do many Git operations directly in GitLab. However, the command line is required for advanced tasks, +like fixing complex merge conflicts or rolling back commits. -For example, if you need to fix complex merge conflicts, rebase branches, -merge manually, or undo and roll back commits, you must use Git from -the command line and then push your changes to the remote server. +For a quick reference of Git commands, download a [Git Cheat Sheet](https://about.gitlab.com/images/press/git-cheat-sheet.pdf). -This guide helps you get started with Git through the command line and can be your reference -for Git commands in the future. If you're only looking for a quick reference of Git commands, you -can download the GitLab [Git Cheat Sheet](https://about.gitlab.com/images/press/git-cheat-sheet.pdf). +For more information about the advantages of working with Git and GitLab: -> For more information about the advantages of working with Git and GitLab: -> -> - <i class="fa fa-youtube-play youtube" aria-hidden="true"></i> Watch the [GitLab Source Code Management Walkthrough](https://www.youtube.com/watch?v=wTQ3aXJswtM) video. -> - Learn how GitLab became the backbone of [Worldline](https://about.gitlab.com/customers/worldline/)'s development environment. +- <i class="fa fa-youtube-play youtube" aria-hidden="true"></i> Watch the [GitLab Source Code Management Walkthrough](https://www.youtube.com/watch?v=wTQ3aXJswtM) video. +- Learn how [GitLab became the backbone of the Worldline](https://about.gitlab.com/customers/worldline/) development environment. -NOTE: -To help you visualize what you're doing locally, there are -[Git GUI apps](https://git-scm.com/download/gui/) you can install. +To help you visualize what you're doing locally, you can install a +[Git GUI app](https://git-scm.com/download/gui/). -## Requirements +## Git terminology -You don't need a GitLab account to use Git locally, but for the purpose of this guide we -recommend registering and signing into your account before starting. Some commands need a -connection between the files in your computer and their version on a remote server. +If you're familiar with Git terminology, you might want to skip this section and +go directly to [prerequisites](#prerequisites). -You must also open a [command shell](#command-shell) and have -[Git installed](#install-git) in your computer. +### Repository -### Command shell +In GitLab, files are stored in a **repository**. A repository is similar to how you +store files in a folder or directory on your computer. -To execute Git commands in your computer, you must open a command shell (also known as command -prompt, terminal, and command line) of your preference. Here are some suggestions: +- A **remote repository** refers to the files in GitLab. +- A **local copy** refers to the files on your computer. -- For macOS users: - - Built-in: [Terminal](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line). Press <kbd>⌘ command</kbd> + <kbd>space</kbd> and type "terminal" to find it. - - [iTerm2](https://iterm2.com/), which you can integrate with [zsh](https://git-scm.com/book/id/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-Zsh) and [oh my zsh](https://ohmyz.sh/) for color highlighting, among other handy features for Git users. -- For Windows users: - - Built-in: `cmd`. Click the search icon on the bottom navigation bar on Windows and type `cmd` to find it. - - [PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/install/installing-windows-powershell?view=powershell-7): a Windows "powered up" shell, from which you can execute a greater number of commands. - - Git Bash: it comes built into [Git for Windows](https://gitforwindows.org/). -- For Linux users: - - Built-in: [Linux Terminal](https://www.howtogeek.com/140679/beginner-geek-how-to-start-using-the-linux-terminal/). +<!-- vale gitlab.Spelling = NO --> +<!-- vale gitlab.SubstitutionWarning = NO --> +Often, the word "repository" is shortened to "repo". +<!-- vale gitlab.Spelling = YES --> +<!-- vale gitlab.SubstitutionWarning = YES --> -### Install Git +In GitLab, a repository is contained in a **project**. -Open a command shell and run the following command to check if Git is already installed in your -computer: +### Fork -```shell -git --version -``` +When you want to contribute to someone else's repository, you make a copy of it. +This copy is called a [**fork**](../user/project/repository/forking_workflow.md#creating-a-fork). +The process is called "creating a fork." -If you have Git installed, the output is: +When you fork a repo, you create a copy of the project in your own +[namespace](../user/group/#namespaces). You then have write permissions to modify the project files +and settings. -```shell -git version X.Y.Z -``` +For example, you can fork this project, <https://gitlab.com/gitlab-tests/sample-project/>, into your namespace. +You now have your own copy of the repository. You can view the namespace in the URL, for example +`https://gitlab.com/your-namespace/sample-project/`. +Then you can clone the repository to your local machine, work on the files, and submit changes back to the +original repository. -If your computer doesn't recognize `git` as a command, you must [install Git](../topics/git/how_to_install_git/index.md). -After that, run `git --version` again to verify whether it was correctly installed. +### Difference between download and clone -## Configure Git +To create a copy of a remote repository's files on your computer, you can either +**download** or **clone** the repository. If you download it, you cannot sync the repository with the +remote repository on GitLab. -To start using Git from your computer, you must enter your credentials (user name and email) -to identify you as the author of your work. The user name and email should match the ones you're -using on GitLab. +[Cloning](#clone-a-repository) a repository is the same as downloading, except it preserves the Git connection +with the remote repository. You can then modify the files locally and +upload the changes to the remote repository on GitLab. -In your shell, add your user name: +### Pull and push -```shell -git config --global user.name "your_username" -``` +After you save a local copy of a repository and modify the files on your computer, you can upload the +changes to GitLab. This is referred to as **pushing** to the remote, because you use the command +[`git push`](#send-changes-to-gitlabcom). -And your email address: +When the remote repository changes, your local copy is behind. You can update your local copy with the new +changes in the remote repository. +This is referred to as **pulling** from the remote, because you use the command +[`git pull`](#download-the-latest-changes-in-the-project). -```shell -git config --global user.email "your_email_address@example.com" -``` +## Prerequisites -To check the configuration, run: +To start using GitLab with Git, complete the following tasks: -```shell -git config --global --list -``` +- Create and sign in to a GitLab account. +- [Open a terminal](#open-a-terminal). +- [Install Git](#install-git) on your computer. +- [Configure Git](#configure-git). +- [Choose a repository](#choose-a-repository). -The `--global` option tells Git to always use this information for anything you do on your system. -If you omit `--global` or use `--local`, the configuration is applied only to the current -repository. +### Open a terminal -You can read more on how Git manages configurations in the -[Git configuration documentation](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration). +To execute Git commands on your computer, you must open a terminal (also known as command +prompt, command shell, and command line). Here are some options: -## Git authentication methods +- For macOS users: + - Built-in [Terminal](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line). Press <kbd>⌘ command</kbd> + <kbd>space</kbd> and type `terminal`. + - [iTerm2](https://iterm2.com/). You can integrate it with [zsh](https://git-scm.com/book/id/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-Zsh) and [oh my zsh](https://ohmyz.sh/) for color highlighting and other advanced features. +- For Windows users: + - Built-in command line. On the Windows taskbar, select the search icon and type `cmd`. + - [PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/install/installing-windows-powershell?view=powershell-7). + - Git Bash. It is built into [Git for Windows](https://gitforwindows.org/). +- For Linux users: + - Built-in [Linux Terminal](https://ubuntu.com/tutorials/command-line-for-beginners#3-opening-a-terminal). -To connect your computer with GitLab, you need to add your credentials to identify yourself. -You have two options: +### Install Git -- Authenticate on a project-by-project basis through HTTPS, and enter your credentials every time - you perform an operation between your computer and GitLab. -- Authenticate through SSH once and GitLab no longer requests your credentials every time you pull, push, - and clone. +Determine if Git is already installed on your computer by opening a terminal +and running this command: -To start the authentication process, we'll [clone](#clone-a-repository) an existing repository -to our computer: +```shell +git --version +``` -- If you want to use **SSH** to authenticate, follow the instructions on the [SSH documentation](../ssh/README.md) - to set it up before cloning. -- If you want to use **HTTPS**, GitLab requests your user name and password: - - If you have 2FA enabled for your account, you must use a [Personal Access Token](../user/profile/personal_access_tokens.md) - with **read_repository** or **write_repository** permissions instead of your account's password. - Create one before cloning. - - If you don't have 2FA enabled, use your account's password. +If Git is installed, the output is: -NOTE: -Authenticating via SSH is the GitLab recommended method. You can read more about credential storage -in the [Git Credentials documentation](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage). +```shell +git version X.Y.Z +``` -## Git terminology +If your computer doesn't recognize `git` as a command, you must [install Git](../topics/git/how_to_install_git/index.md). +After you install Git, run `git --version` to confirm that it installed correctly. -If you're familiar with the Git terminology, you may want to jump directly -into the [basic commands](#basic-git-commands). +### Configure Git -### Namespace +To start using Git from your computer, you must enter your credentials +to identify yourself as the author of your work. The username and email address +should match the ones you use in GitLab. -A **namespace** is either a **user name** or a **group name**. +1. In your shell, add your user name: -For example, suppose Jo is a GitLab.com user and they chose their user name as -`jo`. You can see Jo's profile at `https://gitlab.com/jo`. `jo` is a namespace. + ```shell + git config --global user.name "your_username" + ``` -Jo also created a group in GitLab, and chose the path `test-group` for their -group. The group can be accessed under `https://gitlab.com/test-group`. `test-group` is a namespace. +1. Add your email address: -### Repository + ```shell + git config --global user.email "your_email_address@example.com" + ``` -Your files in GitLab live in a **repository**, similar to how you have them in a folder or -directory in your computer. **Remote** repository refers to the files in -GitLab and the copy in your computer is called **local** copy. -A **project** in GitLab is what holds a repository, which holds your files. +1. To check the configuration, run: -<!-- vale gitlab.Spelling = NO --> -<!-- vale gitlab.SubstitutionWarning = NO --> -Often, the word "repository" is shortened to "repo". -<!-- vale gitlab.Spelling = YES --> -<!-- vale gitlab.SubstitutionWarning = YES --> + ```shell + git config --global --list + ``` -### Fork + The `--global` option tells Git to always use this information for anything you do on your system. + If you omit `--global` or use `--local`, the configuration applies only to the current + repository. -When you want to copy someone else's repository, you [**fork**](../user/project/repository/forking_workflow.md#creating-a-fork) -the project. By forking it, you create a copy of the project into your own -namespace to have read and write permissions to modify the project files -and settings. +You can read more on how Git manages configurations in the +[Git configuration documentation](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration). -For example, if you fork this project, <https://gitlab.com/gitlab-tests/sample-project/> into your namespace, you create your own copy of the repository in your namespace (`https://gitlab.com/your-namespace/sample-project/`). From there, you can clone it into your computer, -work on its files, and (optionally) submit proposed changes back to the -original repository if you'd like. +### Choose a repository -### Download vs clone +Before you begin, choose the repository you want to work in. You can use any project you have permission to +access on GitLab.com or any other GitLab instance. -To create a copy of a remote repository's files on your computer, you can either -**download** or **clone**. If you download, you cannot sync it with the -remote repository on GitLab. +To use the repository in the examples on this page: -[Cloning](#clone-a-repository) a repository is the same as downloading, except it preserves the Git connection -with the remote repository. This allows you to modify the files locally and -upload the changes to the remote repository on GitLab. +1. Go to [https://gitlab.com/gitlab-tests/sample-project/](https://gitlab.com/gitlab-tests/sample-project/). +1. In the top right, select **Fork**. +1. Choose a namespace for your fork. -### Pull and push +The project becomes available at `https://gitlab.com/<your-namespace>/sample-project/`. -After you saved a local copy of a repository and modified its files on your computer, you can upload the -changes to GitLab. This is referred to as **pushing** to GitLab, as this is achieved by the command -[`git push`](#send-changes-to-gitlabcom). +You can [fork](../user/project/repository/forking_workflow.md#creating-a-fork) any project you have access to. -When the remote repository changes, your local copy is behind it. You can update it with the new -changes in the remote repository. -This is referred to as **pulling** from GitLab, as this is achieved by the command -[`git pull`](#download-the-latest-changes-in-the-project). +## Clone a repository -## Basic Git commands +When you clone a repository, the files from the remote repository are downloaded to your computer, +and a connection is created. -For the purposes of this guide, we use this example project on GitLab.com: -[https://gitlab.com/gitlab-tests/sample-project/](https://gitlab.com/gitlab-tests/sample-project/). +This connection requires you to add credentials. You can either use SSH or HTTPS. SSH is recommended. -To use it, log into GitLab.com and fork the example project into your -namespace to have your own copy to playing with. Your sample -project is available under `https://gitlab.com/<your-namespace>/sample-project/`. +### Clone with SSH -You can also choose any other project to follow this guide. Then, replace the -example URLs with your own project's. +Clone with SSH when you want to authenticate only one time. -If you want to start by copying an existing GitLab repository onto your -computer, see how to [clone a repository](#clone-a-repository). On the other -hand, if you want to start by uploading an existing folder from your computer -to GitLab, see how to [convert a local folder into a Git repository](#convert-a-local-directory-into-a-repository). +1. Authenticate with GitLab by following the instructions in the [SSH documentation](../ssh/README.md). +1. Go to your project's landing page and select **Clone**. Copy the URL for **Clone with SSH**. +1. Open a terminal and go to the directory where you want to clone the files. Git automatically creates a folder with the repository name and downloads the files there. +1. Run this command: -### Clone a repository + ```shell + git clone git@gitlab.com:gitlab-tests/sample-project.git + ``` -To start working locally on an existing remote repository, clone it with the -command `git clone <repository path>`. You can either clone it via [HTTPS](#clone-via-https) -or [SSH](#clone-via-ssh), according to your preferred [authentication method](#git-authentication-methods). +1. To view the files, go to the new directory: -You can find both paths (HTTPS and SSH) by navigating to your project's landing page -and clicking **Clone**. GitLab prompts you with both paths, from which you can copy -and paste in your command line. You can also -[clone and open directly in Visual Studio Code](../user/project/repository/index.md#clone-and-open-in-apple-xcode). + ```shell + cd sample-project + ``` -For example, considering our [sample project](https://gitlab.com/gitlab-tests/sample-project/): +You can also +[clone a repository and open it directly in Visual Studio Code](../user/project/repository/index.md#clone-and-open-in-visual-studio-code). -- To clone through HTTPS, use `https://gitlab.com/gitlab-tests/sample-project.git`. -- To clone through SSH, use `git@gitlab.com:gitlab-tests/sample-project.git`. +### Clone with HTTPS -To get started, open a terminal window in the directory you wish to add the -repository files into, and run one of the `git clone` commands as described below. +Clone with HTTPS when you want to authenticate each time you perform an operation +between your computer and GitLab. -Both commands download a copy of the files in a folder named after the project's -name and preserve the connection with the remote repository. -You can then navigate to the new directory with `cd sample-project` and start working on it -locally. +1. Go to your project's landing page and select **Clone**. Copy the URL for **Clone with HTTPS**. +1. Open a terminal and go to the directory where you want to clone the files. +1. Run the following command. Git automatically creates a folder with the repository name and downloads the files there. + + ```shell + git clone https://gitlab.com/gitlab-tests/sample-project.git + ``` -#### Clone via HTTPS +1. GitLab requests your username and password: + - If you have 2FA enabled for your account, you must use a [Personal Access Token](../user/profile/personal_access_tokens.md) + with **read_repository** or **write_repository** permissions instead of your account's password. + - If you don't have 2FA enabled, use your account's password. -To clone `https://gitlab.com/gitlab-tests/sample-project/` via HTTPS: +1. To view the files, go to the new directory: -```shell -git clone https://gitlab.com/gitlab-tests/sample-project.git -``` + ```shell + cd sample-project + ``` NOTE: -On Windows, if you entered incorrect passwords multiple times and GitLab is responding `Access denied`, -you may have to add your namespace (user name or group name) to clone through HTTPS: +On Windows, if you enter your password incorrectly multiple times and an `Access denied` message appears, +add your namespace (username or group) to the path: `git clone https://namespace@gitlab.com/gitlab-org/gitlab.git`. -#### Clone via SSH - -To clone `git@gitlab.com:gitlab-org/gitlab.git` via SSH: - -```shell -git clone git@gitlab.com:gitlab-org/gitlab.git -``` - ### Convert a local directory into a repository -When you have your files in a local folder and want to convert it into -a repository, you must _initialize_ the folder through the `git init` -command. This instructs Git to begin to track that directory as a -repository. To do so, open the terminal on the directory you'd like to convert -and run: +You can initialize a local folder so Git tracks it as a repository. -```shell -git init -``` +1. Open the terminal in the directory you'd like to convert. +1. Run this command: -This command creates a `.git` folder in your directory that contains Git -records and configuration files. We advise against editing these files -directly. + ```shell + git init + ``` -Then, on the next step, add the [path to your remote repository](#add-a-remote-repository) -so that Git can upload your files into the correct project. + A `.git` folder is created in your directory. This folder contains Git + records and configuration files. You should not edit these files + directly. -#### Add a remote repository +1. Add the [path to your remote repository](#add-a-remote) + so Git can upload your files into the correct project. -By "adding a remote repository" to your local directory you tell Git that -the path to that specific project in GitLab corresponds to that specific -folder you have in your computer. This way, your local folder is -identified by Git as the local content for that specific remote project. +#### Add a remote -To add a remote repository to your local copy: +You add a "remote" to tell Git which remote repository in GitLab is tied +to the specific local folder on your computer. +The remote tells Git where to push or pull from. -1. In GitLab, [create a new project](../user/project/working_with_projects.md#create-a-project) to hold your files. +To add a remote to your local copy: + +1. In GitLab, [create a project](../user/project/working_with_projects.md#create-a-project) to hold your files. 1. Visit this project's homepage, scroll down to **Push an existing folder**, and copy the command that starts with `git remote add`. 1. On your computer, open the terminal in the directory you've initialized, paste the command you copied, and press <kbd>enter</kbd>: @@ -291,12 +265,22 @@ To add a remote repository to your local copy: After you've done that, you can [stage your files](#add-and-commit-local-changes) and [upload them to GitLab](#send-changes-to-gitlabcom). +#### View your remote repositories + +To view your remote repositories, type: + +```shell +git remote -v +``` + +The `-v` flag stands for verbose. + ### Download the latest changes in the project -To work on an up-to-date copy of the project (it is important to do this every time -you start working on a project), you `pull` to get all the changes made by users -since the last time you cloned or pulled the project. Use `master` for the -`<name-of-branch>` to get the main branch code, or the branch name of the branch +To work on an up-to-date copy of the project, you `pull` to get all the changes made by users +since the last time you cloned or pulled the project. Replace `<name-of-branch>` +with the name of your [default branch](../user/project/repository/branches/default.md) +to get the main branch code, or replace it with the branch name of the branch you are currently working in. ```shell @@ -305,189 +289,191 @@ git pull <REMOTE> <name-of-branch> When you clone a repository, `REMOTE` is typically `origin`. This is where the repository was cloned from, and it indicates the SSH or HTTPS URL of the repository -on the remote server. `<name-of-branch>` is usually `master`, but it may be any +on the remote server. `<name-of-branch>` is usually the name of your +[default branch](../user/project/repository/branches/default.md), but it may be any existing branch. You can create additional named remotes and branches as necessary. You can learn more on how Git manages remote repositories in the [Git Remote documentation](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes). -### View your remote repositories - -To view your remote repositories, type: - -```shell -git remote -v -``` - -The `-v` flag stands for verbose. +## Branches -## Branching +A **branch** is a copy of the files in the repository at the time you create the branch. +You can work in your branch without affecting other branches. When +you're ready to add your changes to the main codebase, you can merge your branch into +the default branch, for example, `main`. -If you want to add code to a project but you're not sure if it works properly, or you're -collaborating on the project with others, and don't want your work to get mixed up, it's a good idea -to work on a different **branch**. +Use branches when you: -When you create a branch in a Git repository, you make a copy of its files at the time of branching. You're free -to do whatever you want with the code in your branch without impacting the main branch or other branches. And when -you're ready to bring your changes to the main codebase, you can merge your branch into the default branch -used in your project (such as `master`). +- Want to add code to a project but you're not sure if it works properly. +- Are collaborating on the project with others, and don't want your work to get mixed up. A new branch is often called **feature branch** to differentiate from the -**default branch**. +[default branch](../user/project/repository/branches/default.md). ### Create a branch -To create a new feature branch and work from without affecting the `master` -branch: +To create a feature branch: ```shell git checkout -b <name-of-branch> ``` -Note that Git does **not** accept empty spaces and special characters in branch -names, so use only lowercase letters, numbers, hyphens (`-`), and underscores -(`_`). Do not use capital letters, as it may cause duplications. +Branch names cannot contain empty spaces and special characters. Use only lowercase letters, numbers, +hyphens (`-`), and underscores (`_`). -### Switch to the master branch +### Switch to a branch -You are always in a branch when working with Git. The main branch is the master -branch, but you can use the same command to switch to a different branch by -changing `master` to the branch name. +All work in Git is done in a branch. +You can switch between branches to see the state of the files and work in that branch. -```shell -git checkout master -``` - -### Work on an existing branch - -To switch to an existing branch, so you can work on it: +To switch to an existing branch: ```shell git checkout <name-of-branch> ``` -### View the changes you've made - -It's important to be aware of what's happening and the status of your changes. When -you add, change, or delete files/folders, Git knows about it. To check the status of -your changes: +For example, to change to the `main` branch: ```shell -git status +git checkout main ``` ### View differences -To view the differences between your local, unstaged changes and the repository versions -that you cloned or pulled, type: +To view the differences between your local unstaged changes and the latest version +that you cloned or pulled: ```shell git diff ``` -### Add and commit local changes +### View the files that have changes -Local changes are shown in red when you type `git status`. These changes may -be new, modified, or deleted files/folders. Use `git add` to first stage (prepare) -a local file/folder for committing. Then use `git commit` to commit (save) the staged -files: +When you add, change, or delete files or folders, Git knows about the changes. +To check which files have been changed: ```shell -git add <file-name OR folder-name> -git commit -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT" +git status ``` -#### Add all changes to commit +### Add and commit local changes + +When you type `git status`, locally changed files are shown in red. These changes may +be new, modified, or deleted files or folders. + +1. To stage a file for commit: + + ```shell + git add <file-name OR folder-name> + ``` + +1. Repeat step 1 for each file or folder you want to add. + Or, to stage all files in the current directory and subdirectory, type `git add .`. + +1. Confirm that the files have been added to staging: + + ```shell + git status + ``` -To add and commit (save) all local changes quickly: + The files should be displayed in green text. + +1. To commit the staged files: + + ```shell + git commit -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT" + ``` + +#### Stage and commit all changes + +As a shortcut, you can add all local changes to staging and commit them with one command: ```shell -git add . -git commit -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT" +git commit -a -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT" ``` -NOTE: -The `.` character means _all file changes in the current directory and all subdirectories_. - ### Send changes to GitLab.com -To push all local commits (saved changes) to the remote repository: +To push all local changes to the remote repository: ```shell git push <remote> <name-of-branch> ``` -For example, to push your local commits to the _`master`_ branch of the _`origin`_ remote: +For example, to push your local commits to the `main` branch of the `origin` remote: ```shell -git push origin master +git push origin main ``` -On certain occasions, Git disallows pushes to your repository, and then +Sometimes Git does not allow you to push to a repository. Instead, you must [force an update](../topics/git/git_rebase.md#force-push). -NOTE: -To create a merge request from a fork to an upstream repository, see the -[forking workflow](../user/project/repository/forking_workflow.md). - ### Delete all changes in the branch -To delete all local changes in the branch that have not been added to the staging -area, and leave unstaged files/folders, type: +To discard all changes to tracked files: ```shell git checkout . ``` -Note that this removes *changes* to files, not the files themselves. +This action removes *changes* to files, not the files themselves. +Untracked (new) files do not change. ### Unstage all changes that have been added to the staging area -To undo the most recently added, but not committed, changes to files/folders: +To unstage (remove) all files that have not been committed: ```shell -git reset . +git reset ``` ### Undo most recent commit -To undo the most recent commit, type: +To undo the most recent commit: ```shell git reset HEAD~1 ``` -This leaves the changed files and folders unstaged in your local repository. +This action leaves the changed files and folders unstaged in your local repository. WARNING: -A Git commit should not usually be reversed, particularly if you already pushed it +A Git commit should not be reversed if you already pushed it to the remote repository. Although you can undo a commit, the best option is to avoid the situation altogether by working carefully. -### Merge a branch with master branch +You can learn more about the different ways Git can undo changes in the +[Git Undoing Things documentation](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things). -When you are ready to make all the changes in a branch a permanent addition to -the master branch, you `merge` the two together: +### Merge a branch with default branch + +When you are ready to add your changes to +the default branch, you `merge` the two together: ```shell -git checkout <name-of-branch> -git merge master +git checkout <feature-branch> +git merge <default-branch> ``` +In GitLab, you typically use a [merge request](../user/project/merge_requests/) to merge your changes, instead of using the command line. + +To create a merge request from a fork to an upstream repository, see the +[forking workflow](../user/project/repository/forking_workflow.md). + ## Advanced use of Git through the command line For an introduction of more advanced Git techniques, see [Git rebase, force-push, and merge conflicts](../topics/git/git_rebase.md). ## Synchronize changes in a forked repository with the upstream -[Forking a repository](../user/project/repository/forking_workflow.md) lets you create -a copy of a repository in your namespace. Changes made to your copy of the repository -are not synchronized automatically with the original. -Your local fork (copy) contains changes made by you only, so to keep the project -in sync with the original project, you need to `pull` from the original repository. +To create a copy of a repository in your namespace, you [fork it](../user/project/repository/forking_workflow.md). +Changes made to your copy of the repository are not automatically synchronized with the original. +To keep the project in sync with the original project, you need to `pull` from the original repository. -You must [create a link to the remote repository](#add-a-remote-repository) to pull -changes from the original repository. It is common to call this remote the `upstream`. +You must [create a link to the remote repository](#add-a-remote) to pull +changes from the original repository. It is common to call this remote repository the `upstream`. You can now use the `upstream` as a [`<remote>` to `pull` new updates](#download-the-latest-changes-in-the-project) from the original repository, and use the `origin` |