Before you start using Git, you need to configure your name and email, as they will be associated with your commits.
This sets the global username for your Git configuration.
git config --global user.name "Stelios Sotiriadis"Set your global email address associated with your commits.
git config --global user.email "steliosot@msn.com"This specifies that new repositories will use main as the default branch (instead of master).
git config --global init.defaultBranch mainThis command provides information on how to use git config.
git config -hThis opens the official Git documentation for configuration.
git help configTip: Press
Ctrl + Zto exit.
Now let’s create a new project and initialize a Git repository.
Create a new directory called git-ninja, and inside it, create a Python file test.py:
# test.py
print("Hello world!")Navigate to the folder in the terminal:
cd ../../git-ninjaInitialize a new Git repository in the current directory:
git initTip for Mac/Windows: To see hidden files, use
Cmd + Shift + .(Mac) or enableShow Hidden Items(Windows).
Older Git versions may use master as the default branch. Rename it to main:
git branch -m master mainCheck the status of your repository, which shows tracked, untracked, and modified files.
git statusTo start tracking a file (i.e., add it to the staging area):
git add test.pyCreate a new file ninja.py:
touch ninja.pyCheck the status to see the untracked file:
git statusIf you want to remove a file from tracking (keep it locally):
git rm --cached test.pyCreate a .gitignore file to ignore specific file types or files:
# .gitignore
*.txtYou can find useful
.gitignoretemplates at GitHub’s gitignore repository.
You can add all untracked files, except those in .gitignore, to the staging area using one of these commands:
git add --allor
git add -Aor
git add .Take a snapshot of your files (commit the changes):
git commit -m "First commit - committing all files into repository"Add new code to test.py:
print("Hello world!")
print("My name is Stelios!")Check if test.py is modified:
git statusView the differences between the working directory and the last commit:
git diffRed shows removed lines, and Green shows added lines.
Add the modified file to the staging area:
git add test.pyNow, commit these changes to the repository:
git commit -m "Added new print statement to test.py"Git has three main environments:
- Working Directory: Files you are currently editing.
- Staging Area: Files that are staged and ready to be committed.
- Repository (Commit History): The history of all committed files.
If you change your mind and want to unstage a file:
git restore --staged test.pyYou can skip the staging area and commit changes directly with:
git commit -a -m "Updated text to free range"To rename a file:
git mv mysecret.py mysecret_NEW.pyTo delete a file and remove it from Git tracking:
git rm mysecret.pyIf a file was deleted by mistake, you can restore it:
git restore mysecret.pyTo view the complete commit log:
git logTo see the commit history in a more concise format:
git log --onelineYou can modify the message of the last commit:
git commit --amend -m "Changing the file name"View changes introduced by each commit:
git log -pCreate a new branch to work on a separate feature:
git branch FixFunctionTo view all branches:
git branchSwitch to a branch:
git switch FixFunctionYou can create and switch to a new branch in one step:
git switch -c UpdateTextAfter making changes on a feature branch, you can merge it back into the main branch:
git switch main
git merge FixFunctionOnce the branch is merged, you can delete it:
git branch -d FixFunctionIf there are conflicts between branches:
git merge UpdateTextConflict: There are differences between the
mainbranch andUpdateTextbranch that need to be resolved manually.
In the file, remove the conflict markers (<<<<<<<, =======, >>>>>>>) and decide which changes to keep.
Once conflicts are resolved, commit the changes:
git commit -a -m "Resolved merge conflict"Create a new repository on GitHub, e.g., ninja1.
Link your local repository with the GitHub repository:
git remote add origin https://github.com/steliosot/ninja1.gitSet the default branch for your remote repository:
git branch -M mainPush your local repository to GitHub:
git push -u origin mainPush all branches to GitHub:
git push --allOn GitHub, create a new issue, assign a label, and assign it to a team member.
Make changes in a new branch, create a pull request (PR), and then review and merge it.
Fetch all the latest changes from the remote repository and merge them into your local branch:
git fetch
git mergeOr, use git pull to do both in one step:
git pull- Create and Commit Changes
- Create a new repository, add a file, commit, and push it to GitHub.
- Branching Exercise
- Create a new branch, make changes, and merge it into
main. Resolve conflicts if necessary.
- Create a new branch, make changes, and merge it into
- Undo Changes
- Make changes to a file, unstage it, and commit it again.
- Git Ignore Files
- Create a
.gitignorefile to ignore all.txtfiles, and track only Python files. Commit the changes.
- Create a