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
1 |
git --version |
If not, you can install git using (on Ubuntu)
1 |
sudo apt install git |
Set username and email
Every commit is marked with username and email to identify the autor
1 2 |
git config --global user.name "YOUR_USERNAME" |
You can check the informations using
1 |
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
1 |
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:
1 |
git clone https://gitlab.com/some-repo/ |
SSH:
1 |
git clone git@gitlab.com:some-repo |
Switching branches
1 |
git checkout master |
Where master
is the branch name
Download the latest changes in the project
1 |
git pull |
Where remote is usually origin
and branch is your target branch (master or something else).
View remote repos
1 |
git remote -v |
Add a remote repo
1 |
git remote add |
Where name is your defined source name.
Create a branch
1 |
git checkout -b |
Switch to branch
1 |
git checkout |
View changes
1 |
git status |
View differences
1 |
git version |
Add and commit local changes
1 2 |
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
1 2 |
git add . git commit -m "Comment to describe the commit" |
.
means all in Git
Push changes
1 |
git push |
Where remote is usually origin
and branch is your target branch (master or something else).
1 |
git push origin master |
Delete all local changes
1 |
git checkout . |
Undo most recent commit
1 |
git reset HEAD~1 |
But better is to create another commit 🙂
Merge branches
1 2 |
git checkout git merge master |
Merge branch-name
to master