This entire page is a 5 minute read.
This is a beginner reference/tutorial for git. I will include only things that are necessary for the user to start using git. I will avoid mentioning complex topics or scenarios.
After reading this entire document, the user should be ready to start using git locally, and should be able to contribute to remote repositories as well.
git stores your project's entire history, all its branches and commits, in a hidden .git directory at the root of your project. This directory contains everything git needs to track your project.
This is your working directory, your project folder, which holds all your project files in their current state.
The staging area is a step between your working tree and the git repository. When you add new files to your working tree, or make changes to existing files that are being tracked, git doesn't automatically absorb these changes. git will know that you've added new files (untracked files), or made changes to files that are being tracked (changes not staged for commit), but you need to explicitly tell git which changes you want to include in the next commit by adding them to the staging area using git add.
Once you've added changes to the staging area, you can commit them to your git repository using git commit. This creates a new commit with the changes that you had staged. Commits are snapshots of your project at a particular point in time, and they become part of your project's history. You can checkout a commit (discussed later) to see exactly what your project looked like at that point in time.
In git, a remote refers to a copy of a git repository that is hosted on a server or other location, separate from your local repository (local to your computer). Remotes are essentially references to the repository stored on other machines, which can be on your local network or on the internet. Remote repositories are often used for collaboration, backup, and sharing code with others.
By convention, the word "origin" is used to refer to the remote repository, however, it can be called anything at all. It is also possible to have multiple remote repositories linked to your local repository.
To keep things simple, I'm going to say that these are platforms, where you can store your code, your repositories. There are other alternative platforms as well.
git initgit add file1.txt file2.txtgit add . # method 1
git add -A # method 2
git add --all # method 3
Before you can commit a change, there must be a change in the staging area. For example, it could be a new file that has been added to the staging area, or it could be a change to a file that is already being tracked by git, part of the repository.
Commit messages are important. Read how to write better commit messages here.
git commit -m "Add margin."git commit -am "Add margin."This will stage all changes and write a commit message all in one step.
git commitAfter you type this command, the default terminal editor will open up. You will have to type a commit message, save it, then exit the editor. If you do not save a commit message, no changes will be committed.
This will show you if there are any changes in the git repository, and if any new files (untracked files) have been added.
git statusIn git speak, you switch branches by 'checking them out.'
This is the same as creating a new branch. The branch will 'branch out' from your currently checked out branch.
git checkout -b BRANCHNAMEgit checkout BRANCHNAMEgit remote add origin git@github.com:USERNAME/REPOSITORYNAME.gitgit push --set-upstream origin BRANCHNAMEBRANCHNAMEis the local branch you want to push to the remote repository.- It is assumed that you have
checked outthis branch.
fetch will fetch all the new branches, tags, commits from remote, but will not apply them or merge the changes. No local changes will occur.
git fetchpull on the other hand will do a git fetch and then apply the changes (if there are any) to the branch that is checked out. Therefore, git pull is a combination of git fetch followed by git merge.
git pullThis alias will display the git commit history in a stylized fashion, including commit author names. Feel free to rename this alias to your liking.
📝 This is for Linux or macOS.
- On a new line, add the alias to your
.zshrcor.bashrc. - Save.
- Close the terminal currently used, and then open a new terminal.
- While inside a folder that is tracked by
git, typegitlogrto see the commit history. - Exit the commit history view by pressing
q.
alias gitlogr="git log --all --graph --decorate --oneline --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"