Git
What is Git?
Git writes each save as a point called a commit. A commit is not a full copy of the project. It is the difference from the previous state. The history stays on your computer. GitHub and GitLab are sites for sharing that history. They are not Git itself.
What is Git used for?
Git is useful even when one person works. It is for going back from a bad change and for taking someone else's code without mixing it by hand.
- Going back before a bad commit
- Keeping two ideas on separate branches
- Sending only finished code to a server
- Touching the same file as a team
What can you do with Git?
With Git you track a folder, choose a change and commit it, open a branch, and send it to a remote address. You can look at an old commit and return a file to that day's state. Git itself does not start a site and does not check the code.
Is Git hard to learn?
The first three commands are easy: status, add, commit. The difficulty starts with a branch, a merge, and a conflict. When two people change the same line, Git does not pick the right version by itself. A secret, a password, and a huge file should not go into a commit.
Advantages and limits
Advantage: the history stays, a mistake can be undone, and several people work on one project. Limit: Git is not a backup service and it is not the site on the server. A commit message should be short and plain, or the history cannot be read.
Basic ideas
The working folder, the commit, and the remote address are three separate places. A branch is a line that splits from the same history.
- status shows what changed
- add puts a chosen file into the next commit
- commit creates a point with a message
- push sends that point to the remote address
A short example
These three commands look at the state, choose the files, and save them with a short message. Write the message for your own change.
git status git add . git commit -m "First note"
Working examples
The three lines above save everything at once. These examples do a smaller job and say what the screen should show. Run them in a folder that is already a Git project. If git status says this is not a repository, the folder has not been started with git init.
Save one file, not the whole folder
status lists what changed and what Git is not following yet. add note.txt chooses only that file for the next save. Other changed files stay out. commit writes the point. The text in quotes is the message. It should say what you did, not just update. If you write git add . instead, every change in the folder goes into the same point.
git status git add note.txt git commit -m "Add the note"
After the commit, git status says the working tree is clean. note.txt is in the history. A second file you did not add is not.
See the changed lines before you save
diff shows the difference between the file now and the last commit. A line that starts with + was added. A line that starts with - was removed. This is not a commit. Nothing is saved yet. If you changed nothing, the command prints nothing. Read this before add, so you do not save a password or a half-written line by mistake.
git diff
The screen is a comparison, not a new point in history. Closing the terminal does not undo the edit in the file.
Read the history in short lines
log lists the commits. --oneline puts each one on a single line. The latest commit is at the top. The short word at the start is the id. You use that id when you need to look at an old point. The message is the text you wrote in commit. If the messages are all 'update', the list does not tell you what changed.
git log --oneline
You see one line per save. The top line is the last commit. Older ones sit under it.
Send your step, and take someone else's
push sends the commits on your computer to the address you connected, often GitHub. pull brings commits that are on that address but not on your machine. They are not the same command. push does not download a teammate's new work. pull does not upload yours. If pull stops and talks about a conflict, the same line was changed in both places. Git does not choose the right line. You do.
git push git pull
After a clean push, the site has your latest commit. After a clean pull, your folder has the commits that were only on the site.
Common questions
- Are Git and GitHub the same? No. Git is the tool. GitHub is a site that stores that history.
- Is Git only for code? It is mainly for text files. It is a poor path for images and video.
- Is every save a commit? No. First you choose the file, then you write the commit.