Git Commands
Column 1 Rebase Rebasing is the process of moving or combining a sequence of commits to a new base commit. It allows for cleaner project history by avoiding unnecessary merge commits. Rebasing can be used to integrate changes from one branch into another.$ git checkout feature_branch
$ git rebase main Cherry-pick Cherry-picking in Git refers to the act of choosing a specific commit from one branch and applying it onto another. This is useful for selectively incorporating changes without merging entire branches.$ git cherry-pick <commit-hash> Submodules Git submodules allow you to include other Git repositories within your own repository. This is useful for managing dependencies or including external libraries in your project. Submodule references a specific commit, making it easy to track and manage changes.$ git submodule add <repository_url> Reflog The reflog is a reference log that records updates to the tips of branches and other references in the repository. It stores information about when the tips were updated, making it useful for recovering lost commits or undoing changes.$ git reflog
$ git reset --hard HEAD@{1} Bisect The 'git bisect' command helps in finding the commit that introduced a bug by using binary search. It requires marking known good and bad commits, then automatically checking out revisions for testing until it finds the specific problematic commit.$ git bisect start
$ git bisect bad <commit-hash>
$ git bisect good <commit-hash> Filter-branch filter-branch is a git command that allows you to rewrite branches by applying custom filters. It can be used for tasks like removing sensitive data, splitting repositories, or simplifying commit history.$ git filter-branch --tree-filter 'rm -f <file>' Worktree A worktree allows you to check out multiple branches at once, enabling parallel development. It is a separate working directory associated with the repository but on a different path. Worktrees are useful for reviewing changes in isolation without affecting the main branch.$ git worktree add -b <branch-name> <path> LFS (Large File Storage) Git LFS is an open-source Git extension for versioning large files. It replaces large files with text pointers within the Git repository, while storing the file contents on a remote server. This helps in reducing clone and fetch times as well as optimizing storage.$ git lfs track '*.psd' Create a new branch: git checkout -b <branch_name> The 'git checkout' command is used to switch branches. The '-b' option creates and checks out a new branch at the same time. Replace '<branch_name>' with the desired name for your new branch. This shortcut allows you to create and immediately start working on a new feature or bug fix without switching branches manually. Merge changes from one branch into another The 'git merge <branch_to_merge_into_current_branch>' command is used to integrate the changes from a specified branch into the current working branch. This operation combines multiple sequences of commits, making it essential for collaboration and managing feature branches. It's crucial to resolve any conflicts that arise during merging by carefully reviewing and editing conflicting files before finalizing the merge process. List all branches in the repository: git branch The 'git branch' command is used to list all the local branches in a Git repository. It displays both remote and local branches, highlighting the current active branch with an asterisk (*). This command helps users visualize their branching structure and track which codebase they are currently working on. Additionally, it can be combined with other flags such as '-a' to show both remote-tracking branches and local ones or '--merged/--no-merged' for filtering based on merge status. Switch to an existing branch: git checkout <branch_name> - Use 'git checkout <branch_name>' to switch from the current working branch to an existing one. Force delete a local branch The command 'git branch -D <Branch_Name>' is used to force delete a local branch in Git. This should be used with caution as it permanently deletes the specified branch, along with all of its commits and changes. It's essential to ensure that any necessary work from the deleted branch has been merged or saved elsewhere before using this command. Delete a local branch after it has been merged and is no longer needed - Use the command 'git branch -d <branch_name>' to delete a local branch that has been merged into the current branch. Cloning a Remote Repository When cloning a remote repository, use the 'git clone' command followed by the URL of the remote repository. This will create a local copy of the entire project with its history and branches. Cloning allows for collaboration on projects without directly affecting the original source.$ git clone https://github.com/username/repository.git Adding a Remote Repository When working on a local repository, you can add a remote repository to collaborate with others or have an offsite backup. Use the 'git remote add' command followed by the name and URL of the remote repository. This sets up Git to sync changes between your local and remote repositories.$ git remote add origin https://github.com/user/repo.git Pulling Changes from a Remote Repository When pulling changes from a remote repository, use the 'git pull' command to fetch and merge changes. This updates your local branch with any new commits on the remote branch. It's important to specify the remote name (e.g., origin) and the branch you want to pull from.$ git pull origin main Fetching Changes from a Remote Repository To fetch changes from a remote repository, use the 'git fetch' command. This retrieves any new work that has been added to the remote since your last update. After fetching, you can merge those changes into your own branches using 'git merge'. It's essential to stay updated with the latest changes in order to collaborate effectively.$ git fetch origin
$ git merge origin/main Removing or Renaming Remotes To remove a remote, use 'git remote rm <remote_name>'. To rename a remote's shortname, apply 'git remote rename <old_remote_name> <new_remote_name>'. Verify changes with 'git remote -v'.
Pushing Changes to a Remote Repository When pushing changes to a remote repository, use the command 'git push' followed by the name of the remote and branch. For example: git push origin main. This sends your committed changes in local branches to the corresponding remote branches.$ git push origin main Viewing Information about Remotes To view information about the remotes, use 'git remote -v' to list all existing remotes along with their URLs. This command is helpful for checking which remotes are currently set up and where they point.$ git remote -v Managing Multiple Remotes When working with multiple remotes, you can add a new remote using 'git remote add <name> <url>'. To view all the existing remotes, use 'git remote -v'. When pushing or pulling from a specific remote branch, specify it as '<remote-name>/<branch-name>' such as 'git push origin master'. You can also fetch changes from all remotes by running 'git fetch --all'
Column 2 Understanding merge conflicts Merge conflicts occur when Git is unable to automatically resolve differences in code between two commits. To resolve a merge conflict, manually edit the conflicting files to remove the undesirable changes and then commit the corrected files. Use 'git status' command to identify conflicted files and 'git diff' for detailed information about the conflicts. Manually resolving merge conflicts in code files When a merge conflict occurs, Git will mark the conflicted area in the affected file. To resolve it manually, open the file and locate the conflict markers (<<<<<<<, =======, >>>>>>>). Edit the content between these markers to resolve conflicting changes. After making necessary adjustments, save and commit your changes to complete manual resolution. Using 'git status' to identify conflicted files When running 'git status', conflicted files will be listed under the section 'Unmerged paths'. Conflicted files occur when there are conflicting changes in different branches. Resolving conflicts involves editing the affected files, marking them as resolved with 'git add', and then committing the changes using 'git commit'. Resolving merge conflicts using Git tools 1. Use 'git status' to identify which files have conflicts. Creating a new commit after resolving conflicts When resolving conflicts in Git, use 'git add' to stage the resolved files. Then, create a new commit using 'git commit'. Use the '-a' flag with 'git commit' to automatically stage all modified and deleted files before committing. Remember to write a clear and descriptive commit message explaining how you resolved the conflict. git diff for viewing conflict changes before resolution The 'git diff' command is used to view the differences between conflicting changes in a file. It allows users to see what modifications have been made by both parties and understand the conflicts that need resolution. By using 'git diff', developers can identify where code conflicts exist, enabling them to make informed decisions when resolving merge issues. This command provides a clear overview of discrepancies, helping teams collaborate effectively during the merging process. Merging branches with automatic conflict resolution ('recursive strategy') - Use 'git merge -s recursive' to perform a merge using the recursive strategy. Aborting a Conflicting Merge with 'git merge --abort' 'git merge --abort' is used to abort the conflicting merge in Git. This command can be helpful when there are conflicts during a manual merge, and you want to start over. It resets the index and working tree to the state before the failed automatic merge attempt, allowing you to retry merging from scratch. 'ours' and 'theirs' options when merging When resolving a merge conflict, the 'ours' option selects the current branch's version of the conflicting file while discarding changes from the other branch. Conversely, using the 'theirs' option chooses to keep only changes from another branch, disregarding any modifications made in your current branch. These options are useful for specifying which version of code to retain during a merge conflict resolution. Viewing the commit history with git log The 'git log' command displays a list of commits in reverse chronological order. It shows the commit hash, author details, date and time of the commit, and the commit message. Use '--oneline' option to display each commit on one line for a more compact view.$ git log
$ git log --oneline Using options like --oneline, --graph, and --decorate in git log --oneline: Condenses each commit to a single line. Useful for getting a quick overview of the repository's history.--graph: Displays an ASCII graph representing the branch structure alongside the commits. --decorate: Shows ref names (e.g., tags or branches) that point to specific commits. $ git log --oneline
$ git log --graph
$ git log --decorate Limiting the number of commits displayed in git log using -n option - The '-n' option limits the number of commits shown in 'git log'.- It takes a numerical value as an argument to specify how many recent commits should be displayed. - For example, 'git log -3' will display only the last three commit entries. $ git log -3 Filtering and formatting git log output Git log can be filtered to show commits that meet specific criteria, such as author or date. The --grep option allows filtering by commit message content. Formatting options like --oneline, --pretty=format:, and -p help customize the display of commit information.$ git log --author=<name>
$ git log --since='2 weeks ago'
$ git log -S<keyword>
$git-log--format='%h %s' Displaying changes introduced by each commit with -p or -U options in git log - Using the '-p' option displays the difference introduced in each commit as a patch.- The '-U<number>' option can be used to control how many lines of surrounding context Git includes. $ git log -p
$ git log -U2 Git Log with Filtering Options Use 'git log --author=<author>' to filter commits by a specific author. Use '--since' and '--until' options to specify date ranges for the commits you want to see.$ git log --author=JohnDoe
$ git log --since='2021-01-01'
$ git log --until='2022-12-31' Customizing Git Log Output Using Pretty Formats Git log output can be customized using pretty formats to display specific information such as commit hashes, author names, dates, and commit messages. This allows users to tailor the log output according to their preferences or requirements.$ git log --pretty=format:'%h - %an, %ar : %s' Column 3 Forking a Repository When you fork a repository, you create a copy of the original repository in your GitHub account. This allows you to freely experiment with changes without affecting the original project. Forked repositories are linked back to the original, enabling easy collaboration and contribution through pull requests. Cloning a Repository Use the 'git clone' command followed by the URL of the repository to create a local copy. This allows you to work on and contribute to an existing project. Cloning also sets up remote tracking, enabling synchronization with changes made in the original repository. Creating Branches for Collaboration 1. Use 'git branch' to create a new branch. Pushing Changes to Remote Repositories 1. Use 'git push' command to send committed changes from your local repository to a remote repository. Pull Requests and Code Reviews 1. Pull requests are a way to propose changes from a branch in one repository into another. Pull Requests 1. Pull requests are a way to propose changes from a branch in one repository to the base branch of another repository. Merge Changes from Other Contributors 1. Use 'git fetch' to retrieve changes made by other contributors. Resolving Merge Conflicts 1. Pull the latest changes from the remote repository to ensure you have all updates. git init Git command used to initialize a new repository. It creates a hidden subfolder within the existing directory that houses the internal data structure required for version control.$ git init git status Git status is a command that displays the state of the working directory and staging area. It shows which files are staged, unstaged, or untracked. This command helps users to see what changes have been made and if they need to be committed.$ git status git log The 'git log' command displays the commit history of a repository. It shows information such as commit hashes, author details, dates, and commit messages. By default, it lists commits from the current branch in reverse chronological order.$ git log git add The 'git add' command adds changes in the working directory to the staging area. It prepares a snapshot of the file for versioning and further commits.$ git add <file> git commit The 'git commit' command is used to record changes made to the repository. It creates a snapshot of the changes and adds them to the project history. The '-m' flag allows adding a message along with the commit, providing context for the changes being made.$ git commit -m 'Added new feature' git clone Git clone is used to create a copy of an existing Git repository. It allows users to obtain a local copy of the entire project including all branches and commit history. The command also sets up remote tracking, making it easier for collaboration.$ git clone <repository URL> git push The 'git push' command is used to upload local repository content to a remote repository. It updates the remote branch with commits made on the local branch. The syntax for this command is typically 'git push <remote> <branch>'. To simply update the current working branch in the remote, you can use 'git push origin HEAD'.$ git push origin master git pull Git pull is used to fetch and download content from a remote repository and immediately update the local repository with any new commits. It combines git fetch and git merge, bringing changes from the remote branch into your current working branch.$ git pull origin main git log The 'git log' command displays the commit history of a repository. It shows information such as author, date, and commit message for each commit. Use '--oneline' to display each commit on a single line with its abbreviated hash and subject.$ git log
commit c1d2e3f4a5b6...
Author: John Doe <johndoe@email.com>
Date: Mon Sep 20 ...
Updated README.md git checkout Git command used to switch branches or restore working tree files. It updates the state of the repository and the working directory.$ git checkout <branch_name> git reset Git reset is a command used to undo changes and move the HEAD and current branch pointer. It can be used to unstage files, change commit history, or discard commits entirely.$ git reset --soft HEAD^ git revert Git revert is used to undo a previous commit by creating a new commit. It does not erase history but instead creates an inverse commit, effectively reverting the changes introduced by the original one.git revert <commit-hash> Discard Local Changes in a Specific File Git provides the 'git checkout -- <file>' command to discard local changes in a specific file. This action cannot be undone, so it's crucial to use this command with caution. It reverts the specified file back to its state at the last commit.$ git checkout -- filename.txt git clean Git clean is used to remove untracked files from the working directory. It helps in keeping the working directory free of unnecessary or unwanted files that are not being tracked by Git. Use caution with this command as it permanently deletes untracked files.$ git clean -n
Would show which files would be removed without actually removing them. Remove untracked files with git clean The 'git clean' command is used to remove untracked files from the working directory and staging area. Use '-f' flag for force deletion or use '-n' for a dry run, which shows what would be deleted without actually deleting anything.$ git clean -f
$ git clean -n Stash Changes with git stash Git stash is used to temporarily store changes that are not ready to be committed. This allows you to switch branches or perform other tasks without committing half-done work.$ git stash
Saved working directory and index state \"WIP on branchname: commit message\" Use Interactive Rebase to Remove Commits Interactive rebase allows you to modify commit history by removing, editing, or squashing commits. To remove specific commits, use the interactive rebase command with the -i flag followed by the parent of the oldest commit you want to remove. In the interactive rebase editor that opens up, delete lines representing unwanted commits and save your changes.$ git rebase -i <commit> |