Skip to main content

Basic

Basic Git Commands​

  • git init: Initializes a new Git repository in the current directory.
    A .git folder will be automatically created in the current directory.

  • git clone <repository_url>: Clones a remote repository to your local machine.

  • git add <file>: Stages changes to a file for the next commit.

  • git reset <file>: Unstages a file, keeping the changes in the working directory.

  • git rm <file>: Removes a file from the working directory and stages the removal for the next commit.

  • git commit -m "commit message": Commits the staged changes with a descriptive message.

  • git status: Shows the current status of the repository, including staged and unstaged changes.

  • git diff: Shows the differences between the working directory and the staging area.

Viewing Commit History​

  • git log: Displays the commit history of the repository.
  • git log --oneline: Shows a condensed version of the commit history, displaying only the commit hash and message.
  • git log --graph: Displays the commit history in a graphical format, showing the branching and merging of commits.
  • git reflog: Shows a log of all the commits and changes made in the repository, including those that are not part of the current branch history.

Undo Changes​

  • git checkout -- <file>: Discards changes in the working directory and restores the file to the last committed state.
  • git reset HEAD <file>: Unstages a file, keeping the changes in the working directory.
  • git reset HEAD^: Resets the current branch to the previous commit, keeping the changes in the working directory.
  • git reset --hard HEAD: Resets the current branch to the last commit, discarding all changes in the working directory and staging area.
  • git reset --hard <version>: Resets the current branch to a specific commit, discarding all changes in the working directory and staging area.
  • git commit --amend: Modifies the most recent commit, allowing you to change the commit message or add additional changes to the commit.
  • git reset --soft HEAD^: Resets the current branch to the previous commit, keeping the changes in the staging area.

Branching and Merging​

  • git branch <branch_name>: Creates a new branch with the specified name.

  • git branch: Lists all branches in the repository.

  • git checkout <branch_name>: Switches to the specified branch.

  • git checkout -b <branch_name>: Creates a new branch and switches to it.

  • git branch -d <branch_name>: Deletes the specified branch.

  • git branch -a: Lists all branches in the repository, including remote branches.

  • git branch -r: Lists only the remote branches in the repository.

  • git branch -vv: Shows detailed information about each branch, including the tracking status of remote branches.

  • git branch -M <new_branch_name>: Renames the current branch to the specified new name.

  • git branch -m <old_branch_name> <new_branch_name>: Renames a branch from the old name to the new name.

  • git merge <branch_name>: Merges the specified branch into the current branch.

Remote Repositories​

  • git remote add <remote_name> <repository_url>: Adds a new remote repository with the specified name and URL.

  • git remote remove <remote_name>: Removes the specified remote repository.

  • git remote rename <old_name> <new_name>: Renames a remote repository from the old name to the new name.

  • git push -u origin main: Pushes the local main branch to the remote repository and sets it as the upstream branch for future pushes.

  • git push: Pushes local commits to the remote repository.

  • git pull: Fetches and merges changes from the remote repository to the local repository.

  • git remote -v: Shows the remote repositories associated with the local repository.