Unlike some of the competition:
As we go into details of any programming tool, you'll find that documentation is your best friend for learning and improving your tool skill set:
A comment is a programmer-readable explanation or annotation in the source code of a computer program. Anything within comments will be ignored by a compiler and are just there to help us programmers understand the code better.
You'll use comments for documentation and explanation of your code. Anytime you see comments, just know it's there for clarity.
Also known as single-line comment, line comment syntax is prepended with #
# Example line comment
The init
command will initialize your git repository in the current or specified directory, or reinitialize an existing one:
# create a new repository
git init
The status
command will show you the status for the working tree that you're currently in:
# log the current status
git status
The add
command adds file contents to the index:
# add the file by name
git add filename.txt
# add all files within the directory
git add .
# adds every file with the file type: ".txt"
git add '*.txt'
The commit
command is used to commit your files to git with a given commit message:
# commit with the message: "Your Message"
git commit -m "Your Message"
The log
command does just that; it will show the commit logs:
# log your commits
git log
The remote
command is used to add a remote repository, so that you may successfully push your code:
# git remote add <name> <url>
git remote add origin https://github.com/your-username/your-repo-name
The push
command tells git that you're ready to push your local changes. Just tell git where to push:
# git push <remote-name> <branch-name> [-u: remember these parameters]
git push -u origin master
# If you have already used -u, you can just run `git push`
git push
The pull
command tells git that you're wanting to pull any remote changes into your local copy:
# git pull <remote-name> <branch-name>
git pull origin master
# If you have already used -u to set defaults, you can just run `git pull`
git pull
The diff
command allows you to view the differences between commits:
# `HEAD` will give us the most recent commit
git diff HEAD
# `--staged` will give you the changes you've staged
git diff --staged
The reset
command allows you to unstage files that you've staged:
# unstage file by name
git reset filename.txt
# unstage all staged files
git reset .
# unstage every file with the file type: ".txt"
git reset '*.txt'
The checkout
command allows you to switch branches:
# Switch to `test-branch` from current branch
git checkout test-branch
The checkout
command also allows you to restore working tree files:
# git checkout -- <target>
git checkout -- filename.txt
The branch
command allows you to list, create, and delete branches:
# lists branches
git branch
# create a branch with a given name
git branch branch-name
# delete a branch with a given name
git branch -d branch-name
The merge
command allows you to join two or more development histories together, such as a branch:
# merges other-branch into the current branch
git merge other-branch
The rm
command (short for remove) allows you to remove files from the working tree and from the index:
# removes the file by name
git rm filename.txt
# removes every file with the file type: ".txt"
git rm '*.txt'