Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, November 26, 2024

How to Copy Git Repository Without History

There are several methods to do this using git clone, git push or using git archive. But I personally prefer the one using git clone.

Objective is to copy repo 1 which is source repo to a new Repo which is NewRemote repo with out commit history.

Precautions before you proceed with this:

  1. Ensure you have write access to the repository.
  2. Backup any important local changes before proceeding.
  3. This will permanently remove the old commit history.
  4. Collaborators will need to re-clone the repository.

Here are step by step git examples for this specific repo

# 1. Clone the source repository
git clone https://github.com/inagasai/SourceRepo.App.git

# 2. Enter the cloned repository directory
cd vGlence.App

# 3. Verify current branches
git branch -a

# 4. Checkout master branch
git checkout master

# 5. Create a new branch without history
git checkout --orphan clean-main

# 6. Add all files to the new branch
git add .

# 7. Commit the files with a new initial commit
git commit -m "Initial commit - reset repository history"

# 8. Delete the old main branch (if it exists)
git branch -D main 2>/dev/null

# 9. Rename current branch to main
git branch -m main

# 10. Remove the original remote
git remote remove origin

# 11. Add the original repository as a new remote
git remote add origin https://github.com/inagasai/NewRemote.App.git

# 12. Force push to overwrite the remote repository
git push -f origin main
  

Detailed Breakdown of the outcome:

  1. This process creates a new branch with no commit history.
  2. It adds all existing files to a new initial commit.
  3. Force pushes to overwrite the remote repository.
  4. Removes all previous commit history.

Hope this helps.

Tuesday, June 27, 2023

Git Cheat Sheet: Essential Commands for Version Control Mastery

Git is a powerful and widely used version control system that enables developers to efficiently manage their codebase and collaborate on projects. However, mastering Git can be a daunting task, especially for beginners. To ease your learning curve, we've prepared a comprehensive Git cheat sheet that includes the most essential commands you'll need to navigate through Git's functionalities. Whether you're a novice or an experienced developer, this cheat sheet will serve as a handy reference to help you streamline your version control workflow.

Git Configuration:

  • git config --global user.name "[name]": Set your username for Git.
  • git config --global user.email "[email address]" : Set your email address for Git.
  • git config --global color.ui auto: Enable colorful output in Git.

Repository Creation and Cloning:

  • git init: Create a new Git repository in the current directory.
  • git clone [repository URL]: Clone an existing repository to your local machine.

Basic Workflow:

  • git add [file]: Add a file to the staging area.
  • git commit -m "[commit message]": Commit your changes with a descriptive message.
  • git status: Check the status of your repository.
  • git log: View the commit history.
  • git diff: Show the differences between your working directory and the last commit.

Branching and Merging:

  • git branch: List all branches in the repository.
  • git branch [branch name]: Create a new branch.
  • git checkout [branch name]: Switch to a different branch.
  • git merge [branch name]: Merge a branch into the current branch.
  • git stash: Temporarily save changes that you don't want to commit yet.

Remote Repositories:

  • git remote add [remote name] [remote URL]: Add a remote repository.
  • git push [remote name] [branch name]: Push your local changes to a remote repository.
  • git pull [remote name] [branch name]: Fetch changes from a remote repository and merge them into your local branch.

Collaboration:

  • git branch -r: List remote branches.
  • git fetch: Download objects and refs from a remote repository.
  • git branch -d [branch name]: Delete a branch.
  • git clone --branch [branch name] [repository URL]: Clone a specific branch of a repository.

Undoing Changes:

  • git reset [commit]: Un stage commits, preserving changes.
  • git revert [commit]: Create a new commit that undoes changes from a previous commit.
  • git checkout -- [file]: Discard changes in a specific file.

This Git cheat sheet provides you with a quick reference to the most commonly used commands for version control. By familiarizing yourself with these commands, you'll be able to navigate Git's functionalities with ease, collaborate effectively, and maintain a clean and organized codebase. Remember, practice makes perfect, so don't hesitate to experiment and explore additional features and options available in Git. Happy coding!

