Difference between revisions of "Git"
m (→File tracking Lifecycle) |
m (→Problems) |
||
(29 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | =What is git= | ||
+ | Git is a is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers. | ||
+ | [[Image:Git-basic.png|400px|thumb|center|Basic flow of files in git]] | ||
+ | {|border=1 ;style="margin: 0 auto; text-align: center;cellpadding="5" cellspacing="0" | ||
+ | |- bgcolor=lightgrey | ||
+ | !Area!!Explanation | ||
+ | |- | ||
+ | |Working directory|| Typically the project homefolder | ||
+ | |- | ||
+ | |Staging area|| Files "staged" to go into your next commit with the '''git add''' command | ||
+ | |- | ||
+ | |Repository|| The database and history of committed project files. '''git commit''' command | ||
+ | |- | ||
+ | |} | ||
=Install git= | =Install git= | ||
Download git from https://git-scm.com/download/win and install it. | Download git from https://git-scm.com/download/win and install it. | ||
+ | Learn basic git by watching this video tutorial: [https://learning.oreilly.com/videos/-/9781789348231/ Learn git in 3 hours] | ||
+ | { | ||
=Basic configuration= | =Basic configuration= | ||
From terminal issue the following commands: (Windows start the '''git bash''' app) | From terminal issue the following commands: (Windows start the '''git bash''' app) | ||
<source lang=bash> | <source lang=bash> | ||
− | + | git config --global user.name "Henrik Thomsen" | |
− | + | git config --global user.email "heth@mercantec.dk" | |
+ | </source> | ||
+ | It will create a '''~/.gitconfig''' file for the user | ||
+ | ==Create a new git project== | ||
+ | <source lang=bash> | ||
+ | mkdir project | ||
+ | cd project | ||
+ | git init | ||
</source> | </source> | ||
+ | git init will create a '''.git''' directory | ||
+ | == Create ssh keys for authentication== | ||
+ | Add ssh authentication keys on Linux host and copy public key to git, then | ||
+ | git remote set-url origin git@github.com:username/your-repository.git | ||
+ | |||
=File tracking states= | =File tracking states= | ||
A specified file can be in one of four states | A specified file can be in one of four states | ||
Line 16: | Line 44: | ||
|Untracked||The file is '''not''' tracked by git and changes are not recorded | |Untracked||The file is '''not''' tracked by git and changes are not recorded | ||
|- | |- | ||
− | |Unmodified||The file is tracked by git and | + | |Unmodified||The file is tracked by git and has not changed since it's last '''commit''' |
|- | |- | ||
|Modified||The file is tracked by git and has changed since it's last '''commit''' | |Modified||The file is tracked by git and has changed since it's last '''commit''' | ||
Line 23: | Line 51: | ||
|- | |- | ||
|} | |} | ||
+ | |||
=File tracking lifecycle= | =File tracking lifecycle= | ||
− | [[Image:Tracking Files Learn Git in 3 Hours.png|500px|center| | + | [[Image:Tracking Files Learn Git in 3 Hours.png|500px|center|thumb| Files can change state issuing '''git''' commands]] |
+ | |||
+ | =Basic git commands= | ||
+ | {|border=1 ;style="margin: 0 auto; text-align: center;cellpadding="5" cellspacing="0"; | ||
+ | |- bgcolor=lightgrey | ||
+ | ! command !! Explanation | ||
+ | |- | ||
+ | |git status||Status of git repository (must be in folder project or project subfolder) | ||
+ | |- | ||
+ | |style="vertical-align:top;|git add <file/dir>... | ||
+ | |Files added to the repository - '''Untracked->staged''' or '''Modified->staged''' | ||
+ | '''Note:''' When adding a directory - the directory is not tracked for new files | ||
+ | |- | ||
+ | |git diff [file...]||See modified files content - files not staged (Use '''git add''' to stage) | ||
+ | |- | ||
+ | |git diff --staged [file...]||See staged files modified but not committed | ||
+ | |- | ||
+ | |git commit||Interactive: Commit staged files to repository - Write a comment in the editor and exit | ||
+ | |- | ||
+ | |git commit -v||Same as above, but shows you the differences | ||
+ | |- | ||
+ | |git commit -m TXT|| Commits directly with the commit TXT | ||
+ | |- | ||
+ | |git commit -a -m TXT || add all changed files to the staging area and commit (Most used) | ||
+ | |- | ||
+ | |git commit --amend||Edit last commit - add files before etc.... | ||
+ | |- | ||
+ | |git revert <HASH> || Revert existing commit(s) | ||
+ | |- | ||
+ | |git gui||A portable graphical interface to Git (Good diff tool) | ||
+ | |- | ||
+ | | git log || <nowiki>Show commit logs many options (git log [--help|-n INT|-p|--pretty=oneline|--since "5 days"|--graph])</nowiki> | ||
+ | |- | ||
+ | | git pull || Fetch from and integrate with another repository or a local branch. | ||
+ | |- | ||
+ | |- | ||
+ | |} | ||
+ | == .gitignore file== | ||
+ | To ignore fx. build files or logfiles create a '''.gitignore''' file in the repository home directory. Content fx. '''*.log''' | ||
+ | Pattern matching | ||
+ | * Asterisk matches zero or more characters '''*''' | ||
+ | * Patterns starting with a slash '''/''' matches in the same name as the '''.gitignore''' file | ||
+ | *Patterns ending with a slash '''/''' only match folder names | ||
+ | *Exclamation mark '''!''' negates a pattern: fx. '''!henrik.log''' includes '''henrik.log''' even if '''*.log''' is ignored | ||
+ | *Nested directories: '''/**/*.c''' Will include all '''*.c''' files regardless of number if subdirectories | ||
+ | *'''See:''' https://github.com/github/gitignore for examples of '''.gitignore''' project files | ||
=Why is it called Git?= | =Why is it called Git?= | ||
Linus Torvalds has quipped about the name "git", which is British English slang for a stupid or unpleasant person. Torvalds said: "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."<ref>http://www.infoworld.com/article/05/04/19/HNtorvaldswork_1.html</ref><ref> https://git.wiki.kernel.org/articles/g/i/t/GitFaq_ebc3.html#Why_the_.27git.27_name.3F </ref><ref>http://www.pcworld.idg.com.au/article/129776/after_controversy_torvalds_begins_work_git_</ref> | Linus Torvalds has quipped about the name "git", which is British English slang for a stupid or unpleasant person. Torvalds said: "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."<ref>http://www.infoworld.com/article/05/04/19/HNtorvaldswork_1.html</ref><ref> https://git.wiki.kernel.org/articles/g/i/t/GitFaq_ebc3.html#Why_the_.27git.27_name.3F </ref><ref>http://www.pcworld.idg.com.au/article/129776/after_controversy_torvalds_begins_work_git_</ref> | ||
+ | =Problems= | ||
+ | == push fails due to repo has new commits pushed == | ||
+ | If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using: | ||
+ | <source lang=bash> | ||
+ | git pull --rebase | ||
+ | git push | ||
+ | </source> | ||
+ | The full syntax is: | ||
+ | <source lang=bash> | ||
+ | git pull --rebase origin main | ||
+ | git push origin main | ||
+ | </source> | ||
+ | ==Add Linux client== | ||
+ | ===Generate an SSH Key Pair=== | ||
+ | First, you'll need to generate a new SSH key pair. | ||
+ | |||
+ | *Open a terminal. | ||
+ | *Generate the key using the following command: | ||
+ | <source lang=bash> | ||
+ | ssh-keygen -t ed25519 -C "your_email@example.com" | ||
+ | </source> | ||
+ | |||
+ | If you're using an older system that doesn't support `ed25519`, use `rsa`: | ||
+ | |||
+ | <source lang=bash> | ||
+ | ssh-keygen -t rsa -b 4096 -C "your_email@example.com" | ||
+ | </source> | ||
+ | *When prompted, specify a file to save the key, or press Enter to use the default location (`~/.ssh/id_ed25519`). | ||
+ | *Set a passphrase for added security, or press Enter to leave it empty for no passphrase. | ||
+ | |||
+ | === Add SSH Key to the SSH Agent=== | ||
+ | *Ensure the SSH agent is running: | ||
+ | <source lang=bash> | ||
+ | eval "$(ssh-agent -s)" | ||
+ | </source> | ||
+ | *Add your SSH private key to the SSH agent: | ||
+ | <source lang=bash> | ||
+ | ssh-add ~/.ssh/id_ed25519 | ||
+ | </source> | ||
+ | |||
+ | ===Add Your SSH Key to GitHub=== | ||
+ | *Copy the SSH key to your clipboard: | ||
+ | <source lang=bash> | ||
+ | cat ~/.ssh/id_ed25519.pub | ||
+ | </source> | ||
+ | *Open GitHub in your browser. | ||
+ | *Go to ''Settings'' > ''SSH and GPG keys''. | ||
+ | *Click on ''New SSH key''. | ||
+ | *Paste your key into the "Key" field. | ||
+ | *Click ''Add SSH key''. | ||
+ | |||
+ | ===Configure Git to Use SSH=== | ||
+ | *Change your repository's remote URL to use SSH: | ||
+ | <source lang=bash> | ||
+ | git remote set-url origin git@github.com:username/repo.git | ||
+ | </source> | ||
+ | Replace `username` and `repo` with your GitHub username and the repository name. | ||
+ | |||
+ | ===Testing the Configuration=== | ||
+ | Test your SSH connection: | ||
+ | <source lang=bash> | ||
+ | ssh -T git@github.com | ||
+ | </source> | ||
+ | You should see a message like "Hi username! You've successfully authenticated." | ||
+ | |||
+ | You should now be set up to push changes to GitHub using SSH certificates. If you encounter any issues, verify that your SSH key is correctly added to the SSH agent and that it’s also correctly associated with your GitHub account. | ||
=References= | =References= |
Latest revision as of 09:18, 5 July 2025
Contents
What is git
Git is a is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers.
Area | Explanation |
---|---|
Working directory | Typically the project homefolder |
Staging area | Files "staged" to go into your next commit with the git add command |
Repository | The database and history of committed project files. git commit command |
Install git
Download git from https://git-scm.com/download/win and install it.
Learn basic git by watching this video tutorial: Learn git in 3 hours {
Basic configuration
From terminal issue the following commands: (Windows start the git bash app)
git config --global user.name "Henrik Thomsen"
git config --global user.email "heth@mercantec.dk"
It will create a ~/.gitconfig file for the user
Create a new git project
mkdir project
cd project
git init
git init will create a .git directory
Create ssh keys for authentication
Add ssh authentication keys on Linux host and copy public key to git, then
git remote set-url origin git@github.com:username/your-repository.git
File tracking states
A specified file can be in one of four states
State | Explanation |
---|---|
Untracked | The file is not tracked by git and changes are not recorded |
Unmodified | The file is tracked by git and has not changed since it's last commit |
Modified | The file is tracked by git and has changed since it's last commit |
Staged | means that you have marked a modified file in its current version to go into your next commit snapshot. |
File tracking lifecycle
Basic git commands
command | Explanation |
---|---|
git status | Status of git repository (must be in folder project or project subfolder) |
git add <file/dir>... | Files added to the repository - Untracked->staged or Modified->staged
Note: When adding a directory - the directory is not tracked for new files |
git diff [file...] | See modified files content - files not staged (Use git add to stage) |
git diff --staged [file...] | See staged files modified but not committed |
git commit | Interactive: Commit staged files to repository - Write a comment in the editor and exit |
git commit -v | Same as above, but shows you the differences |
git commit -m TXT | Commits directly with the commit TXT |
git commit -a -m TXT | add all changed files to the staging area and commit (Most used) |
git commit --amend | Edit last commit - add files before etc.... |
git revert <HASH> | Revert existing commit(s) |
git gui | A portable graphical interface to Git (Good diff tool) |
git log | Show commit logs many options (git log [--help|-n INT|-p|--pretty=oneline|--since "5 days"|--graph]) |
git pull | Fetch from and integrate with another repository or a local branch. |
.gitignore file
To ignore fx. build files or logfiles create a .gitignore file in the repository home directory. Content fx. *.log Pattern matching
- Asterisk matches zero or more characters *
- Patterns starting with a slash / matches in the same name as the .gitignore file
- Patterns ending with a slash / only match folder names
- Exclamation mark ! negates a pattern: fx. !henrik.log includes henrik.log even if *.log is ignored
- Nested directories: /**/*.c Will include all *.c files regardless of number if subdirectories
- See: https://github.com/github/gitignore for examples of .gitignore project files
Why is it called Git?
Linus Torvalds has quipped about the name "git", which is British English slang for a stupid or unpleasant person. Torvalds said: "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."[1][2][3]
Problems
push fails due to repo has new commits pushed
If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:
git pull --rebase
git push
The full syntax is:
git pull --rebase origin main
git push origin main
Add Linux client
Generate an SSH Key Pair
First, you'll need to generate a new SSH key pair.
- Open a terminal.
- Generate the key using the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
If you're using an older system that doesn't support `ed25519`, use `rsa`:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- When prompted, specify a file to save the key, or press Enter to use the default location (`~/.ssh/id_ed25519`).
- Set a passphrase for added security, or press Enter to leave it empty for no passphrase.
Add SSH Key to the SSH Agent
- Ensure the SSH agent is running:
eval "$(ssh-agent -s)"
- Add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519
Add Your SSH Key to GitHub
- Copy the SSH key to your clipboard:
cat ~/.ssh/id_ed25519.pub
- Open GitHub in your browser.
- Go to Settings > SSH and GPG keys.
- Click on New SSH key.
- Paste your key into the "Key" field.
- Click Add SSH key.
Configure Git to Use SSH
- Change your repository's remote URL to use SSH:
git remote set-url origin git@github.com:username/repo.git
Replace `username` and `repo` with your GitHub username and the repository name.
Testing the Configuration
Test your SSH connection:
ssh -T git@github.com
You should see a message like "Hi username! You've successfully authenticated."
You should now be set up to push changes to GitHub using SSH certificates. If you encounter any issues, verify that your SSH key is correctly added to the SSH agent and that it’s also correctly associated with your GitHub account.