Turn a directory into a git project
git init
Check the status of your changes
git status
Add a file to the staging area
git add <filename>
Add multiple files to the staging area with a single command
git add <filename_1> <filename_2>
Check the difference between the working directory and the staging area
git diff <filename>
Commit to permanently store changes from the staging area inside the repository
Message flag: -m
give a message associated to the commit
git commit -m "Complete first line of dialogue"
Add flag: -a
commit everything (Mix add and commit in a single step)
This only can be used if git is already tracking the file
git commit -am 'Updated the README'
--amend
To add new changes to previews commit
git commit -a --amend
Commits are stored chronologically in the repository and can be viewed with:
Used to view the history of the commits in a project.
git log
One line flag : --oneline
to see commits in one line
To limit the number of commits logged
git log -2
You can combine the limit flag with the one line flag
git log -1 --oneline
To see all the information git has abourt logged commits
git log -p
To see the summary of the changes that you made
git log --stat
Shows Head and the branch where it is
git log --decorate
Show full commit hash and message
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
Maximum amount of information possible
git log --pretty=fuller
To supply a custom format
git log --pretty=format:"%h | %an(%ae) : %s"
Show a commit graph (See branches presented visually)
git log --graph
Show the HEAD commit.
HEAD
is at the most recent commit
git show HEAD
Restore the file in your working directory to look exactly as it did when you last made a commit
git checkout HEAD <filename>
You have to create an ingnore file
vim .gitignore
#Untracked folders
<folder name>
#Untracked files
Thumbs.db
This command resets the file in the staging area to be the same as the HEAD commit. It does not discard file changes from the working directory, it just removes them from the staging area.
git reset HEAD <filename>
Git enables you to rewind to the part before you made the wrong turn and create a new destiny for the project. This command works by using the first 7 characters of the SHA of a previous commit.
git reset <SHA>
git checkout HEAD <filename>
Discards changes in the working directory.
git reset HEAD <filename>
Unstages file changes in the staging area.
git reset SHA
Can be used to reset to a previous commit in your commit history.
You can use the command below to answer the question: “which branch am I on?”
git branch
To create a new branch, use:
git branch <new_branch>
Branch names can’t contain whitespaces
To switch to another branch
git checkout <branch_name>
Create a new branch and checkout shortcut
git checkout -n <branch_name>
Delete branch
git branch -d <new_branch>
Force delete
git branch -D <new_branch>
To see last commit in a deleted branch
git cat-file -p <1201b5f>
Take content from one branch and merge it into another
To merge branches go to the branch you want to have the changes of another branch and then merge
git checkout master
git merge new_branch
- Your goal is to update
master
with changes you made tonew_branch
new_branch
is the giver branch, since it provides the changesmaster
is the receiver branch, since it accepts those changes
The current branch is always the branch that receives the changes
The pull command results in a mege commit by default
git pull <repository> <branch>
The fetch command brings in changes but doesn't update our current branch Fetch is like a pull, but you have to merge manually and it doesn't add a merge commit automatically
git fetch <repository> <branch>
git diff FETCH_HEAD
We can compare files, branches or anything that has a hash
git diff master new-branch
to only see the names of the files that changed
git diff master new_branch --name-only
Shows names and insertions/deletions
git diff master new_branch --stat
Used to mark a particular point in a project's history as special, such as a release Tags cannot be changed To see existing tags
git tag
to create a tag
git tag <tag_name>
to see what commit a tag points to
git show <tag_name>
To point a tag to a particular commit
git tag -a <tag_name> <commit_hash>
to checkout the commit a tag points to
git checkout <tag_name>
To reattach tour HEAD you just need to checkout to master branch
A stash is a stack that can take any number of stashes stored in reverse chronological order Save changes without commiting
git stash
To see stashed changes
git stash list
To look at a particular stash The stash with the lowest index is the most recent stash
git stash show stash@{1}
To see actual changes
git stash show stash@{1} -p
To compare two stashed changes
git diff stash@{0}..stash@{1}
To remove a specific stash
git stash drop stash@{0}
To clear all stashed changes
git stash clear
To save changes with a description message
git stash save '<description>'
With apply you can specify wchich stash to apply
git stash apply
Take must recent stash, applies the changes and removes the stash from the list
git stash pop
Squash commits into one The i flag is used to make the rebase interactive: you see the commits that are about to be squashed and edit the commit message
git rebase -i <commit selected to squash after it>
Rebase HEAD of a branch underneath your current changes in your current branch
git rebase <branch_name>