Know How: Git Usage
To creat a repository of an exsisting project in github from your local server:
-
Create a new repository in Github website. To avoid errors, do not initialize the new repository with README, license, or gitignore files. (This is important as I did mistakes at the beginning after clicking add README)
- Use the init command to initialize the local directory as a Git repository. For Git 2.27.1 or an earlier version, set the name of the default branch using && git symbolic-ref HEAD refs/heads/main:
$ git init && git symbolic-ref HEAD refs/heads/main
- Add the files in your new local repository. This stages them for the first commit.
$ 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.
- In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin REMOTEURL
$ git remote -v
#Verifies the new remote URL - Push the changes in your local repository to GitHub.com.
$ git push origin main
References:
To merge changes from the remote repo to local, such as after readme file was added in the remote repo:
- Pulling changes from a remote repository
$ git pull origin main
To add the README file locally:
- First creat the README.md:
$ touch README.md
- Then add, commit and push the changes:
$ git add .
$ git commit -m “added readme”
$ git push origin main
To merge changes from remote to local but the local files was also changed:
- If you want remove all local changes - including files that are untracked by git - from your working copy, simply stash them:
$ git stash push –include-untracked
- If you don’t need them anymore, you now can drop that stash:
$ git stash drop
- Then you can pull from remote without errors:
$ git pull origin main
References:
Or
$ git fetch
$ git reset –hard HEAD
$ git merge ‘@{u}’
To sync with the local, push changes from the local to remote:
- Add the updated files in your local repository, which tells Git that you want to include all of your changes in the next commit.
$ git add .
- Git commit takes a snapshot of those changes.
$ git commit -m “description of changes”
- To push the current branch and set the remote as upstream.
$ git push –set-upstream origin main
References:
Clone from a remote repository:
- Using git clone cmd
$ git clone REMOTEURL
Push git cloned repository to your own on GitHub:
After you have cloned a remote repository to your own repository, and then push to your own remote server, you will find a white arrow in the github repository and you cannot open it and see the contents inside. This white arrow means it is a nested folder. To remove it and show the contents, do:
- If you don’t care about the history of that folder, delete locally its .git subfolder inside that folder using
$ rm -rf .git
- Then you would need to delete the gitlink entry:
$ git rm –cache sub_folder_name
- Finally, you can add, commit and push that folder content.
References: