After installing Git, here are some initialization commands and common operations to get you started.
Create a new directory, navigate into it, right-click, and select "Git Bash Here" to access the command line.
1. Configure Username
Set the global username across all repositoires using the --global flag:
git config --global user.name 'YourName'
2. Configure User Email
git config --global user.email 'your_email@example.com'
3. Initialize a Repository
The following commands create a directory named project, initialize it as a Git repository, create a file index.html, and stage it:
mkdir project // create directory
cd project // navigate into directory
git init // initialize git repository
git status // check file status
touch index.html // create new file
git add index.html // stage the file
Check the staging status:
git status
Commit the staged file to the repository:
git commit -m 'add index.html'
4. Modifying Files
ls // list directory contents
vi index.html // open file in editor
After editing, press ESC, type :wq, and press Enter to save and exit.
cat index.html // view file contents
git add index.html // stage modifications
git commit -m 'update file' // commit changes
git status // verify status
5. Deleting Files
rm -rf index.html // delete local file
git rm index.html // remove from staging area
git commit -m 'remove file' // commit deletion
6. Remote GitHub Operations
View all configured settings:
git config --list
Clone a remote repository, make changes, and push back. Replace the URL with your actual repository address:
git clone https://github.com/username/repository.git // clone repository
cd .. // go up one directory
vi index.html // create or edit file
git add index.html // stage changes
git commit -m 'commit message' // commit to local repository
git push // push to remote repository
The push operation may require authentication credentials. Only after pushing will your changes be reflected in the remote repository.
7. Credential Caching
To avoid entering credentials repeatedly, you can embed them in the remote URL. Edit the config file:
vi .git/config
Modify the remote URL from:
[remote "origin"]
url=https://github.com/username/repository.git
To:
[remote "origin"]
url=https://username:password@github.com/username/repository.git
This allows subsequent push operations without manual credential entry.