GitLab - Git Commands
GitLab - Essential Git Commands
Section titled “GitLab - Essential Git Commands”Introduction
Section titled “Introduction”While GitLab provides a web interface for many operations, Git itself is a command-line tool. Understanding basic Git commands is crucial for effective version control and collaboration using GitLab. These commands are run in your terminal or command prompt in your local project repository.
Initial Configuration (One-time setup)
Section titled “Initial Configuration (One-time setup)”Before you start using Git, configure your user name and email address. This information will be associated with your commits.
$ git config --global user.name "Your Name"$ git config --global user.email "youremail@example.com"To check your configuration:
$ git config --global --listCheck Git version:
$ git --versionWorking with Repositories
Section titled “Working with Repositories”Clone an existing repository from GitLab:
$ git clone <repository_url>Initialize a new Git repository in an existing directory:
$ git initBranching and Merging
Section titled “Branching and Merging”List all local branches (current branch is marked with an asterisk):
$ git branchCreate a new branch:
$ git branch <new-branch-name>Switch to a branch (modern Git recommends git switch):
$ git switch <branch-name># Older command:# $ git checkout <branch-name>Create a new branch and switch to it immediately:
$ git switch -c <new-branch-name># Older command:# $ git checkout -b <new-branch-name>Merge changes from another branch into your current branch (e.g., merging feature-branch into main):
$ git switch main$ git merge feature-branchDelete a local branch (safely, only if merged):
$ git branch -d <branch-name>Force delete a local branch (use with caution):
$ git branch -D <branch-name>Making Changes
Section titled “Making Changes”Check the status of your working directory and staging area:
$ git statusAdd file changes to the staging area:
$ git add <file-name> # Add a specific file$ git add . # Add all changes in the current directory and subdirectoriesCommit staged changes to your local repository:
$ git commit -m "Your descriptive commit message"View commit history:
$ git log$ git log --oneline --graph --decorate # A more concise viewSynchronizing with GitLab (Remote Repository)
Section titled “Synchronizing with GitLab (Remote Repository)”Note: origin is the default name for your remote repository. The primary branch is often named main (modern) or master (older).
Fetch changes from the remote repository (doesn’t merge):
$ git fetch originPull changes from the remote repository (fetches and merges/rebases):
$ git pull origin <branch-name># Example: git pull origin mainIf your local branch is tracking a remote branch, you can often just use:
$ git pullPush your local commits to the remote repository:
$ git push origin <branch-name># Example: git push origin feature-branchTo set the upstream branch for the first push (so future git push and git pull can be run without arguments):
$ git push -u origin <branch-name>Undoing Changes
Section titled “Undoing Changes”Discard changes in a specific file in your working directory (modern Git recommends git restore):
$ git restore <file-name># Older command:# $ git checkout -- <file-name>Unstage a file (remove from staging area but keep changes):
$ git restore --staged <file-name># Older command:# $ git reset HEAD <file-name>Amend the last commit (change message or add more changes):
$ git commit --amendCaution: Avoid amending commits that have already been pushed to a shared remote branch.
Revert a commit (creates a new commit that undoes the changes of a previous commit):
$ git revert <commit-hash>Reset your current branch to a previous commit (can lose data, use with care):
$ git reset --hard <commit-hash> # Discards all changes since that commit$ git reset --soft <commit-hash> # Keeps changes stagedRemove untracked files from your working directory (DANGEROUS - cannot be undone):
$ git clean -f # Remove untracked files$ git clean -fd # Remove untracked files and directoriesFurther Learning
Section titled “Further Learning”This list covers many common Git commands, but Git is a powerful tool with many more features. For further learning, refer to the official Git documentation (git-scm.com) or interactive tutorials available online.