Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Last active August 23, 2023 14:00
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment