Git initialization:
git init
Delet the Initialized git repository: (This will remove the version control system)
rm -rf .git
Display the status of git repository:
git status
Display commits history:
git log
Add all files to staging area:
git add .
Remove all files from staging area:
git rm --cached -r .
Commiting the staging area files:
git commit -m "Intial Commit"
Pushing the local git repository to remote git repository:
git push origin master
Pull from remote to local repository:
git pull
Cloning a Remote Repository:
git clone <url>
Merging a Remote Repository:
git pull origin <branch_name>
- This Merges the remote repository with local repository.
Creating a branch:
git branch <branch-name>
Displaying all the branches:
git branch
Merging the branch with main branch:
git merge <branch-name>
Switching Branches
git checkout <branch-name>
Create a new branch and switch to it:
git checkout -b feature/new-feature
Go to the specific commit of the file:
git checkout <commit-hash>
Go to the latest commit of the working branch:
git checkout <branch-name>
This command will update your working directory to the latest commit on the specified branch.
git reset [options] <commit-hash>
This command in Git is a versatile command that allows you to reset the state of your repository to a specific commit.
git reset --soft <commit-hash>
The --soft option
- This command will undo the last commit, but keep the changes from that commit in your working directory and staging area.
- This means that your changes are not lost and are ready to be committed again.
You can then manually unstage or delete the files you want to remove.
A -- B -- C -- D (HEAD)
If you run git reset --soft B
, you'll get:
A -- B (HEAD) -- C -- D
The commits C and D are effectively "uncommitted," and their changes are in the staging area, ready for a new commit.
git reset --hard <commit-hash>
The --hard option
- moves the branch pointer to the specified commit
- resets the staging area (index) to that commit
- discards all changes in your working directory
Use this option with caution, as it permanently deletes your uncommitted changes.
A -- B -- C -- D (HEAD)
If you run git reset --hard B
, you'll get:
A -- B (HEAD)
Commits C and D are effectively "discarded," and their changes are lost.
Note : We might want to update the remote repository with the changes we made in the local repository.
To do so we can use the following command:
git push --force origin <branch-name>
If you have new files in your working directory that you want to remove in addition to resetting your branch to the latest commit, you can use the git clean command.
The git clean command is used to remove untracked files and directories from your working directory. Here's how you can do it:
Check for Untracked Files:
git clean -n
Remove Untracked Files:
git clean -f
Be very cautious when using git clean -f because it permanently deletes untracked files and directories, and there's no way to recover them.
git revert <commit-hash>
This command is used to create a new commit that undoes the changes made by a previous commit.
A -- B -- C -- D (HEAD)
You want to undo the changes introduced by commit C. You can use git revert as follows:
git revert C
After running this command, Git will create a new commit (let's call it E) that undoes the changes from commit C. The commit history will look like this:
A -- B -- C -- D -- E (HEAD)
Git subtree is a feature in Git that allows you to embed one Git repository within another as a subdirectory.
This is useful when you want to include another repository's content into your own project, while still keeping them separate, and track changes to the embedded repository as part of your project's history.
git subtree add --prefix=subdir-name remote-repo.git branch-name