Working with Git on the command line (Linux)

Git basics

Simple instruction how to handle git on the command line

Check if Git is installed and ready

git --version

If not, you can install git using (on Ubuntu)

sudo apt install git

Set username and email

Every commit is marked with username and email to identify the autor

git config --global user.name "YOUR_USERNAME"
git config --global user.email "your_email_address@example.com"

You can check the informations using

git config --global --list

(1) Initialize a local directory for Git

If you want to include your local directory to version control, use init to tell Git what to track

git init

.git directory is created including configuration files

(2) Clone a repo

If you want to start working on some existing project, you can clone it (copy) to your local machine. You can use HTTPS or SSH. Using HTTPS you have to put credentials every time, using SSH only first time.
HTTPS:

git clone https://gitlab.com/some-repo/

SSH:

git clone git@gitlab.com:some-repo

Switching branches

git checkout master

Where master is the branch name

Download the latest changes in the project

git pull

Where remote is usually origin and branch is your target branch (master or something else).

View remote repos

git remote -v

Add a remote repo

git remote add

Where name is your defined source name.

Create a branch

git checkout -b

Switch to branch

git checkout

View changes

git status

View differences

git version

Add and commit local changes

git add 
git commit -m "Comment to describe the commit"

Note: Folders with no files inside are not added to Git

Add all changes to commit

git add .
git commit -m "Comment to describe the commit"

. means all in Git

Push changes

git push

Where remote is usually origin and branch is your target branch (master or something else).

git push origin master

Delete all local changes

git checkout .

Undo most recent commit

git reset HEAD~1

But better is to create another commit 🙂

Merge branches

git checkout 
git merge master

Merge branch-name to master

Leave a Reply

Your email address will not be published. Required fields are marked *