Git Code Review
Git Code Review
Git Code Review
You can determine if Git is already installed on your computer by opening a terminal and
running this command:
git --version
If your computer doesn’t recognize git as a command, you must install Git.
Configure Git
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.
1. In your shell, add your user name:
You can read more on how Git manages configurations in the Git configuration
documentation.
Choose a repository
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.
Clone a repository
When you clone a repository, the files from the remote repository are downloaded to
your computer, and a connection is created.
This connection requires you to add credentials. You can either use SSH or HTTPS. SSH
is recommended.
cd sample-project
You can also clone a repository and open it directly in Visual Studio Code.
cd sample-project
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.
git init
3. A .git folder is created in your directory. This folder contains Git records and
configuration files. You should not edit these files directly.
4. Add the path to your remote repository so Git can upload your files into the
correct project.
Add a remote
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.
After you’ve done that, you can stage your files and upload them to GitLab.
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 the name of your default branch, 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.
Branches
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.
A new branch is often called feature branch to differentiate from the default branch.
Create a branch
To create a feature branch:
git checkout -b <name-of-branch>
Branch names cannot contain empty spaces and special characters. Use only
lowercase letters, numbers, hyphens (-), and underscores (_).
Switch to a branch
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.
2. 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 ..
3. Confirm that the files have been added to staging:
git status
For example, to push your local commits to the main branch of the origin remote:
git push origin main
Sometimes Git does not allow you to push to a repository. Instead, you must force an
update.
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 unstage (remove) all files that have not been committed:
git reset
This action leaves the changed files and folders unstaged in your local repository.
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.
You can learn more about the different ways Git can undo changes in the Git Undoing
Things documentation.
In GitLab, you typically use a merge request to merge your changes, instead of using the
command line.