Git Commands

# Initialize a Git repository
git init

# Configure user information
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Clone a repository
git clone <repository URL>

# Check repository status
git status

# Stage files
git add <file>
git add .

# Commit changes
git commit -m "Commit message"

# View commit history
git log
git log --oneline

# Create branches
git branch <branch-name>
git checkout <branch-name>
# Create and checkout branch at once
git checkout -b <new-branch>

# Merge branches
git merge <branch-name>

# Create and apply patches
git diff > patchfile.patch
git apply patchfile.patch

# Revert changes
git checkout <commit-hash> <file>
git revert <commit-hash>

# Discard local changes
git stash
git stash apply

# Ignore files
nano .gitignore
# Add file patterns to ignore

# Configure remote repository
git remote add origin <remote-url>
git remote -v

# Push changes to remote repository
git push origin <branch-name>

# Pull changes from remote repository
git pull origin <branch-name>

# Resolve merge conflicts
# Open conflicted files and resolve conflicts manually
git add <resolved-file>
git commit -m "Merge conflict resolved"

# Tag releases
git tag <tag-name>
git push origin --tags

# Undo last commit
git reset HEAD~
git reset <commit-hash>

# Rebase commits
git rebase <branch-name>

# Amend last commit
git commit --amend

# Collaborate with others
git fetch
git merge origin/<branch-name>
git push origin <branch-name>

# Reset branch to a specific commit
git reset --hard <commit-hash>

# Create and apply patches
git diff > patchfile.patch
git apply patchfile.patch

# View branch history
git branch -v

# Delete branches
git branch -d <branch-name>
git branch -D <branch-name>

# Resolve merge conflicts
# Open conflicted files and resolve conflicts manually
git add <resolved-file>
git commit -m "Merge conflict resolved"

# Tag releases
git tag <tag-name>
git push origin --tags

# Show differences between commits
git diff <commit-hash-1> <commit-hash-2>

# Show differences between branches
git diff <branch-name-1> <branch-name-2>

# Revert a commit
git revert <commit-hash>

# Squash commits
git rebase -i HEAD~<number-of-commits>

# Show remote branches
git remote show origin

# Update local repository from remote
git fetch origin
git merge origin/<branch-name>

# Delete remote branch
git push origin --delete <branch-name>

# Configure Git aliases
git config --global alias.<alias-name> <command>

# Configure Git colors
git config --global color.ui auto

# Ignore file permission changes
git config core.filemode false

# Show Git configuration
git config --list

# Remove files from repository
git rm <file>
git rm -r <directory>
git commit -m "File removed"

# Move or rename files
git mv <source-file> <destination-file>
git commit -m "File moved or renamed"

# Amend commit message
git commit --amend

# Show branches and their commits
git show-branch --all

# Show the current branch
git branch --show-current

# Show branches merged into the current branch
git branch --merged

# Show branches not merged into the current branch
git branch --no-merged

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove untracked files, directories, and ignored files
git clean -fxd

# Show commit statistics
git shortlog -s -n

# Cherry-pick a commit
git cherry-pick <commit-hash>

# Amend author of a commit
git commit --amend --author="Author Name <[email protected]>"

GitHub Crash Course:

  1. To create a new repository, log in to GitHub and click on the "New" button. Follow the prompts to create the repository.
  2. To link your local repository to the remote GitHub repository, use git remote add origin <repository URL>.
$ git remote add origin <https://github.com/user/repo.git>
  1. To upload your local commits to GitHub, use git push origin <branch name>.
$ git push origin main
  1. To create a personal copy of someone else's repository on your GitHub account, fork the repository.
  2. To propose changes to the original repository, create a pull request from your forked repository.
  3. To collaborate with others, create new branches in your repository, make changes, and create pull requests.
  4. To review and provide feedback on pull requests from other contributors, review them and suggest changes or approve them.
  5. Use GitHub's issue tracker to report bugs, suggest features, or discuss ideas related to a repository.
  6. Discover and explore open-source projects and repositories on GitHub.

Setting up a Git project

If you want to work with an existing project, clone it:

$ git clone <url> - for example: git clone <https://[email protected]/greenido/html5-boilerplate.git>

If you do not have an existing git project, create one:

$ cd project/
$ git init          # initializes the repository
$ git add .         # add those 'unknown' files
$ git commit        # commit all changes, edit changelog entry
$ git rm --cached <file>... # ridiculously complicated command to undo, in case you forgot .gitignore