Setting Up Your Identity
Before making any commits, configure your username and email. This step is mandatory once per Git installation; lacking it will cause commit failures.
git config --global user.name "your name"
git config --global user.email "your@email.com"
Starting a Local Repository
Navigate to your project folder and initialize version control:
git init
This creates a hidden .git directory holding the repository metadata. On Linux/macOS, use ll -a to see it; on Windows you can enable hidden items view.
Checking Repository Status
To see untracked and modified files at any time:
git status
After creating a file such as notes.txt, running the status command shows it as untracked (red).
Staging Files
Move a file to the staging area before committing:
git add notes.txt
A warning about line endings may appear – Git automatically converts them. Run git status again; the file will now appear in green, ready to be committed.
Committing to the Local Repository
Record the staged snapshot with a descriptive message:
git commit -m "add initial notes"
Subsequent git status output will confirm a clean working tree.
Viewing History
For a compact log with shortened commit hashes:
git reflog
For the full commit history:
git log
Modifying Files and Tracking Changes
Edit the same file and run git status to see it listed as modified. Stage it again:
git add notes.txt
git commit -m "update content"
Now the log shows multiple commit entries.
Traversing Versions
To move the HEAD and working directory to any previous commit, use the hard reset with the commit hash from reflog:
git reset --hard <commit-hash>
Branching Essentials
Branches let you isolate work without affecting the main line, and they facilitate parallel development.
Listing Branches
git branch -v
Creating a New Branch
git branch feature-login
Switching Branches
git checkout feature-login
While on the new branch, any file modifications remain local to that branch. Switching back to master will reveal the previous state of the code.
Merging Branches
To integrate a branch into the current one:
git merge feature-login
A conflict arises when the same parts of a file were changed on both branches. Suppose master changes the last line, and feature-login changes the second-to-last line. Merging produces a conflict. You must manually resolve it by editing the file, removing conflict markers (<<<<<<<, =======, >>>>>>>), and keeping the desired version. After saving:
git add .
git commit -m "resolve merge conflict"
The merge applies only to the current branch; the source branch remains untouched.
Collaboration Models
Within a team: colleagues clone the same repository, push to and pull from a shared remote, and often work on feature branches.
Cross-team collaboration: one team forks the original repo, makes changes, and submits a pull request to the upstream project.
IntelliJ IDEA Integration with Git
Preparing Ignore Rules
Create a .gitignore file in your user home (or the .gitconfig location) to exclude IDE artifacts and build outputs:
*.class
*.log
.idea/
target/
*.iml
*.jar
*.war
.classpath
.project
.settings
Configuring Git in IDE
In IDEA, set the path to the Git executable under Settings | Version Control | Git. Create a new Maven module, delete any existing .gitignore if present, then go to VCS | Enable Version Control Integration and choose Git. The project files will appear red (unversioned).
Committing and Pushing
Select the project, click Git | Add to stage, then Git | Commit Directory. After providing a message, commit. The first commit will turn the files to normal color. Subsequent changes appear in blue or green.
Switching Versions
After multiple commits, open the Git tool window. The yellow tag marks the HEAD, the green labels indiccate branches. Right-click on any commit and choose Checkout Revision to jump to that version.
Branch Operations via IDE
Right-click the project and select Git | Branches | New Branch to create a branch. To switch, use the branches popup in the bottom-right corner. Merging follows the same conceptual steps: while on master, choose Git | Branches | Merge and select the source branch. If conflicts occur, IDEA displays a dialog with a merge tool; you can accept one side or manually edit the result, then apply and commit.
Connecting to Gitee
Creating a Remote Repository
On Gitee, create a new repository. Avoid initializing it with a README if you intend to push an existing project, as it may cause push conflicts.
Installing the Plugin
In IDEA, go to Settings | Plugins, search for Gitee, install and restart. Then log in to your Gitee account through Settings | Version Control | Gitee.
Pushing to Remote
With a local repository ready, use VCS | Git | Push (or Ctrl+Shift+K). Set the remote URL to your Gitee repository and push the current branch.
Pulling Changes from Gitee
If someone modifies files directly on Gitee, return to IDEA and use VCS | Git | Pull to fetch and merge those updates into your local working branch.