Git for Beginners
This tutorial assumes that the default branch for your repository's host is master. I believe for GitHub it is still master, but GitLab uses main. Please do not type "master" if you're on the main branch, and vice versa. Doing so will give you an error.
How to create and initialize a bare repository (i.e. a directory) for a site like GitHub:
$ mkdir my-cool-thing $ cd my-cool-thing $ git init Initialized empty git repository $ touch myFile.js $ cat > README.md # My Cool Thing Hey everyone, this is *my* cool thing. $ git add myFile.js $ git add README.md $ git remote add origin email@example.com:you/example.git $ git commit -m "Initial commit" [master f73ed8] Test 2 files changed, 7 insertions(+), 0 deletions(-) create mode 100644 myFile.js create mode 100644 README.md $ git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 261 bytes | 261 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) To github.com:you/example.git
What this series of commands does is: It creates a new directory, with two files inside of it. myFile.js, and README.md. It initializes an empty repository. These files are added to the "staging area", which is a kind of "place" you put your modifications.
The GitHub repository URL is added as the remote "origin": the default global URL. This needs to be done before anything can be pushed. The typical commit message for the first commit is "Initial commit". What git push origin master means, is: You are pushing to the master branch of the origin global location.
|Command||What It Does|
|$ git init||Initialize an empty Git repository in the current directory. This creates an invisible directory ".git", which is present in every repository.|
|$ git remote add origin [repository URL]||This command is very important when creating a new project in version control. GitHub for example (remember Git is not necessarily the same as GitHub), will give you a URL that looks like this: https://github.com/You/your-repo.git.|
|$ git add [file or directory]||Add a newly changed file or directory to the staging area. The staging area is a place where files are selected to be committed. Just because you change a file, does not mean your changes have been staged. Adding a directory, like git add ., will automatically add all of the changed files in that directory.|
|$ git rm [unwanted file]||Removes the file from the repository, AND actually deletes it! The only way to restore a file deleted this way is with git checkout or git reset --hard|
|$ git rm -r [unwanted directory]||Does the same for an entire directory. The "-r" option, for most essential Linux commands (cp, rm, chmod), can be assumed to make the command operate recursively: through a whole directory, and its sub-directories.|
|$ git rm --cached [retracted file]||This is like safe mode git rm. This deletes the file from the repository (marks it for deletion in the staging area), but retains the actual file. This adds the "rm" change to your current staged changes.|
|$ git mv [existing file] [new filename]||Move or rename an existing file, to a new directory, or new filename.|
|$ git status||Your best friend. This command tells you all staged and unstaged changes for the repository, since the last commit. Staged changes are in green, unstaged changes are in red.|
|$ git log||Show a list of recent commits, with their commit messages, authors, and hashes, for the repository.|
|$ git checkout [branch or commit] -- [specific file]||This command allows you to browse the Git repository as it appeared in a specific commit in time. You can get a list of commit hashes (those 6-digit hexadecimal numbers) from the git log command. You can check out the whole repository, or simply look at an individual file. For example, if a repository has a branch called "dev", you would do git checkout dev. Or, if you've tracked that something important happened at commit d3ad69, you can also type git checkout d3ad69. To retrieve only a single file from that commit, use git checkout d3ad69 -- filename.txt.|
|$ git checkout -b [new branch name]||Create a new branch and switch to it. This only creates the new branch in your local repo, not on the global repo.|
|$ git commit -m "commit message"||This is the command that writes your staged changes in stone, for all future programmers. Committing saves all of your saved changes, and then waits to be pushed. You can commit several times before you push, and all of the commits will send at once. Make sure your commit message is always enclosed in "quotes".|
When you commit for the first time, you will possibly be prompted to enter information about yourself: Your name and email address. This information is saved in ~/.gitconfig. Your name and email are pretty much what associate you with your commits, and your GitHub will automatically link your profile if you're committing with the same email address.
|$ git reset --hard [remote/branch]||This is the command that saves your ass if you ever fuck up a repository so much, that you just want to be abandon all of your changes and set it back to how it was. The command in this situation is usually git reset --hard origin/master.|
|$ git push [remote] [branch]||Push (globally publish) the locally-saved commits to the global repository. This command is usually git push origin master, but can also be something like git push upstream dev. When and if you create a new branch for yourself and start committing to it (a very powerful development strategy anywhere), you will have to modify your "git push" command. Oh, and these names are optional. After pushing at least once, git push will assume the remote is "origin" and the branch is "master".|
|$ git pull [remote] [branch]||Pull (retrieve the latest commits) and synchronize your local repository with the global one. This command always gives you a colorful summary of which files were changed, and how many additions and deletions there were.|
|$ git remote (set-url, get-url, add, remove) [remote] [repository URL]||Add, remove, set a new URL for the given remote, or retrieve the existing remote URL. For example, if you've created a new repository on GitHub, and would like to connect your current directory to it, you would type git remote set-url origin https://github.com/You/project.git.|