You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Git is a distributed version control system, created by Linus Torvalds in 2005. Git is practical, simplified, fast, efficient, and free and open source.
First commands
Name
Command
Description
Init
git init
Create directory configured as a git repository
Status
git status
Show repository status
Add
git add <file name>
Add new and modified files for next commit
Commit
git commit -m 'description'
Register commit with all files you used git add
Log
git log
View latest commits in repository
Settings
git config --global user.email "you@example.com ”
Set up your email (only the first time)
git config --global user.name “Your Name”
Set up your name (only the first time)
File status
❌ Untracked
🆕 Modified
🆗 Staged
✔️ Committed
Workflow
✂️ Edit,
📝 Commit, and
🔃 Sync with remote repository
Create a remote repository
Name
Command
Description
Remote
git remote -v
Configure the server
git remote add <remote><url>
Add another remote server
Push
git push -u origin master
Sync with remote repository the first time
git push
Sync with remote repository
Clone
git clone <url>
Download remote repository
Pull
git pull
Keep repository synchronized with last branch commits
History and conflicts
Name
Command
Description
Diff
git diff [path]
Display differences between commits and branches
git diff HEAD~1
Shows what was changed in the last commit
Checkout
git checkout <commit><file>
Shows how a file or entire repository was in a given commit
git checkout <commit>
Changes the repository to that commit state
git checkout master
To return the repository to the last commit
git checkout -- <path_or_file>
Undo all non-stage changes since the last commit
git checkout HEAD -- <path_file>
Undo changes since last commit including stage
Revert
git revert <commit>
Creates a new commit that undoes changes to the specified commit
Reset
git reset <commit>
Reset repository for a given commit
git reset --hard <commit>
Resets and removes all changes ❗ Be careful when using
Branch, Merge and Rebase
Name
Command
Description
Branch
Branch master is the default. Development branch make it easy to control
git branch <new_branch>
Create a new branch
git checkout -b <branch>
Create a new branch
git branch -d <branch>
Delete a branch
git checkout <branch>
Switch to the branch
Merge
git merge <branch>
Commit a branch to the current branch. Finds a common commit (base) between branches and applies all commits that the current branch doesn't have. If there are commits in the current branch that is not in the other branch, a merge commit will be created.
Rebase
git rebase <branch>
Similar to Merge but different in the order of committing. In Rebase, your commits in front of the base are temporarily removed. Commits from another branch are applied to your branch, and your commits are finally applied one by one. There may be conflicts that will be resolved for each commit.
Fetch
git fetch
pull = fetch + merge. Download remote updates but do not apply them to the repository. Lets you rebase a branch instead of merge. Fetch and rebase is best for keeping track of development.
Tag
git tag [name_tag]
Useful for defining stable versions of the project
Other commands
Name
Command
Description
Amend
git commit --amend
Changes the last commit.
Stash
git stash
Save Working Directory Changes. It allows you to rebase, merge, switch branches without the need to commit.
git stash list
Applies the last stored stash.
git stash pop
Cherry Pick
git cherrypick <commit>
Applies to commit changes to the current branch. Create a new commit. Useful to retrieve history.
Blame
git blame
Shows changes made to one file per line. Show the author and commit that line was made. Useful to check when changes were made, why, and by whom.
Bisect
git bisect
Let's you do a binary search in commits to find a change. Useful for changes that have changed behavior and cannot be easily identified by code. When the change may be quite old.