GitHub - warestack/git-ninja
Comprehensive Git Workflow Guide
1. Set Up Git Configuration
Before you start using Git, you need to configure your name and email, as they will be associated with your commits.
1.1 Specify Your Name
This sets the global username for your Git configuration.
git config --global user.name "Stelios Sotiriadis"1.2 Specify Your Email
Set your global email address associated with your commits.
git config --global user.email "steliosot@msn.com"1.3 Specify the Default Branch
This specifies that new repositories will use main as the default branch (instead of master).
git config --global init.defaultBranch main
1.4 Get Information About git config
This command provides information on how to use git config.
1.5 Get Help for git config
This opens the official Git documentation for configuration.
Tip: Press
Ctrl + Zto exit.
2. Initialize a Git Repository
Now let’s create a new project and initialize a Git repository.
2.1 Create a Directory and a File
Create a new directory called git-ninja, and inside it, create a Python file test.py:
# test.py print("Hello world!")
2.2 Open the Folder in Terminal
Navigate to the folder in the terminal:
2.3 Initialize the Git Repository
Initialize a new Git repository in the current directory:
Tip for Mac/Windows: To see hidden files, use
Cmd + Shift + .(Mac) or enableShow Hidden Items(Windows).
2.4 Rename Default Branch (If Using Older Version)
Older Git versions may use master as the default branch. Rename it to main:
git branch -m master main
3. Tracking Files and Making Commits
3.1 Check the Status
Check the status of your repository, which shows tracked, untracked, and modified files.
3.2 Track a File
To start tracking a file (i.e., add it to the staging area):
3.3 Create Another File ninja.py
Create a new file ninja.py:
3.4 Check Status Again
Check the status to see the untracked file:
3.5 Untrack a File
If you want to remove a file from tracking (keep it locally):
3.6 Create a .gitignore File
Create a .gitignore file to ignore specific file types or files:
You can find useful
.gitignoretemplates at GitHub’s gitignore repository.
4. Staging and Committing Changes
4.1 Track All Files (Except Ignored)
You can add all untracked files, except those in .gitignore, to the staging area using one of these commands:
or
or
4.2 Commit Changes
Take a snapshot of your files (commit the changes):
git commit -m "First commit - committing all files into repository"5. Modify and Commit Changes
5.1 Make Changes to test.py
Add new code to test.py:
print("Hello world!") print("My name is Stelios!")
5.2 Check the Status Again
Check if test.py is modified:
5.3 See the Diff (Changes)
View the differences between the working directory and the last commit:
Red shows removed lines, and Green shows added lines.
5.4 Stage the Changes
Add the modified file to the staging area:
5.5 Commit the Changes
Now, commit these changes to the repository:
git commit -m "Added new print statement to test.py"6. Working with Different Git Environments
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.
6.1 Move a File from Staging to Working Directory
If you change your mind and want to unstage a file:
git restore --staged test.py
6.2 Skip Staging and Commit Directly
You can skip the staging area and commit changes directly with:
git commit -a -m "Updated text to free range"7. Renaming, Deleting, and Restoring Files
7.1 Rename a File
To rename a file:
git mv mysecret.py mysecret_NEW.py
7.2 Delete a File
To delete a file and remove it from Git tracking:
7.3 Restore a Deleted File
If a file was deleted by mistake, you can restore it:
8. Viewing Commit History
8.1 View Commit History
To view the complete commit log:
8.2 View Commit History in a Shortened Format
To see the commit history in a more concise format:
9. Modify Previous Commits
9.1 Amend the Last Commit
You can modify the message of the last commit:
git commit --amend -m "Changing the file name"9.2 View Commit Changes (Patches)
View changes introduced by each commit:
10. Working with Branches
10.1 Create a New Branch
Create a new branch to work on a separate feature:
10.2 List All Branches
To view all branches:
10.3 Switch to a Branch
Switch to a branch:
10.4 Create and Switch to a New Branch
You can create and switch to a new branch in one step:
10.5 Merge Branches
After making changes on a feature branch, you can merge it back into the main branch:
git switch main git merge FixFunction
10.6 Delete a Branch
Once the branch is merged, you can delete it:
git branch -d FixFunction
11. Resolve Merge Conflicts
11.1 Try Merging Changes
If there are conflicts between branches:
Conflict: There are differences between the
mainbranch andUpdateTextbranch that need to be resolved manually.
11.2 Manually Resolve Conflicts
In the file, remove the conflict markers (<<<<<<<, =======, >>>>>>>) and decide which changes to keep.
11.3 Commit the Resolved Changes
Once conflicts are resolved, commit the changes:
git commit -a -m "Resolved merge conflict"12. Working with GitHub
12.1 Create a New Repository on GitHub
Create a new repository on GitHub, e.g., ninja1.
12.2 Add Remote Repository
Link your local repository with the GitHub repository:
git remote add origin https://github.com/steliosot/ninja1.git
12.3 Set the Branch
Set the default branch for your remote repository:
12.4 Push to Remote Repository
Push your local repository to GitHub:
12.5 Push All Branches
Push all branches to GitHub:
13. Collaborating on GitHub
13.1 Create an Issue
On GitHub, create a new issue, assign a label, and assign it to a team member.
13.2 Pull Requests
Make changes in a new branch, create a pull request (PR), and then review and merge it.
13.3 Fetch and Merge from GitHub
Fetch all the latest changes from the remote repository and merge them into your local branch:
Or, use git pull to do both in one step:
Exercises
- 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