Initiate repo locally
git init
git add --all
git commit -m "Initial commit"
git remote add origin [SSH link]
git branch -M main
git push -u origin main
Pushing to GitHub
git add --all
git commit -m "state changes made"
git push origin main
Some useful repo commands
git clone [SSH link]
- Cloning GitHub repo to local computer
rm -rf .git
- Make a local repo into non-repo directory
Undo changes
Discard changes
- Warning: Be very careful, it's final.
git restore .
- Undo all current changes. Discard all saved but not pushed files.
git clean -f
- Remove all new (untracked) files
Stash changes
git stash --include--untracked
- Stash changes for later use. Working copy will be "clean" and the not-so-wanted changes are stashed.
git stash pop
- Reapply the last saved state and also delete and clean it from the Stash
Also read
Time Travel in Your Project: Undo Changes with Git
Changing repo folder name
- After repo name is changed, for example from cheatsheets to codingnotes and then into codeyluwak, even though local can track and update the remote correctly, we should still changing the link into the new correct URL.
git remote set-url
(existing remote name) (the new URL)
- Existing remote name:
origin
, upstream
, etc. Check it at .git configuration.
- URL: SSH or HTTPS. Check it out at the github page.
- In the case of this Codey Luwak, remote name is
origin
and it is using SSH link.
- Terminal commands:
cd .git
less config
or more config
git remote set-url origin git@github.com:dinarosita/codeyluwak.git
Branch
Organizing a project with branches
main
branch - Keep only finished features here.
- feature branches - Where we are developing each feature to the project. When we are done, we "merge" it to the main branch
Commands
git branch
- See all branches. Current location is marked by *
.
git branch [branchName]
- Make a new branch.
git checkout [branchName]
- Change to a branch.
git checkout main
- Change back to "main" branch. Remember that "main" is a branch name too.
git checkout -b [branchName]
- Make a branch and change to it.
git merge [branchName]
- Take the changes have been committed in branchName
and add them to the current branch.
git branch -d [branchName]
- Delete branch that's already been merged into main
. If it hasn't been merged, it will be refused.
git branch -D [branchName]
- Force deletion of a branch that hasn't been merged into main
.
git push --delete origin [branchName]
- delete the branch located in the remote repo on GitHub
Merging conflict
- Happens when a line in a file is changed in 2 different branches. When occured, merging can't be finished.
- To finish the merging, first we have to resolve the conflict