Git - Most useful commands.

 Here is a cheat sheet of common Git commands that you may find useful:

  • git init: Initializes a new Git repository in the current directory.

  • git clone <repository>: Clones an existing Git repository from a remote source.

  • git add <file>: Adds a file to the staging area, preparing it for commit.

  • git commit -m "message": Commits the changes in the staging area with a commit message.

  • git push <remote> <branch>: Pushes the committed changes to the specified remote repository and branch.

  • git pull <remote> <branch>: Pulls the latest changes from the specified remote repository and branch.

  • git branch <name>: Creates a new branch with the specified name.

  • git checkout <branch>: Switches to the specified branch.

  • git merge <branch>: Merges the specified branch into the current branch.

  • git stash: Stashes the current changes, allowing you to switch branches without committing.

  • git stash pop: Applies the stashed changes and removes them from the stash.

git rebase is a Git command that allows you to apply a series of commits from one branch onto another branch. It is often used to synchronize branches or to clean up the commit history by modifying or reordering commits.

Git Rebase

  • git rebase <branch>: Applies the commits from the specified branch onto the current branch.

  • git rebase --onto <newbase> <upstream> <branch>: Rebases the specified branch onto the specified new base, using the upstream branch as a reference.

  • git rebase -i <commit>: Opens an interactive rebase session, allowing you to modify or reorder the commits starting from the specified commit.

  • git rebase --continue: Continues the rebase process after resolving conflicts.

  • git rebase --abort: Aborts the rebase process and restores the original state of the branches.

It's important to note that git rebase can modify the commit history and may cause conflicts if the same lines of code have been modified on both branches. It's a good idea to use git rebase with caution and to make sure you have a backup of your work before using it.

Git Cherry-pick



git cherry-pick
is a Git command that allows you to apply the changes introduced by a specific commit to your current branch. It is often used to selectively apply changes from one branch to another, or to apply changes from a commit that is not yet merged into the branch.

Here is an example of how to use git cherry-pick:

  1. Check out the branch you want to apply the changes to.

  2. Find the commit you want to cherry-pick using git log or git show. Make note of the commit hash.

  3. Use the git cherry-pick command to apply the changes introduced by the commit:

git cherry-pick <commit hash>
  1. If there are conflicts, resolve them and use git add to stage the resolved files.

  2. Use git cherry-pick --continue to finish the cherry-pick process.

It's important to note that git cherry-pick can modify the commit history and may cause conflicts if the same lines of code have been modified on both branches. It's a good idea to use git cherry-pick with caution and to make sure you have a backup of your work before using it.

Git Merge

git merge is a Git command that allows you to combine the changes from two or more branches into a single branch. It is often used to merge changes from a feature branch back into the main branch, or to synchronize multiple branches.

Here is an example of how to use git merge to merge changes from one branch into another:

  1. Check out the branch you want to merge the changes into. This will be the "destination" branch.

  2. Use the git merge command to merge the changes from the other branch into the destination branch:

git merge <source branch>

For example, to merge the changes from the feature branch into the main branch, you would use the following command:

git checkout main git merge feature
  1. If there are conflicts, resolve them and use git add to stage the resolved files.

  2. Use git commit to create a new commit that includes the merged changes.

By default, git merge will create a new commit that combines the changes from both branches. If you prefer to preserve the individual commits from the source branch, you can use the --no-commit option and manually commit the merged changes later.

I hope this helps you understand how to use git merge to combine the changes from multiple branches. Let me know if you have any questions or need further assistance.

Get Edit or Undo commits


It is generally not recommended to delete or edit commits in Git, as this can cause problems with the commit history and make it difficult to track changes. However, there are a few situations in which it may be necessary to modify a commit:

  • Amending a commit message: If you notice a typo or want to make a minor change to the commit message, you can use the git commit --amend command to modify the message of the most recent commit.

  • Modifying a commit: If you need to make changes to a commit that is not the most recent one, you can use the git rebase command in interactive mode (git rebase -i <commit>) to modify or reorder the commits in the branch.

  • Deleting a commit: If you want to delete a commit entirely, you can use the git rebase command in interactive mode (git rebase -i <commit>) to delete the commit. Alternatively, you can use the git revert command to create a new commit that undoes the changes introduced by the commit you want to delete.

It's important to note that modifying or deleting commits can cause problems with the commit history and may cause conflicts if the same lines of code have been modified on multiple branches. It's a good idea to use these commands with caution and to make sure you have a backup of your work before using them.


Comments

Popular posts from this blog

Java GC and memory leaks, Thread and System performance