git --version
sudo apt install git
If you want to use GitHub, use your username and email from there
git config --global user.name "cyprich"
git config --global user.email "cypooriginal@gmail.com
Makes git available for current directory
Shows the state of your project
Shows history of project
Shows the difference in changed files
Adds file to git
Makes file staged
Adds all png files
Adds all files
Allows you to git add more precisely, just some lines for example
It will show you a chunk of your code and you can do following:
y- yes - add this chunkn- no - do not add this chunks- split - split chunk into smaller piecese- edit - manually edit and pick lines
Removes added files
Removes files that already have been on git, but keeps them on device
Makes commit
Specifies commit message, so you don't have to deal with vim
Commits all changed files, you don't have to git add
Works only with changed files, if you create new file, you have to git add
Goes back to commit marked with <hash>
Goes back to actual (newest) version
Removes changes in <file>
Clones repository from GitHub to your machine
Note: this may cause problems because. See [[#Generate SSH key]] or follow this tutorial to fix problems
Pushes commit to the server
Pushes to master branch
Sets default value to origin master. Git will always push to origin master, unless you tell it to not
You don't have to use origin master anymore
Pulls commit from the server
Note: you may be asked, if you want to Merge or Rebase. For example, if you want to use Merge, type
git config pull.rebase false
Checks for changes on remote server. Now when you hit git status, you will see that you need to pull
Similar to example before, provides more information, without git status
Shows all branches of repo
Shows all branches of repo with additional info
Makes new branch with given <name>
Remove branch with given <name>
'Switches' to <name> branch
'Switches' to <name> branch, creates it, if not present
Moves changes from <name> to current branch
For example, if I hit git checkout master and then git merge new-feature, I just got the new-feature to master
| Merge | Rebase |
|---|---|
| Non-destructive | Destructive |
| You are keeping 2 branches | You are keeping only 1 branch |
| Parallel branches | Takes one branch and sticks it to the other |
Create a new repository on the command line
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/cyprich/MinecraftServer.git
git push -u origin master
Push existing repository from the command line
git remote add origin https://github.com/cyprich/MinecraftServer.git
git branch -M master
git push -u origin master
git branch -m main master
git fetch origin
git branch -u origin/master master
git remote set-head origin -a
To avoid typing your password or token every time you push to a Git repository, you can set up an SSH key
- Generate new SSH Key
ssh-keygen -t rsa -b 4096 -C cypooriginal@gmail.com
- Add the Key to GitHub account
- Copy public key (usually located at
~/.ssh/id_rsa.pub) - On GitHub go to Account Settings > SSH and GPG keys
- Click New SSH key and paste here the public key
- Copy public key (usually located at
- Change git remote URL from HTTPS to SSH
git remote set-url origin git@github.com:cyprich/repo.git
I changed my GitHub username, so I had to do this for every repo
git remote set-url origin git@github.com:cyprich/Obsidian.git
Once I had project on my GitHub and school GitLab, here's how I did it
# only change email locally on this repo
git config user.email "school.email@school.com"
# actually adding new repo
git remote add gitlab https://osmijanko.fri.uniza.sk/cyprich6/filamenty
# check with
git remote -v
# pushing to repo
git push origin master
git push gitlab master
# push to both repos automatically
git config push.default mathing