Please consider this cheat sheet as a starting point for your Git journey, and continue to expand your knowledge by exploring additional resources and documentation.

Sunday, June 13, 2021

Git: How to set Git User Name and User email Globally & repository specific

Usually when we install Git, we typically configure your global username and email address after installing Git. However, you can do so now if you missed that step or want to make changes. After you set your global configuration, repository-specific configuration is optional.

Git configuration works the same across Windows, macOS, and Linux.

To set your global username/email configuration:

Open the command line.

-- set user name
$ git config --global user.name "First Name Last Name"
-- set user email
$ git config --global user.email "useremail@gmail.com"

To set repository-specific username/email configuration:

From the command line, change into the repository directory.

--Set your username
$ git config user.name "FIRST_NAME LAST_NAME"

--Set your email address
$ git config user.email "useremail@gmail.com"

--Verify your configuration by displaying your configuration file
$ cat .git/config

Hope this helps!

Monday, September 10, 2018

Git Cheat sheet for all

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. Git is one of the, if the not the, most popular version control systems available. Its used by millions of projects across all languages.

Remembering all those commands to perform common git tasks can be a bit of a nightmare for anyone.

Creating Repositories

# create new repository in current directory
git init

# clone a remote repository
git clone [url]
# for example cloning the entire jquery repo locally
git clone https://github.com/inagasai/nsmodel


Branches and Tags

# List all existing branches with the latest commit comment 
git branch –av

# Switch your HEAD to branch
git checkout [branch]

# Create a new branch based on your current HEAD
git branch [new-branch]

# Create a new tracking branch based on a remote branch
git checkout --track [remote/branch]
# for example track the remote branch named feature-branch-foo
git checkout --track origin/feature-branch-foo

# Delete a local branch
git branch -d [branch]

# Tag the current commit
git tag [tag-name]


Local Changes

# List all new or modified files - showing which are to staged to be commited and which are not 
git status

# View changes between staged files and unstaged changes in files
git diff

# View changes between staged files and the latest committed version
git diff --cached
# only one file add the file name
git diff --cached [file]

# Add all current changes to the next commit
git add [file]

# Remove a file from the next commit
git rm [file]

# Add some changes in < file> to the next commit
# Watch these video's for a demo of the power of git add -p - http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
git add -p [file]

# Commit all local changes in tracked  files
git commit –a
git commit -am "An inline  commit message"

# Commit previously staged changes
git commit
git commit -m "An inline commit message"

# Unstages the file, but preserve its contents

git reset [file]


Commit History

# Show all commits, starting from the latest 
git log 

# Show changes over time for a specific file 
git log -p [file]

# Show who changed each line in a file, when it was changed and the commit id
git blame -c [file]


Update and Publish

# List all remotes 
git remote -v

# Add a new remote at [url] with the given local name
git remote add [localname] [url]

# Download all changes from a remote, but don‘t integrate into them locally
git fetch [remote]

# Download all remote changes and merge them locally
git pull [remote] [branch]

# Publish local changes to a remote 
git push [remote] [branch]

# Delete a branch on the remote 
git branch -dr [remote/branch]

# Publish your tags to a remote
git push --tags


Merge & Rebase

# Merge [branch] into your current HEAD 
git merge [branch]

# Rebase your current HEAD onto [branch]
git rebase [branch]

# Abort a rebase 
git rebase –abort

# Continue a rebase after resolving conflicts 
git rebase –continue

# Use your configured merge tool to solve conflicts 
git mergetool

# Use your editor to manually solve conflicts and (after resolving) mark as resolved 
git add <resolved- file>
git rm <resolved- file>


Undo

# Discard all local changes and start working on the current branch from the last commit
git reset --hard HEAD

# Discard local changes to a specific file 
git checkout HEAD [file]

# Revert a commit by making a new commit which reverses the given [commit]
git revert [commit]

# Reset your current branch to a previous commit and discard all changes since then 
git reset --hard [commit]

