some notes for git learners
Initialize a Git Project:
To start a new Git repository for your project, use the following command:
git initCheck the Status:
To view the current status of your project and its files, use:
git statusAdd Files to the Staging Area:
Add files to the staging area to prepare them for commit. You have several options:
Add all files:
git add -AAdd specific file types, e.g., all HTML files:
git add "*.html"Add specific file :
git add "file-name"
Commit Changes:
To save the staged changes as a new snapshot, use the
git commitcommand:git commit -m "Your message about the changes"git commit: Archive staged changes and add a descriptive message.- Always include a message with your commit.
View Differences:
Git provides several ways to view differences:
See the difference between the current stage and the last commit:
git diff HEADSee the difference between the current stage and the previous stage:
git diff --staged
Reset and Exit the Staging Area:
To manipulate the state of your working directory, staging area, and commit history, you can use the
git resetcommand:To unstage a file, use:
git reset 'filename'To move the HEAD pointer to a specific commit and optionally modify the staging area and working directory, use:
git reset --soft <commit> git reset --mixed <commit> git reset --hard <commit>
Restore Files:
To manipulate the state of your working directory and staging area without affecting the commit history, you can use the
git restorecommand:To unstage a file, use:
git restore --staged <file>To restore a specific file from a previous commit to your working directory, use:
git restore --source=<commit> <file>To restore your entire working directory to match a specific commit, use:
git restore --source=<commit> .
Branches:
Managing branches is essential for collaboration and project organization. Here are some branch-related commands:
Show all branches in the project:
git branchCreate a new branch for your project:
git branch 'branch-name'Delete a branch from the project:
git branch -d 'branch-name'
Checkout Branch and Files:
- To switch to a different branch and replace all the files in your current branch with those from 'branch-name',
use:
git checkout 'branch-name' - To replace a file with the version from the HEAD (latest commit), use:
git checkout -- 'filename''--': Refers to the HEAD (latest commit).
- To switch to a different branch and replace all the files in your current branch with those from 'branch-name',
use:
Merge Branches:
To merge a branch into your current branch, use:
git merge 'branch-name'View Commit History:
To see all the commit logs, use:
git logRemove a File:
To remove a file from Git and your project, use:
git rm 'file-name'Push to a Remote Repository:
To push all the changes from your local
masterbranch to a remote repository (usually in the cloud, like GitHub or GitLab), use:git push origin masterorigin: Represents the remote repository.git push -u origin master: After using this parameter, you can usegit pushinstead ofgit push origin master.
Pull from a Remote Repository:
To update, retrieve, and replace the changes from the remote repository into your local
masterbranch, use:git pull origin masterClone a Repository:
To copy an existing repository from a remote location (like GitHub) to your local machine, use:
git clone <repository_URL>Push Changes to Remote:
To send committed changes from your local repository to the remote server, use:
git pushAdd Remote Repository:
To add a remote address to your current project on your local machine, use:
git remote add origin <remote_URL>Change Remote URL:
To replace the current remote URL with a new one, use:
git remote set-url origin <new_remote_URL>Pull Changes from Remote:
To fetch and merge changes from the remote server into your local branch, use:
git pull origin mainSet Upstream Branch:
To track a remote branch, use:
git branch --set-upstream-to=origin/main mainPush with Upstream:
To push the current branch and set the remote as the upstream branch, use:
git push --set-upstream origin <local-branch>Tagging:
Tags are used to mark specific points in the commit history. also you can make versions for your application
Show all tags:
git tagCreate a tag for the last commit:
git tag -a <tag-name or version-number> -m "Your message"Create a tag for a specific commit:
git tag -a <version-number> <commit-id> -m "Your message"Search between tags (e.g., tags starting with 'v0'):
git tag -l 'v0*'Push a specific tag to the remote:
git push origin <tag-name>Push all tags to the remote:
git push origin --tagsSwitch to a Tag:
git switch <tag-name>Verify your tag:
git tag -v <tag-name or version-number>
GPG Keys and Commit Signing:
Show all keys:
gpg --list-keysGenerate a new key:
gpg --gen-keyShow my secret keys:
gpg --list-secret-keys --keyid-format LONGSet your signing key for this project:
git config user.signingKey 'your-secret-key'Show your signing key for this project:
git config user.signingKeySet a global signing key for all projects:
git config user.signingKey 'your-secret-key' --globalSign your version/tag:
git tag -s 'version-number' -m "your-message"Sign your commit:
git commit -S -m 'your-message'Verify your tag:
git tag -v 'tag-name or version-number'
Blame History:
- Show all the change history about your file:
git blame 'filename' - Show all the change history about your requested line in the requested file:
git blame 'filename' -L5 - Git Blame is useful for tracing the changes made to a file, and using the
-Lparameter, you can specify a specific line range to investigate. It helps in understanding who made the changes and when they were made, which can be valuable for tracking alterations, identifying contributors, and understanding the evolution of the codebase.
- Show all the change history about your file:
Remember to use these commands as needed in your Git projects. It's important to maintain a clean and organized version control process for effective collaboration and project management.