Install Git #
Linux (Debian based) #
# Install Git
sudo apt install git -y
Windows (Winget) #
# Install Git (Run PowerShell as Administrator)
winget install Git.Git
# Check Git version (Close and open a new PowerShell)
git --version
Git Commandline #
Overview #
Note: The branch former called “main” is now called “master” branch.
Git Version & Help #
# Check Git Version
git --version
# List the available Git help topics
git help -a
# Quit help topics list
q
# List help for specific git command: For example "add"
git help add
Prerequisites: Configure User & Email #
Set the username and email address that will be associated with any commits you create across all Git repositories on your computer:
# Define your name
git config --global user.name "Your Name"
# Define youremail
git config --global user.email "you@example.com"
Clone Repository #
# Clone Repository: Syntax
git clone rego-url
# Clone Repository: Example
git clone git@gitlab.jklug.work:root/my-git-project.git
# CD into Repository: Syntax
cd repository-name
# CD into Repository: Example
cd my-git-project
Branches #
List Branches: Local & Remote #
# List available branches and current branch: Local
git branch
# Shell output:
* main
# List available branches: Remote
git branch -r
# Shell output:
origin/HEAD -> origin/main
origin/main
# List available branches: Local & remote
git branch -a
# Shell output:
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
Create Local Branch #
# Create a new "dev" branch
git switch --create dev
# Shell output:
Switched to a new branch 'dev'
# Verify current branch
git branch
# Shell output:
* dev
main
Switch Branch #
# Manually switch to dev branch
git switch main
# Shell output:
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Note: The git checkout
is an older command and has multiple purposes, which can make it less intuitive.
Push Local Branch to Remote Repository #
For example bush the local “dev” branch:
# Switch to local dev branch
git switch dev
# Push local branch and establish tracking relationship between local branch and it's remote counterpart
git push -u origin dev
# Shell output:
To gitlab.jklug.work:root/my-git-project.git
* [new branch] dev -> dev
branch 'dev' set up to track 'origin/dev'.
Checkout specific Remote Branch #
# Fetch remote branches
git fetch origin
# List all branches
git branch -a
# Shell output:
* main
remotes/origin/HEAD -> origin/main
remotes/origin/dev
remotes/origin/main
# To create a local branch tracking the remote dev
git checkout -b dev origin/dev
# Verify current branch
git branch
# Shell output:
* dev
main
Checkout a new Remote Branch #
-
Create a new branch in the remote repository via the GitLab GUI, for example “production”.
-
To see the newly created branch in the GUI, refresh the page.
# Fetch all branches from remote repository
git fetch
# Shell output:
From gitlab.jklug.work:root/my-git-project
* [new branch] production -> origin/production
# Verify the available branches
git branch
# Shell output:
* dev
main
production
# Switch to production branch
git switch production
Status, Add, Commit, Push & Pull #
Status #
Add a new file to the local branch, for example production-file2
# List untracked files: Lists files in the working directory that are not tracked by Git
git status
# Shell output:
Untracked files:
(use "git add <file>..." to include in what will be committed)
production-file2
Stage Files & Changes: Add #
# Stage specific file: For example "production-file2"
git add production-file2
# Stage all changes: New files, modifications, and deletions in the current directory and its subdirectories
git add .
# Stages changes to files that are already being tracked by Git
git add -u
Commit Changes #
# Commit Changes
git commit -m "Added file"
# Shell output:
git commit -m "Added file"
Push Changes #
Default Remote Repository:
# Push commits from the current local branch to its configured upstream branch.
git push
Define Remote Repository:
-
Pushes commits from the current local branch to a specific remote branch on the origin remote repository, regardless of the current tracking configuration.
-
First-time push without upstream configuration: This sets up the production branch on the remote repository and syncs the local branch to it
# Push the commits from local branch to the remote repository: Define remote repository
git push origin remote-branchname
Pull Changes #
# Pull latest changes from the remote repository
git pull
pull
: Fetches the latest changes from the remote branch, merges those changes into your current branch
Merge Branches #
For example merge the “dev” branch into “production”:
# Switch to the "production" branch (The branch where the changes should be merged)
git switch production
# Merge the "dev" branch into "production"
git merge dev -m "Add some feature"
# Push commits from the current local branch to its configured upstream branch.
git push
Commit History: Log #
# List commit history: Specific branch
git log branchname
# List commit history: All branches
git log --all
Shorter Log Output:
# List commit history: Specific branch
git log dev --oneline
# Shell output:
8e4c33b (origin/dev, dev) added files
034eb07 added dev file
9aedf5f (origin/main, origin/HEAD, main) Initial commit
Reset to Specific Commit #
Identify the Commit Hash #
# List commit history: Specific branch
git log dev --oneline
# Shell output:
8e4c33b (origin/dev, dev) added files
034eb07 added dev file
9aedf5f (origin/main, origin/HEAD, main) Initial commit
Ensure the Correct Branch #
# Make sure you're on the branch you want to reset
git switch dev
Reset to the Specific Commit #
# Reset the current branch to the specified state
git reset --hard 034eb07
Verify the Reset #
Check the commit history to confirm that the reset was successful:
- The commits after 034eb07 should no longer appear
# List commit history: Specific branch
git log dev --oneline
# Shell output:
034eb07 (HEAD -> dev) added dev file
9aedf5f (origin/main, origin/HEAD, main) Initial commit
Push the Reset Branch to the Remote Repository #
If the previously created commit was already pushed to the remote repository and the remote branch should be updates, the push needs the --force
option:
# Commit to remote repository
git push --force origin dev
Change Commit Message #
Change Latest Commit Message #
Change the previously made commit message, for example:
# Example: Original commit message
git commit -m "some-message"
# Fix the commit message
git commit --amend -m "fixed some-message"
Verify the commit message:
# List commit history: Specific branch
git log dev --oneline
# Shell output:
8e15505 (HEAD -> dev) fixed some-message
034eb07 (origin/dev) added dev file
9aedf5f (origin/main, origin/HEAD, main) Initial commit
Change Previously Made Commit Message #
Identify the Commit Hash:
# List commit history: Specific branch
git log dev --oneline
# Shell output:
8e15505 (HEAD -> dev) fixed some-message
034eb07 (origin/dev) added dev file
9aedf5f (origin/main, origin/HEAD, main) Initial commit
Start from the previously made commit:
# Change commit text
git rebase -i HEAD~2
# Shell output:
pick 034eb07 added dev file
pick 8e15505 fixed some-message
# Rebase 9aedf5f..8e15505 onto 9aedf5f (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
# to this position in the new commits. The <ref> is
# updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
-
Change “pick” to “reword” but not the commit message
-
Press
i
for input (VIM editor)
# Change the previously commit message
reword 034eb07 added dev file
pick 8e15505 fixed some-message
-
Press
Esc
and:wq
-
Change the actual commit message (A new editor opens)
# Change the commit message: For example as follows
fixed added dev file and some other files
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Nov 20 10:55:53 2024 +0100
#
# interactive rebase in progress; onto 9aedf5f
# Last command done (1 command done):
# reword 77a6136 added dev file
# Next command to do (1 remaining command):
# pick cea461a fixed some-message
# You are currently editing a commit while rebasing branch 'dev' on '9aedf5f'.
#
# Changes to be committed:
# new file: dev-file
#
-
Press
Esc
and:wq
-
Verify the commit message
# List commit history: Specific branch
git log dev --oneline
# Shell output:
3e927d5 (HEAD -> dev) fixed some-message
d50153e fixed added dev file and some other files
9aedf5f (origin/main, origin/HEAD, main) Initial commit
More #
Open File in VSCode #
# Open file in Visual Studio Code (Save file / changes)
code .\filename
Check Branch Tracking Configuration #
List wich local branch is tracking a remote branch
# List branch tracking
git branch -vv
# Shell output:
dev 034eb07 [origin/dev] added dev file
main 9aedf5f [origin/main] Initial commit
* production 4e35e51 [origin/production] Added file
Line Ending Conversion #
# List Git automatic line ending conversion status
git config --get core.autocrlf
# Set Git automatic line ending conversion: Off
git config --global core.autocrlf false
Init new local Project #
# Create new Project in GitLab Webinterface
https://gitlab.com/jueklug/my-repository
# Create Folder for Repository
mkdir my-repository
# CD into Folder
cd my-repository
# Initialize Git Repository
git init
# Connect your local repository to the remote repository (SSH)
git remote add origin git@gitlab.com:jueklug/my-repository.git
Optional: Add something to push
# Create file
touch readme.md
# Add specific Changes
git add readme.md
# Commit Changes
git commit -m "Added readme.md"
Push
# First time push
# Associate your local master branch with the master branch on the origin remote
git push -u origin master
# Push Commits to Remote Repository
git push origin master