Code repo management
First-time Get a Local Repo
1. Create a New Repository on GitHub
- Click on the + icon in the top-right corner of the page and select New repository.
- Enter a Repository name and an optional Description.
- Choose between a Public or Private repository.
- (Optional) Add a .gitignore file, a license, and a README file.
- Click Create repository.
2. Initialize a Local Repository
-
Open Terminal or Command Prompt:
Navigate to the directory where your source code is located.
- Initialize Git:
git init
- Add Files:
git add [file_name]
- Tips:
1.) use . to add all files.
git add .
2.) Then use [git rm] to remove the unnecessary files/folder
git rm -r [folder_name] git rm [file_name]
- Tips:
1.) use . to add all files.
- Commit Your Files:
git commit -m "Initial commit"
3. Connect Your Local Repository to GitHub
- Add Remote Repository:
- Copy the HTTPS or SSH URL of your GitHub repository from the GitHub website.
- In your terminal or command prompt, run:
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.git
4. Push Your Code to GitHub
- Push to Github:
git push -u origin master
- Verify Your Upload
2.1 Check GitHub:
- Go to your GitHub repository page.
- You should see all your source code files uploaded.
Additional Steps (Optional)
- Create and Switch to a New Branch:
git checkout -b new-branch-name
Then, push this branch to Github:
git push -u origin new-branch-name
-
Pull changes from GitHub:
If you want to fetch and merge changes from the GitHub repository to your local repository, use:
git pull origin master
-
Collaborate with Others:
Add collaborators in your GitHub repository settings if it’s a team project.
Git Status
Check Git Status
git status
What git status Shows You
- Untracked Files: These are files in your working directory that are not being tracked by Git. If you want Git to track them, you need to add them using git add.
1
2
3
4
5
Untracked files:
(use "git add <file>..." to include in what will be committed)
somefile.txt
anotherfile.py
-
Changes Not Staged for Commit: These are modifications to tracked files that haven’t been staged yet. You can stage them using git add.
Example:
1
2
3
4
5
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: example.txt
- Changes to Be Committed: These are files that have been staged and are ready to be committed. These changes will be included in the next commit.
Example:
1
2
3
4
5
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: newfile.txt
modified: anotherfile.py
- Clean Working Directory: If there are no changes in the working directory or the staging area, Git will report that your working directory is clean.
Example:
1
2
3
4
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Trouble Shooting
CONFLICT
- Conflict (content): Merge conflict in ***.py
1
git status
1
2
3
4
5
<<<<<<< HEAD
# Your changes
=======
# Changes from the other branch
>>>>>>> other-branch
1
git add path/to/resolved-file
1
2
git commit -m "conflict resolved."
git push origin master
- Conflict (modify/delete): Merge conflict in /*.py The conflicts in your output indicate situations where a file was deleted in one branch and modified in another (modify/delete conflicts). In these cases, you’ll need to decide whether to keep the deletion, keep the modifications, or manually integrate the changes.
1
rm -r path/to/your/folder(files)
1
git rm -r path/to/your/folder