Sunday, 22 December 2019

Streamlining the usage of GITHUB


Streamlining the usage of Github with Commands
1. Start a new git repository under one directory locally
In this step, we need to build a local git repository and decide which files will be added.
A new repo from scratch
¯  Create a directory to contain the project.
¯  Go into the new directory.
¯  Type git init. , use rm -rf .git to reverse this operation
¯  Write some code.
¯  Type git add . to add all the files if you do want to track all of them.
A new repo from an existing project
Say you have got an existing project that you want to start tracking with git.
  • Go into the directory containing the project. If in this repo, there is already a git that belongs to other authors, then use rm -rf .git to remove tis git and start your own.
  • Type git init.
  • Type git add to add all of the relevant files.

   git add -n . #to see what will be added
Note: If we have files that larger than 100M, use gitignore
You’ll probably want to create a .gitignore file right away, to indicate all of the files you don’t want to track. The .gitignore file is not added to a repository by default.
Ø  The problem is that .gitignore ignores just files that weren't tracked before (by git add). Run git reset name_of_file to unstage the file and keep it.
Ø  In case you want to also remove given file from the repository (after pushing). These files are libraries/documentation you don’t want to delete but also don’t want to push to github. Let say you have your project in folder your_project and a doc directory: your_project/doc.
1.      Remove it from the project directory (without actually deleting it): git rm --cached doc/*
2.      If you don’t already have a .gitignore, you can make one right inside of your project folder: project/.gitignore using echo "" > .gitignore or vi command
3.      Put doc/* in the .gitignore
4.      Stage the file to commit: git add .gitignore
5.      Commit: git commit -m "message".
6.      Push your change to github.
      2. Connect it to github and push files
             Now we have shown the github which file we want to track and submit.
             Type git commit -m "message" to initiate the submission.
             Additionally useful commands:

                      git status # to check the current state
You’ve now got a local git repository. You can use git locally, like that, if you want. But if you want the thing to have a home on github, do the following.
  • Go to github.
  • Log in to your account.
  • Click the new repository button in the top-right. You’ll have an option there to initialize the repository with a README file, but I don’t.
  • Click the “Create repository” button.
Now, follow the second set of instructions, “Push an existing repository…”, to use ssh connection, (if you set up ssh in “Your first time”, then you won’t have to type your password every time you push things to github)


$ git remote add origin git@github.com:username/new_repo.git
$ git push -u origin master
To use https connection, you’ll have to type your github password every time you push to github.

$ git remote add origin https://github.com/username/newrepo.git
$ git push -u origin master

To definitely be able to login using https protocol, you should first set your authentication credential to the git Remote URI:

git remote set-url origin

https://yourusername@github.com/user/repo.git
Then you’ll be asked for a password when trying to git push. If this error pop out, error: src refspec master does not match any.
Problem                                               
When pushing a new repository to Bitbucket Server, the following error is returned from the Git client:

git push -u origin mastererror: src refspec master does not match any.
error: failed to push some refs to 'http://stash.company.com/scm/PROJECT/REPO.git'
Cause
This is caused by the repository still being empty. There are no commits in the repository and thus no master branch to push to the server.
Resolution
Create the first commit inside of the repository and then it can be pushed. For example, the following with create an initial commit and push it to the server.

touch initialgit add initialgit commit -m "initial commit"
git push -u origin master
If we remote github is different comprared with local, push with — force

git push origin <your_branch_name> --force
If error fatal: remote origin already exists prompts out, As the error message indicates, there is already a remote configured with the same name. So you can either add the new remote with a different name or update the existing one if you don’t need it:
Check the current configuration:

vi .git/config
Update the existing one:
you can do a $ git remote -v to see what that origin which already exists is, If you think this is there by some error, you can update it like so:

$ git remote set-url origin git@github.com:username/first_app.git
Or another option is to do it in the config files.
To add a new remote:
called for example github instead of origin (which obviously already exists in your system), do the following:

$ git remote add github git@github.com:ppreyer/first_app.git
Remember though, everywhere in the tutorial you see “origin” you should replace it with “github”.
For example 
$ git push origin master should now be 
$ git push github master.
IF we deleted directories and what to update again:

How to delete files permanently from your local and remote git repositories
If you frequently have to set up git version control system for existing projects then you would very likely run into the problem of having to remove files, which should not have been in git, from git after they were added. Running a git rm will only delete the file from the head and the file would still remain in the repository. This may be alright for small files but this could be troublesome for large files as these would unnecessarily bloat the git repository. But don't worry git being a 'swiss army knife' of version control systems there is a solution to delete files permanently from both your local and remote git repositories.
Do note that this is not a newbie solution and you have to do your bit of reading up on these commands and their implications before you actually run this on a production repository. Also note that you have to have RW+ permissions on the repository to do non-fast-forward pushes
CD to your local working folder and run the following command
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME" -- --all
replace FOLDERNAME with the file or folder you wish to remove from the given git repository.
Once this is done run the following commands to clean up the local repository

rm -rf .git/refs/original/
git ref log expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now

Now push all the changes to the remote repository
git push --all --force
This would clean up the remote repository.
Now you will have to recreate the .git folders of all the working copies of the repository. This would include cloned repositories by other developers or the cloned repositories at the live and dev sites. You can do this by cloning the repository afresh into a temporary folder and the move the .git folder from the newly cloned repository to the local repository after deleting the existing .git folder from in there. Do remember to checkout the same branch as is already checked out in the local repository in the temporary repository before you move the .git folder to the local repository.
Remember to make a backup copy of the latest repository and the latest local files before you do any of these things so that you can fall back to a working repository if you run into issues.
3. Others
Switching remote URLs from SSH to HTTPS
Open Git Bash.
Change the current working directory to your local project.
List your existing remotes in order to get the name of the remote you want to change.

git remote -v origin git@github.com:USERNAME/REPOSITORY.git (fetch)
origin git@github.com:USERNAME/REPOSITORY.git (push)
Change your remote’s URL from SSH to HTTPS with the git remote set-url command.

git remote set-url origin

https://github.com/USERNAME/REPOSITORY.git
Verify that the remote URL has changed.
git remote -v # Verify new remote URL origin

https://github.com/USERNAME/REPOSITORY.git (fetch) origin https://github.com/USERNAME/REPOSITORY.git (push)
The next time you git fetchgit pull, or git push to the remote repository, you'll be asked for your GitHub username and password.
If you have two-factor authentication enabled, you must create a personal access token to use instead of your GitHub password.
You can use a credential helper so Git will remember your GitHub username and password every time it talks to GitHub.
Switching remote URLs from HTTPS to SSH
Open Git Bash.
Change the current working directory to your local project.
List your existing remotes in order to get the name of the remote you want to change.
git remote -v origin https://github.com/USERNAME/REPOSITORY.git (fetch) origin https://github.com/USERNAME/REPOSITORY.git (push) 
Change your remote’s URL from HTTPS to SSH with the git remote set-url command.

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
Verify that the remote URL has changed.

git remote -v # Verify new remote URL origin

git@github.com:USERNAME/REPOSITORY.git (fetch) origin

git@github.com:USERNAME/REPOSITORY.git (push)
To Track Forked Repo
We need this if we forked a branch from others’ github, we might have the origin already existed.
origin  https://github.com/CLOUD/Fast-Arena-CAAE.git (fetch)
origin  https://github.com/CLOUD/Fast-Arena-CAAE.git (push)
$ git checkout -b branch_name #give it a new branch name
This command will build a branch and get into this branch. Then we do
$ git add .
$ git commit .
$ git push origin branch_name


No comments:

Post a Comment

Streamlining the usage of GITHUB

Streamlining the usage of Github with Commands 1. Start a new git repository under one directory locally In this step, we need to bui...