# Reset your current branch to a previous commit and preserve all changes as unstaged changes 
git reset [commit]

#  Reset your current branch to a previous commit and preserve staged local changes 
git reset --keep [commit]

Hope this helps for quick reference!!

Monday, January 09, 2017

Top 20 Developer Tools of 2016

Here are the list of top 20 Developer utilities that are must to have for Developer.

  1. GitKraken: The downright luxurious Git GUI client for Windows, Mac, and Linux.
  2. Atom: A hackable text editor for the 21st Century.
  3. VS Code: A free, lightweight tool for editing and debugging web apps.
  4. Git: A free and open source distributed version control system.
  5. GitHub: A web-based Git repository hosting service.
  6. Visual Studio: Developer tools and services for any platform with any language.
  7. Sublime Text: A sophisticated text editor for code, markup, and prose.
  8. Chrome DevTools: A set of web authoring and debugging tools built into Google Chrome.
  9. Docker :An open platform for developers and system administrators to build, ship, and run distributed applications.
  10. GitLab: Git repository management, code reviews, issue tracking, activity feeds, and wikis.
  11. IntelliJ IDEA: A Java IDE.
  12. PhpStorm : A commercial, cross-platform IDE for PHP.
  13. Postman: A powerful GUI platform to make your API development faster & easier.
  14. ReSharper : A Visual Studio extension for .NET developers.
  15. Slack: Real-time messaging, archiving, and search for modern teams.
  16. PyCharm: An IDE used specifically for Python.
  17. Android Studio: The official IDE for Android platform development.
  18. Notepad++: A free source code editor which supports several programming languages running under the MS Windows environment.
  19. Xcode : an IDE for macOS/and iOS development.
  20. Stack Overflow: The largest online community for programmers to learn, share their knowledge, and advance their careers.

Sunday, December 11, 2016

Git Bash Commands Overview

Version Control Systems are software's tools that help a software team to manage changes to their source code. It keeps track of every modification to the code. If a mistake is made, developers can compare earlier versions of the code and/or revert back the changes to help fix the mistakes without disrupting the other team members.

As of now, the most widely used modern version control systems in the world are Git, TFS, SourceSafe and SVN.

Git is a mature, actively maintained open source project originally developed by Linus Torvalds, the famous creator of the Linux operating system kernel, in the year 2005.

Having a distributed architecture, Git is an example of "Distributed Version Control System" (DVCS). Rather than having only one single place for the full version history, every developer's working copy of the code can be treated as a repository in Git. In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

Here is a list of some common terms used in Git:

Blobs
Blob stands for Binary Large Object. Each version of a file is represented by blob, which holds the file data but doesn’t contain any metadata. It is a binary file and in Git database, it is named as SHA1 hash of that file.

Trees
Tree is an object as binary file, which represents a directory. It also holds blobs as well as other sub-directories. It stores references to blobs and trees which are also named as SHA1 hash of the tree object.

HEAD
HEAD is a pointer, which always points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit. The heads of the branches are stored in ".git/refs/heads/" directory.

Clone
Clone operation creates the local instance of the repository. It acts as mirroring of the complete remote repository. Users can perform any operations with this local repository. The only time networking gets involved is when the repository instances are being synchronized.

Branches
Branches are used to create another line of development from the repository's master branch to work on a new feature. Once the feature is completed, it is merged back with the master branch and we delete the branch. Every branch is referenced by HEAD, which points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit.

Pull
Pull operation copies the changes from a remote repository instance to the local repository. The pull operation is used for synchronization between two repository instances.
 
Push
Push operation copies changes from a local repository instance to the remote repository. This is used to store the changes permanently into the Git repository.
 
Commits
Commit operation holds the current state of the repository. A commit is also named by SHA1 hash code. Every commit object has a pointer to the parent commit object.
 
Tags
Tag assigns a meaningful name with a specific version in the repository. Tags are very similar to branches, but the difference is that tags are immutable. Once a tag is created for a particular commit, even if you create a new commit, it will not be updated. Usually, developers create tags for product releases.