Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Last active December 11, 2024 23:15
Show Gist options
  • Save bijay-shrestha/f57d5424b8935d0f68d307336942ddd7 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/f57d5424b8935d0f68d307336942ddd7 to your computer and use it in GitHub Desktop.

GitHub

Add Github repository to local drive

echo "# error-handling" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/bijay-shrestha/error-handling.git
git push -u origin master

Adding an existing project to GitHub using the command line

Follow this link

Removing .idea folder from the remote

echo '.idea' >> .gitignore
git rm -r --cached .idea
git add .gitignore
git commit -m '<write some message stating the file is removed>'
git push

How do I revert a Git repository to a previous commit?

$ git reset --hard <commidId> && git clean -f

Remove Git branch from local and remote

$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>

Note that in most cases the remote name is origin.

Removing a remote

Use the git remote rm command to remove a remote URL from your repository.

The git remote rm command takes one argument:

A remote name, for example, destination

$ git remote -v
# View current remotes
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)
> destination  https://github.com/FORKER/REPOSITORY.git (fetch)
> destination  https://github.com/FORKER/REPOSITORY.git (push)

$ git remote rm destination
# Remove remote
$ git remote -v
# Verify it's gone
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

How to deal with "refusing to merge unrelated histories" error

git pull origin master --allow-unrelated-histories

Rename a Git branch with the -m command option

git branch -m new-name

Exclude Files from Git Push

git update-index --assume-unchanged <file>

Remove 'node_modules' from Git Repo

#add 'node_modules' to .gitignore file

git rm -r --cached node_modules
git commit -m 'Remove the now ignored directory node_modules'
git push origin master

How do I rename both a Git local and remote branch name?

1. Rename your local branch:

If you are on the branch you want to rename: git branch -m new-name

If you are on a different branch: git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch:

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch:

Switch to the branch and then: git push origin -u new-name

4. Amend multiple Commit message:

To amend the commit messages of the last two commits in Git, follow these steps:

##Step 1: Use Interactive Rebase Run the following command to start an interactive rebase for the last two commits:

git rebase -i HEAD~2

##Step 2: Edit the Commit Messages In the interactive rebase interface:

You'll see something like this:

pick def456 Second commit message```
Change the word pick to reword for the commits whose messages you want to edit:

```reword abc123 First commit message
reword def456 Second commit message```

Save and close the editor (usually by pressing :wq in vim).

##Step 3: Update the Commit Messages
Git will now let you edit the messages one by one:

For the first commit, it will open the commit message editor. Modify the message as needed, save, and close.

Then it will do the same for the second commit.

##Step 4: Complete the Rebase
After editing both commit messages, Git will complete the rebase. If there are no conflicts, you're done!

##Step 5: Push Changes (If Necessary)
If you already pushed these commits to a remote branch, you'll need to force push the updated history:

`git push --force`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment