Skip to main content

Git command

· 2 min read

Git Basics

git init
git clone <repository_url>
git status

Staging and Commits

git add <file(s)>
git commit -m "Commit message"
git reset --soft HEAD^ // Undo Last Commit (Keep Changes)
git reset --hard HEAD^ // Undo Last Commit (Discard Changes)

Branching

git branch <branch_name>
git checkout <branch_name>
git checkout -b <branch_name>
git merge <branch_name>
git branch -d <branch_name>

Remote Repositories

git remote add <remote_name> <repository_url>
git push <remote_name> <branch_name>
git pull <remote_name> <branch_name>

Logging and History

git log
git show <commit_hash>
git diff

Miscellaneous

touch .gitignore  // Ignore Files (Create .gitignore)
git checkout -- <file(s)> // Undo Changes in Working Directory
git reset HEAD <file(s)> // Undo Staged Changes

Cherry-pick

git cherry-pick <commit_hash>

Rebase

git rebase <base_branch>
git rebase -i <base_branch> // Interactive rebase

Squash Commits during Rebase

// Change "pick" to "squash" for the commits you want to squash
// Follow on-screen instructions to edit the commit messages
git rebase -i <base_branch>

Amend the Last Commit

git commit --amend

Stash Changes

git stash
git stash save "Stash message"

Apply Stashed Changes

git stash apply
git stash pop // Apply and remove from stash

View Stash List

git stash list

Show Differences with Stash

git stash show -p <stash_id>

Discard Stashed Changes

git stash drop <stash_id>
git stash clear // Remove all stashes

Tagging

git tag <tag_name>  // Create lightweight tag
git tag -a <tag_name> -m "Tag message" // Create annotated tag
git push origin <tag_name> // Push tag to remote

Submodules

git submodule add <repository_url> <path>  // Add submodule
git submodule update --init --recursive // Initialize submodules
git submodule foreach git pull origin master // Update submodules

Git Configurations

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Show Configurations

git config --list