Skip to content

Latest commit

 

History

History
219 lines (142 loc) · 2.8 KB

File metadata and controls

219 lines (142 loc) · 2.8 KB

Git

📘 Git Commands Reference

📁 Repository Management

git init

Initialize a new Git repository in the current directory.

git clone <repo-url>

Clone an existing Git repository to your local machine.


📄 Staging & Committing

git status

Show the current state of the working directory and staging area.

git add <file>

Add a file to the staging area.

git add .

Add all changes (new, modified, deleted files) to the staging area.

git commit -m "Your commit message"

Commit staged changes with a message.


📜 Branching

git branch

List all local branches.

git branch <branch-name>

Create a new branch.

git checkout <branch-name>

Switch to a different branch.

git checkout -b <new-branch>

Create and switch to a new branch.

git merge <branch>

Merge the specified branch into the current one.


🌐 Remote Repositories

git remote add origin <repo-url>

Add a remote repository named origin.

git remote -v

Show URLs of the remote repositories.

git push origin <branch-name>

Push local branch to the remote repository.

git pull

Fetch and merge changes from the remote repository.


🧹 Undo & Reset

git reset <file>

Unstage a file (but keep the changes in working directory).

git reset --hard

Discard all local changes and reset to the last commit.

git checkout -- <file>

Discard changes in a specific file.


🔍 Viewing History

git log

View commit history.

git log --oneline

Get the all files changes in the last commit

git diff --name-only HEAD HEAD~1

View a simplified commit history.

git diff

Show differences between working directory and index.

git show <commit>

Show details of a specific commit.


🧪 Tags

git tag

List all tags.

git tag <tagname>

Create a new tag.

git push origin <tagname>

Push a specific tag to the remote repository.


🗑️ Deleting

git branch -d <branch>

Delete a local branch.

git rm <file>

Remove a file from the working directory and stage the deletion.


🗑️ Git Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

This means that, for example, instead of typing git commit, you just need to type git ci

🧠 Tips

  • Use .gitignore to exclude files from tracking.
  • Always commit with meaningful messages.
  • Use git stash to temporarily save uncommitted changes.