Table of contents
- Initial configuration
- Branch management
- Staging and snapshots
- History and logs
- Checkout and restore
- Diffs and comparisons
- Merge and rebase
- Tags
- Stashing
- Networking and remotes
- Cherry-picking
- Closing issues
- File states
Initial configuration
Check Git version
$ git --version: Shows the currently installed Git version.
General help
$ git --help: Shows general Git help, including a list of the most common commands.
User configuration
$ git config --global user.name <name>: Sets the global user name for all repositories.
$ git config --global user.email <email>: Sets the global user email for all repositories.
$ git config user.name: Shows the configured user name.
$ git config user.email: Shows the configured user email.
Edit global configuration
$ git config --global -e: Opens the configured editor to edit the global Git configuration file.
$ git config --global --edit: Another way to open the editor to modify the global Git configuration.
Git aliases
$ git config --global alias.<alias> <command>: Creates a global alias for a Git command.
Default branch configuration
$ git config --global init.defaultBranch <name>: Sets the default branch name for new repositories.
$ git config --global init.defaultBranch main: Sets ‘main’ as the default branch in new repositories.
UI configuration
$ git config --global color.ui auto: Enables automatic colors in the Git command-line interface.
Repository management
$ git init: Initializes a new local Git repository. This creates a new .git subdirectory containing all necessary repository files.
$ git clone git_url: Clones a Git repository from the given URL. This copies all repository data, including the full commit history.
Branch management
$ git branch: Lists all local branches in the current repository.
$ git branch <branchName>: Creates a new local branch with the specified name.
$ git branch -m <name>: Renames the current branch to a new name.
$ git branch -m master main: Renames the ‘master’ branch to ‘main’.
$ git branch -d <branchName>: Deletes a local branch. It can only be deleted if all changes have been merged.
$ git branch -d -f <branchName>: Force-deletes a local branch, even if it has unmerged changes.
$ git branch -vv: Shows all local branches with extra information, including the last commit and relationship to remote branches.
$ git branch -r: Lists all remote branches.
$ git branch -a: Lists all branches, both local and remote.
$ git branch -m <new_name>: Renames the current branch to <new_name>.
Staging and snapshots
Repository status
$ git status: Shows the repository status, including pending changes, untracked files, and more.
Adding files
$ git add <file>: Adds a specific file to the staging area.
$ git add .: Adds all modified files in the current directory to the staging area.
$ git add <file1> <file2>: Adds multiple specific files to the staging area.
$ git add *.<extension>: Adds all files with a specific extension to the staging area.
$ git add js/*.<extension>: Adds all files with a specific extension inside a given directory to the staging area.
$ git add css/: Adds all files inside a specific directory to the staging area.
Removing files
$ git rm <file>: Removes a file from the working directory and the staging area.
$ git rm --cached <file>: Removes a file from the staging area but keeps it in the working directory.
$ git rm --cached .: Removes all files from the staging area but keeps them in the working directory.
Moving and renaming files
$ git mv <currentName> <newName>: Moves or renames a file or directory.
Undoing changes
$ git reset <file>: Removes a specific file from the staging area but keeps the changes in the working directory.
$ git reset .: Removes all files from the staging area but keeps the changes in the working directory.
$ git reset --soft HEAD^: Undoes the last commit while keeping changes in the staging area.
$ git reset --soft HEAD^3: Undoes the last three commits while keeping changes in the staging area.
$ git reset --soft <commit>^: Undoes up to a specific commit while keeping changes in the staging area.
$ git reset --mixed <commit>: Resets HEAD to the specified commit, undoing staging changes but keeping changes in the working directory.
$ git reset --hard <commit>: Resets HEAD, the staging area, and the working directory to the state of the specified commit.
$ git reset --hard: Resets HEAD, the staging area, and the working directory to the last commit, discarding all changes.
Committing
$ git commit -m "message": Creates a commit with the given message.
$ git commit -am "message": Adds all modified files to the staging area and creates a commit with the message.
$ git commit --amend -m "message": Amends the last commit, allowing you to change the message or add more changes to the same commit.
$ git commit -am "<Fixes #tag>: <message>": Creates a commit with a message that includes a reference to a specific issue or tag.
History and logs
Viewing commit history
$ git log: Shows the full commit history for the current repository, including details such as author, date, and message for each commit.
$ git log --oneline: Shows the commit history in a short format, one line per commit with its abbreviated ID and message.
$ git log --follow [file]: Shows the commit history for a specific file, including renames.
Reference log
$ git reflog: Shows a log of operations performed in the repository, such as commits, rebase, merge, and more. Useful for recovering previous commit states.
Checkout and restore
Switching branches and restoring files
$ git checkout -- .: Restores all files in the working directory to their last state in HEAD, discarding uncommitted local changes.
$ git checkout <branchName>: Switches to the specified branch, updating the working directory and index to match the selected branch.
$ git checkout <tagname>: Switches to a specific tag, effectively moving the working directory to the code version at that point in time.
$ git checkout -b <branchName>: Creates a new branch with the specified name and switches to it immediately.
$ git checkout -b <new-branch> <tagname>: Creates a new branch from the specified tag and switches to it immediately.
$ git checkout -: Switches back to the last branch you were on. A shortcut to return to the previous branch.
$ git checkout <branch> -- <file>: Updates the specified file in the working directory to match the version from the given branch or tag.
Restoring files with git restore
$ git restore <file>: Restores a specific file to its last state in HEAD, discarding uncommitted local changes.
$ git restore .: Restores all files in the working directory to their last state in HEAD, similar to git checkout -- ..
Diffs and comparisons
$ git diff: Shows differences between the working directory and the index (staging area), useful for seeing changes that are not yet staged.
$ git diff --staged: Shows differences between files in the staging area and the last commit. Useful for reviewing changes that are ready to be committed.
Merge and rebase
Git merge
$ git merge <branchName>: Merges the specified branch into the current branch. Git will attempt a “fast-forward” if possible, i.e. when there are no conflicting changes between the branches.
$ git merge [alias]/[branch]: Merges a specified remote branch into the current local branch. Common when working with remote branches.
$ git merge --no-ff [alias]/[branch]: Performs a merge by explicitly creating a merge commit, even when a fast-forward is possible. This helps preserve branch history as separate entities.
$ git merge --ff-only [alias]/[branch]: Performs the merge only if a fast-forward is possible. If not, the command fails, which can be useful to avoid creating extra merge commits.
Git rebase
$ git rebase <branch>: Reapplies the current branch’s commits on top of the specified branch. Used to “move” a series of changes from one branch to another, changing the base of the commits.
$ git rebase -i HEAD~3: Starts an interactive rebase of the last three commits. Lets you edit, reorder, combine, or drop commits interactively.
$ git rebase -i <hash>: Starts an interactive rebase from the commit identified by the hash. Lets you adjust commits from that point onward.
$ git rebase -i HEAD~4: Similar to the previous command, but for the last four commits, allowing interactive editing of those commits.
Additional notes
-
Merge vs rebase: While
mergepreserves the exact history of changes,rebaserewrites history so that all changes appear to have been made in a cleaner timeline. This can simplify the project’s visual history but should be used with care, especially on shared branches. -
Interactive rebase: A powerful tool for cleaning up history before integrating changes into a main branch. It can make commits easier to understand and manage.
-
Rebase caution: By changing commit history,
rebasecan complicate collaboration on shared branches. Always coordinate with your team before rebasing branches that others might be using.
Tags
Tags in Git are commonly used to mark specific points in the repository history, such as version releases.
List tags
$ git tag: Lists all tags in the current repository.
Create tags
$ git tag -a <semanticVersion>: Creates an annotated tag with the specified semantic version. Annotated tags include extra metadata such as author name, date, and an associated message, and are considered more complete.
$ git tag -a <semanticVersion> -m "message": Creates an annotated tag and lets you specify the tag message on the command line without opening an editor.
$ git tag -a <semanticVersion> <hash> -m "message": Creates an annotated tag at a specific commit identified by its hash. Useful for tagging past commits retroactively.
Delete tags
$ git tag -d <semanticVersion>: Deletes the specified tag from the local repository.
Show tag information
$ git show <tag>: Shows information about the specified tag, including the associated commit, tag message, and changes in that commit.
Pushing tags with Git
$ git push --tags: Pushes all local tags to the remote.
$ git push origin <tag>: Pushes a specific tag to the remote.
Stashing
$ git stash: Temporarily stores uncommitted changes in tracked files, giving you a clean working directory.
$ git stash save "stash name": Saves changes in a stash with a specific name for easy identification.
List stashes
$ git stash list: Shows a list of all stored stashes.
$ git stash list -p --stat: Shows a list of all stashes along with the stats and patch for each stash.
Apply stashes
$ git stash pop: Applies the last saved stash and then removes it from the stash list.
$ git stash apply stash@{n}: Applies the stash specified by its index without removing it from the stash list.
Remove stashes
$ git stash drop: Removes the last stash from the list.
$ git stash drop stash@{n}: Removes the stash specified by its index.
$ git stash clear: Removes all saved stashes.
Show stash content
$ git stash show -p stash@{n}: Shows the specific changes stored in the indicated stash.
Networking and remotes
Remote branch management
$ git push origin :<branch>: Deletes a branch on the remote repository.
$ git push origin --delete <old>: Also deletes a branch on the remote repository.
Publishing changes
$ git push origin -u <new_name>: Pushes changes to the specified branch and sets up tracking with the remote branch.
$ git push --set-upstream origin <branch>: Similar to the above, pushes the local branch to the remote and sets up tracking.
$ git push [alias] [branch]: Pushes changes to the specified branch on the repository defined by the alias.
Fetching changes
$ git pull: Fetches and merges changes from the remote repository into the current branch.
$ git pull upstream <branch>: Fetches changes from the specified branch of the upstream repository and merges them into the local branch.
$ git pull --all: Fetches changes from all branches of the remote repository.
$ git pull --rebase: Fetches changes and rebases them onto the current branch.
Managing remote repositories
$ git fetch [alias]: Fetches objects and references from the repository specified by the alias.
$ git remote add upstream <url>: Adds a new remote repository named “upstream”.
$ git remote add [alias] [url]: Adds a new remote repository with a specific alias.
$ git remote set-url origin [git_url]: Changes the URL of the “origin” remote repository.
$ git remote rm [remote repo name]: Removes the configuration for the specified remote repository.
$ git remote prune origin: Cleans up stale references to branches that have been deleted on the “origin” remote.
Cherry-picking
$ git cherry-pick [commit_id]: Applies the changes introduced by the specified commit onto the current branch. This command is useful for bringing specific changes from one branch to another without merging or rebasing the entire branch.
Closing issues
When working with GitHub or similar platforms, you can close issues automatically by including certain keywords in your commit messages. This keeps a clear link between commits and the specific issues they address, improving traceability and project documentation.
Keywords for closing issues
- close
- closes
- closed
- fix
- fixes
- fixed
- resolve
- resolves
- resolved
These keywords can be used in commit messages to indicate that the changes in that commit resolve an issue. They should be followed by the issue number you want to close.
Example
Suppose you have an open issue #123 about a bug in your application. You can close this issue directly from a commit with a message like:
git commit -m "Fixes #123 - Fix login function error"
File states
-
M (Modified): Modified since the last commit.
-
A (Added): Added to the index for the next commit.
-
D (Deleted): Deleted and staged for the next commit.
-
R (Renamed): Renamed since the last commit.
-
C (Copied): Copied from another file.
-
U (Unmerged): In conflict after a merge.
-
?? (Untracked): Not under version control.
-
! (Ignored): Ignored according to the .gitignore file.
-
X (Unknown): An unrecognized state or an untracked file in the index.