Git is a distributed version control system that allows you to manage and track changes in the source code of a project.
It facilitates collaboration among developers and the maintenance of software versions.
Installation and Initial Setup
Install on Windows
Download the installer from the official page and follow the instructions.
Install on Linux
Use the corresponding package manager:
sudo apt-get install git # For Ubuntu/Debian
sudo yum install git # For CentOS/RHELInstall on Mac
Use Homebrew:
brew install gitInitial Configuration
Configure User
Set your name and email (they will appear in commit logs)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"View Current Configuration
You can check your current configurations with:
git config --listConfigure Editor
Choose your preferred text editor for commit messages.
git config --global core.editor "editor_name"Basic Workflow Cycle
Working Directory
Local files not tracked by Git.
Staging Area
Files prepared for the next commit.
Repository (HEAD)
Files that have been committed and stored in Git.
Repositories
Initialize a New Repository
Create a new Git repository in the current directory.
git initClone an Existing Repository
Copy a remote repository to your local machine.
git clone https://github.com/user/repo.gitView Repository Status
Show the current status of files in the working directory (modified, untracked, etc.).
git statusShow
Displays details of a specific commit, such as file changes, commit message, and metadata (author, date).
git show <commit>Blame
Shows who made the last change to each line of a file, useful for tracking when and who made modifications to the code.
git blame <file>Change Control
Add Changes
Add files to the staging area.
Add a Specific File
git add filenameAdd All Modified Files
git add .Commit Changes
Make a Commit
Commit the changes added to the staging area:
git commit -m "Descriptive commit message"Combine add and commit in One Step
Add and commit all changes in a single command.
git commit -a -m "Commit message"Remove a File and Untrack it from Git
Delete the file and remove it from Git tracking.
git rm filenameRename or Move a File
Rename or move a file in the repository.
git mv new_filenameHistory and Change Review
View Commit History
List recent commits in the project.
git logView History with Diffs
Show commit history with detailed line-by-line changes.
git log -pCompact History
Display the history with a summarized representation of commits.
git log --oneline --graph --allView Uncommitted Changes
Show the differences between files in the working directory and the last version of the repository.
git diffView Changes Between Two Specific Commits
Compare differences between two commits using their identifiers.
git diff commit_id1 commit_id2Undo Changes
Revert Unstaged Changes
Discard changes in modified files that have not been added to the staging area.
git checkout -- filenameUndo Staged Changes
Return files from the staging area to their previous state.
git reset filenameReset a Commit Locally (Soft Reset)
Undo the last commits while keeping changes in the working area.
git reset --soft HEAD~1Hard Reset
Discard the last commits and all changes.
git reset --hard HEAD~1Revert a Published Commit
Create a new commit that undoes the changes from a previous commit.
git revert commit_idWorking with Branches
List All Branches
Show all local and remote branches.
git branch -aCreate a New Branch
Create a new branch and stay on the current branch.
git branch branch_nameSwitch Branches
Switch to an existing branch.
git checkout branch_nameCreate and Switch to a New Branch
Command that creates a branch and automatically switches to it.
git checkout -b branch_nameMerge a Branch with the Current One
Combine changes from another branch into the current branch.
git merge branch_nameDelete a Branch
Once merged, you can safely delete a branch.
git branch -d branch_nameForce Delete a Branch
Delete a branch even if it has not been merged.
git branch -D branch_nameRebase
Reorganize the commits of the current branch onto another branch. It is useful for cleaning up history or integrating changes without creating a merge commit.
git rebase <branch>Cherry Pick
Apply a specific commit from any branch or change history to the current branch. This allows selectively applying only certain changes.
git cherry-pick <commit>Remote Repositories
View Configured Remotes
Show a list of remote repositories.
git remote -vAdd a Remote Repository
Link your local repository with a remote one.
git remote add origin https://github.com/user/repo.gitRemove a Remote
Unlink a remote repository.
git remote remove originFetch Changes from a Remote Repository
Download changes from the remote repository without applying them.
git fetchUpdate the Local Repository with Remote Changes
Fetch and merge changes from the remote repository to the current branch.
git pull origin branch_namePush Changes to a Remote Repository
Send your commits to the remote repository.
git push origin branch_nameConflict Resolution
Resolve Merge Conflicts
When there are conflicts during a merge, Git will indicate the conflicting files. You need to edit them and resolve the conflicts manually. After resolving:
git add resolved-file.txt
git commit -m "Conflicts resolved"Tags
Tags are used to mark specific versions.
View All Tags
Show a list of all created tags.
git tagCreate a Tag
Mark a tag.
git tag tag_namePush Tags to a Remote Repository
Send tags to the remote repository.
git push origin --tagsStash
The Stash is used to temporarily save changes.
Save Changes in Stash
Temporarily save uncommitted changes in the working area to allow working on other changes without losing progress.
git stashList Saved Stashes
Show a list of saved changes.
git stash listApply Saved Changes
Restore changes from the most recent stash.
git stash applyRemove the Most Recent Stash
Delete the last applied stash.
git stash dropOptimization and Cleanup
Remove Untracked Files
Clean the working directory by removing files that are not under version control.
git clean -fCompact Commit History
Rewrite the history of the last commits.
git rebase -i HEAD~nOptimize the Repository
Remove unreferenced objects and optimize the size of the repository.
git gc