git remote add origin [remote name you defined. it is up to you] ssh://git@github.com/[username]/[repository-name].git [URL of the repo weather was https or ssh]
Add a remote repository to my project
git remote set-url <remote_name> <remote_url>
Change git gemote URL or you want to rename remote
Inspection & Comparison:
Command
Description
git log
View changes
git log --summary
View changes (detailed)
git log --oneline
View changes (briefly)
git diff [source branch] [target branch]
Preview changes before merging
Git log --all --graph
View changes
git log --all --oneline
View changes
Tags:
Command
Description
git tag-a V2.0.0 -m "Your message Here"
Tag your commits
git show v2.0.0
show all commits related to this tag
git push origin v2.0.0
push tag to the remote repo
Undothings:
Command
Description
First step-> git reset --hard HEAD~1
undo one commit
Second Step-> git push origin <branch_name> --force
Step 1 -> git commit --amend -m "New commit message"
If it is the most recent commit, you can simply do this
Step 2 -> git push --force
This brings up the editor with the last commit message and lets you edit the message. (You can use -m if you want to wipe out the old message and use a new one.)
git rm --cached file : removes the copy of the file from the index / staging-area, without touching the working tree copy. The proposed next commit now lacks the file. If the current commit has the file, and you do in fact make a next commit at this point, the difference between the previous commit and the new commit is that the file is gone. Below are the steps you can follow:
Here are the idea codes in .gitignore
Merging Branches with a Specific Commit Message
To merge one branch into another with a specific commit message, follow these steps:
Switch to the branch you want to merge into:
git checkout target-branch
Merge the source branch into the target branch with a specific commit message:
git merge source-branch --no-ff -m "Your specific commit message"
Example
If you want to merge a branch named feature-branch into main with a specific commit message, use the following commands:
git checkout main
git merge feature-branch --no-ff -m "Merged feature-branch into main with specific commit message"
Explanation of --no-ff Option in Git Merge
The --no-ff option in the git merge command stands for "no fast-forward."
What is Fast-Forward Merge?
In a fast-forward merge, Git simply moves the pointer of the current branch forward to point to the latest commit in the branch being merged. This happens when there have been no new commits on the current branch since it diverged from the branch being merged. This type of merge does not create a new commit, resulting in a linear history.
What is a No Fast-Forward Merge?
When you use the --no-ff option, Git creates a new merge commit even if a fast-forward merge is possible. This merge commit will have two parent commits: one from the current branch and one from the branch being merged. This ensures that the history of the merge is preserved and makes it clear that a feature branch has been integrated.
Why Use --no-ff?
Preserve History: It helps in maintaining a clearer project history by showing when a feature was merged into the main branch.
Traceability: It allows you to easily trace back all the commits that were part of a feature branch.
Grouping Changes: It logically groups the commits of a feature or bug fix together under a single merge commit.
Example
Consider a repository with the following commit history:
* C4 - Main branch
|
* C3 - Main branch
|
| * C2 - Feature branch
|/
* C1 - Common ancestor
If you perform a fast-forward merge, the history might look like this:
* C4 - Feature branch (fast-forward)
|
* C3 - Main branch
|
* C2 - Feature branch
|
* C1 - Common ancestor
However, if you use the --no-ff option, the history will look like this:
* M - Merge commit
|\
| * C2 - Feature branch
* | C4 - Main branch
* | C3 - Main branch
|/
* C1 - Common ancestor
So, using --no-ff helps in preserving the historical context of the changes, making the project history easier to understand and navigate.
➠ To rename (amend) a specific commit using its SHA, you can use an interactive rebase in Git. Here are the steps:
➠ Start Contributing ☺
I will be more than happy if you decide to contribute and/or fork my repo and make something awesome out of it. I will love seeing some feedback or stars from you guys.
➠ To get more information about our repository, just send us a message from here and we will send you the documents related to this study for a better understanding!