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.
To execute Git commands in your computer, you’ll need to open a command shell. Here are some suggestions:
- For macOS users:
- For Windows users:
- For Linux users:
- Built-in: Linux Terminal.
Run following command to check you have already installed Git in your system.
If you have Git installed, the output will be:
git version X.Y.Z
If you not have installed Git. Please install it.
To start using Git from your computer, you’ll need to 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.
In your shell, add your user name:
git config --global user.name "your_username"
And your email address:
git config --global user.email "email@example.com"
To check the configuration, run:
git config --global --list
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 will be applied only to the current repository.
Basic Git commands
Initialize a local directory for Git version control
If you have an existing local directory that you want to initialize for version control, use the init command to instruct Git to begin tracking the directory:
This creates a .git directory that contains the Git configuration files.
Once the directory has been initialized, you can add a remote repository and send changes to GitLab.com. You will also need to create a new project in GitLab for your Git repository.
Clone a repository
To start working locally on an existing remote repository, clone it with the command git clone <repository path>. By cloning a repository, you’ll download a copy of its files to your local computer, automatically preserving the Git connection with the remote repository.
As an example, consider this repository path:
Clone via HTTPS
git clone https://gitlab.com/gitlab-org/gitlab.git
Clone via SSH
git clone firstname.lastname@example.org:gitlab-org/gitlab.git
Switch to the master 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.
git checkout master
Download the changes from repository
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 you are currently working in.
View your remote repositories
To view your remote repositories, type:
git remote -v
The -v flag stands for verbose.
Add a remote repository
To add a link to a remote repository:
git remote add
List all available Branches
Theres are three command to get branches. every command have own different role
The command to list all branches in local and remote repositories is:
git branch -a
If you require only listing the remote branches from Git Bash then use this command:
git branch -r
You may also use the show-branch command for seeing the branches and their commits as follows:
Create a branch
git checkout -b <name-of-branch>
Work on an existing branch
To switch to an existing branch, so you can work on it:
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:
To view the differences between your local, unstaged changes and the repository versions that you cloned or pulled, type:
Add and commit local changes
You’ll see any local changes 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:
git commit -m “COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT”
Add all changes to commit
To add and commit (save) all local changes quickly:
git add . git commit -m "COMMENT TO DESCRIBE THE INTENTION OF THE COMMIT"
Send changes to GitLab.com
To push all local commits (saved changes) to the remote repository:
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:
git checkout .
Unstage all changes that have been added to the staging area
To undo the most recently added, but not committed, changes to files/folders:
git reset .
Undo most recent commit
git reset HEAD~1
Merge a branch with master branch
When you are ready to make all the changes in a branch a permanent addition to the master branch, you merge the two together:
git checkout git merge master