Git Command Line Cheat Sheet
Git Command Line Cheat Sheet
'git log' provides a history of commits which helps track how a project has evolved over time. Options like '--follow [file]' trace changes to specific files across renames, while '--pretty=oneline --graph --decorate --all' offers a visual representation of branch and merge history, aiding in understanding complex development paths and contributions .
Branches help isolate different features or bug fixes, allowing multiple contributors to work on separate tasks without interfering with the main codebase. Developers can create new branches for specific tasks using 'git checkout -b new_branch' and merge them into master branches when completed using 'git merge' after resolving conflicts. This isolates changes until they are stable and ready for integration .
'git rm' is used to delete a file from the working directory and stage the removal for commit simultaneously. This should be used when a file needs to be removed from the repository rather than just the local system to ensure the change is tracked and pushed to the remote repository in the next commit, maintaining consistency across environments .
'git stash' temporarily saves changes that aren't ready to be committed, allowing developers to switch branches or work on other tasks without losing their current work in progress. This is especially useful in maintaining a flexible workflow where unexpected tasks arise and developers need to shift focus quickly without committing incomplete changes .
Synchronization is achieved through commands like 'git fetch', which updates your local repository with the changes from the remote repository without merging them, 'git pull', which merges fetched changes into your current branch, and 'git push', which uploads local commits to a remote branch. This ensures that developers have the latest updates and avoid conflicts by reconciling local changes with the remote repository .
'git reset --hard' is used when a developer wants to discard all local changes, both staged and unstaged, and reset the repository to the last commit. This is appropriate in scenarios where changes are not needed, or the working directory is in an irrecoverable state. However, using it will permanently lose any changes not committed, so it should be used cautiously .
The '--no-ff' option prevents fast-forwarding, ensuring that a new commit is always created on merge. This is useful for maintaining a clear and recorded branch history, indicating where branches were merged. It should be used when you want to keep the complete historical context of development efforts even if fast-forwarding would naturally apply .
Setting a global user.name and user.email in Git configurations ensures that commits are properly attributed to the right individual, providing accurate commit history and accountability. This metadata is critical for traceability and collaboration within teams, allowing contributors to know who made specific changes .
Effective management involves regularly syncing local and remote branches using 'git fetch' and 'git pull', cleaning up obsolete branches with 'git branch -d' or 'git remote rm', and setting relevant upstream branches for synchronization. Mismanagement, such as confusion from unused branches or inconsistent updates, can lead to conflicts, outdated codebases, and disruptions in collaboration .
'.gitignore' files specify which files or directories Git should ignore, meaning they will not be tracked or included in commits. This is crucial for excluding unnecessary files such as system files, temporary files, or dependencies that do not need to be shared, thus streamlining repositories and reducing clutter .