Loading...
Loading...
learngitbranching.js.org - git training simulator
// Set username
git config --global user.name "Your Name"
// Set email
git config --global user.email "example@email.com"
// Check current configuration
git config --list
// Create new repository
git init
// Clone remote repository
git clone <repository URL>
// Check repository status (new/modified files)
git status
// Add files to index (staging area)
git add <filename or .>
// Commit changes
git commit -m "Commit message"
// View commit history
git log
// Brief history:
git log --oneline --graph --decorate --all
// Send changes to remote repository
git push
// Get changes from remote repository
git pull
// Create new branch and switch to it
git checkout -b <branch_name>
// Switch to existing branch
git checkout <branch_name>
// View list of local branches
git branch
// Delete branch locally
git branch -d <branch_name>
// Merge branch into current
git merge <branch_name>
Tip:
Before merge or rebase make sure you have no uncommitted changes to avoid conflicts.
// Merge
// merge creates merge commit and preserves branching history.
git checkout main
git merge feature
// Rewriting history (rebase)
// rebase "rewrites" commits on top of another branch, creating linear history.
git checkout feature
git rebase main
// Add new remote repository
git remote add <name> <URL>
// View list of remote repositories
git remote -v
// Fetch changes from remote repository (without merging)
git fetch <repo_name>
// Merge changes after fetch
git merge <branch_name>
// Undo changes in working directory (before commit)
git checkout -- <filename>
// Remove file from index (but keep changes in working directory)
git reset <filename>
// "Reset" branch to specific commit
git reset --hard <commit_hash>
Warning:
git reset --hard command irreversibly deletes changes that aren't committed and not saved in other branches.
// Create lightweight tag
git tag v1.0
// Create annotated tag
git tag -a v1.0 -m "Version 1.0"
// View list of tags
git tag
// Send tags to remote repository
git push --tags
// View difference between working directory and index
git diff
// View difference between index and last commit
git diff --staged
// View changes between two commits
git diff <commit1> <commit2>
// Search commit by message/author:
git log --grep="search in comments"
git log --author="Name"
If you need to urgently switch to another branch, but have uncommitted changes:
// Save changes to temporary storage
git stash
// View list of "stashes"
git stash list
// Restore last stash
git stash pop