Git Archives - Code Snippets https://www.mohamedkadi.com/snippet/category/git/ Fast Coding without memorizing Thu, 02 Nov 2023 11:25:35 +0000 en-US hourly 1 https://wordpress.org/?v=6.7 216119969 Mastering Time Travel in Git: Returning to a Previous Commit with Finesse https://www.mohamedkadi.com/snippet/mastering-time-travel-in-git-returning-to-a-previous-commit-with-finesse/ https://www.mohamedkadi.com/snippet/mastering-time-travel-in-git-returning-to-a-previous-commit-with-finesse/#respond Tue, 23 May 2023 11:27:55 +0000 https://www.mohamedkadi.com/snippet/?p=54 Master the art of revisiting past commits with our three essential tips. Learn how to locate the desired commit, navigate your local environment, and efficiently manage branches. Harness the power of Git to streamline your development workflow and maintain control over your project’s history. Tips for Going Back to a Previous Commit in Git: Find […]

The post Mastering Time Travel in Git: Returning to a Previous Commit with Finesse appeared first on Code Snippets.

]]>
Master the art of revisiting past commits with our three essential tips. Learn how to locate the desired commit, navigate your local environment, and efficiently manage branches. Harness the power of Git to streamline your development workflow and maintain control over your project’s history.

Tips for Going Back to a Previous Commit in Git:

  1. Find the desired commit either by using the command git log --oneline in your terminal or by reviewing the commit history on the GitHub website.
  2. Return to the selected commit on your local environment using the command git checkout <commit-id> ..
  3. Keep in mind that after going back to a previous commit, you will be in a “detached HEAD” state. Create a new branch using git checkout -b <new-branch-name> to continue working from there.
  4. If you want to abandon the changes and return to the latest commit, use git checkout <branch-name> to switch back to the desired branch.
  5. Remember that going back to a previous commit discards any changes made after that commit, so exercise caution and have backups if needed.

More Info (After you switch to commit-id):

You are in ‘detached HEAD‘ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

The post Mastering Time Travel in Git: Returning to a Previous Commit with Finesse appeared first on Code Snippets.

]]>
https://www.mohamedkadi.com/snippet/mastering-time-travel-in-git-returning-to-a-previous-commit-with-finesse/feed/ 0 54
Mastering Git Repository Initialization: Simplifying the Process to Begin with Your Current Commit https://www.mohamedkadi.com/snippet/mastering-git-repository-initialization-simplifying-the-process-to-begin-with-your-current-commit/ https://www.mohamedkadi.com/snippet/mastering-git-repository-initialization-simplifying-the-process-to-begin-with-your-current-commit/#respond Sun, 12 Mar 2023 03:36:32 +0000 https://www.mohamedkadi.com/snippet/?p=37 In short, here are the steps to make the current commit the only (initial) commit in a Git repository: Backup your repository: Ensure you have a backup of your repository’s current state as the following steps cannot be reverted. Remove all history and configuration: Execute the following commands: Save your <github-uri> by running cat .git/config. […]

The post Mastering Git Repository Initialization: Simplifying the Process to Begin with Your Current Commit appeared first on Code Snippets.

]]>

In short, here are the steps to make the current commit the only (initial) commit in a Git repository:

  1. Backup your repository: Ensure you have a backup of your repository’s current state as the following steps cannot be reverted.
  2. Remove all history and configuration: Execute the following commands:
    • Save your <github-uri> by running cat .git/config.
    • Delete the .git directory using rm -rf .git.
  3. Reconstruct the Git repository with the current content:
    • If you haven’t set up the init.defaultBranch configuration, use git config --global init.defaultBranch <branch-name>. For example, set main as the <branch-name>.
    • Initialize a new Git repository with git init.
    • Add all files to the repository using git add ..
    • Create the initial commit with git commit -m "Initial commit".
  4. Push to GitHub:
    • Add the remote origin by executing git remote add origin <github-uri>.
    • Push the changes with git push -u --force origin main.

Remember that this method is considered a brute-force approach and should not be used if your repository contains submodules. In such cases, it is recommended to use alternative methods like interactive rebase.

The post Mastering Git Repository Initialization: Simplifying the Process to Begin with Your Current Commit appeared first on Code Snippets.

]]>
https://www.mohamedkadi.com/snippet/mastering-git-repository-initialization-simplifying-the-process-to-begin-with-your-current-commit/feed/ 0 37