Breaking Posts

6/trending/recent
Type Here to Get Search Results !

Adding locally hosted code to GitHub

 If your code is stored locally on your computer, you can import the code to GitHub using GitHub CLI or Git commands.

Windows

Warning: Never git addcommit, or push sensitive information to a remote repository. Sensitive information can include, but is not limited to:


Adding a local repository to GitHub with GitHub CLI

  1. In the command line, navigate to the root directory of your project.

  2. Initialize the local directory as a Git repository.

    $git init -b main
  1. Stage and commit all the files in your project.

    $git add . && git commit -m "initial commit"

Adding a local repository to GitHub using Git


Create a new repository on GitHub.com. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.


  1. Open Git Bash.

  2. Change the current working directory to your local project.

  3. Use the init command to initialize the local directory as a Git repository. By default, the initial branch is called main.

    If you’re using Git 2.28.0 or a later version, you can set the name of the default branch using -b.

    $ git init -b main

If you’re using Git 2.27.1 or an earlier version, you can set the name of the default branch using && git symbolic-ref HEAD refs/heads/main.

$ git init && git symbolic-ref HEAD refs/heads/main
$ git add .

Commit the files that you've staged in your local repository.

$ git commit -m "First commit
At the top of your repository on GitHub.com's Quick Setup page, click  to copy the remote repository URL.
$ git remote add origin <REMOTE_URL>
# Sets the new remote
$ git remote -v
# Verifies the new remote URL

Push the changes in your local repository to GitHub.com.

$ git push origin main

If you get erro like yello box then run command 
$ git push origin master

git pull and git fetch


  • git pull: Update your local working branch with commits from the remote, and update all remote tracking branches.
It is always a good idea to run git status - especially before git pull

 You can find this commit by searching the git reflog

Undo A git pull

To effectively "undo" a git pull, you cannot undo the git fetch - but you can undo the git merge that changed your local working branch.

To do this, you will need to git reset to the commit you made before you merged. You can find this commit by searching the git reflog. The reflog is a log of every place that HEAD has pointed - every place that you have ever been checked out to. This reflog is only kept for 30 to 90 days, depending on the commit, and is only stored locally. (The reflog is a great reason not to delete a repository if you think you've made a mistake!)

Run git reflog and search for the commit that you would like to return to. Then, run git reset --hard <SHA> to reset HEAD and your current branch to the SHA of the commit from before the merge.


Note: You can find the remotes with git remote -v, and see all available remote tracking branches with git branch --all.


  • git status: Always a good idea, this command shows you what branch you're on, what files are in the working or staging directory, and any other important information.
  • git branch: This shows the existing branches in your local repository. You can also use git branch [banch-name] to create a branch from your current location, or git branch --all to see all branches, both the local ones on your machine, and the remote tracking branches stored from the last git pull or git fetch from the remote.
  • git push: Uploads all local branch commits to the remote.
  • git log: Browse and inspect the evolution of project files.
  • git remote -v: Show the associated remote repositories and their stored name, like origin.




git push

  • git push -f: Force a push that would otherwise be blocked, usually because it will delete or overwrite existing commits (Use with caution!)
  • git push -u origin [branch]: Useful when pushing a new branch, this creates an upstream tracking branch with a lasting relationship to your local branch
  • git push --all: Push all branches
  • git push --tags: Publish tags that aren't yet in the remote repository



Work was not yet on any branch

  1. Create and checkout to a new branch from your current commit: git checkout -b [branchname]
  2. Then, push the new branch up to the remote: git push -u origin [branchname]




Accidentally committed to the wrong branch

  1. Checkout to the branch that you intended to commit to: git checkout [branchname]
  2. Merge the commits from the branch that you did accidentally commit to: git merge [main]
  3. Push your changes to the remote: git push
  4. Fix the other branch by checking out to that branch, finding what commit it should be pointed to, and using git reset --hard to correct the branch pointer


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.