Most Used Git Commands

A list of commonly used Git commands

Posted by Marcelo on May 05, 2024

The Most Used Git Commands

Command Description How to use it
git add Add a file to the staging area (stage file changes for a commit) git add file1 file2 file3 or git add .
git branch List, create, or delete branches. git branch --list (list branches, the current branch is highlighted in green) <br> git branch (list branches, the current branch is highlighted in green) <br> git branch hotfix1 (create a branch called hotfix1)<br>git branch -d hotfix1 (delete branch called hotfix1 if it is merged with the main branch; -D forces deletion) <br> git branch -m master main (rename local branch from master to main)
git checkout Switch to another branch. git checkout hotfix1<br>git checkout main<br>git checkout -b hotfix2 (create a branch hotfix2 if it doesn't exist and switch to it)<br>git checkout -B hotfix2 (create a branch hotfix2 if it doesn't exist, otherwise it is reset, and then switch to it) <br>git checkout e50c8e30ba79b6c6753d9b8fd9d74120a914e0b0
git clean Undo changes on untracked files and directories. git clean -di
git clone Create a local copy of a remote repository. git clone https://github.com/your_username/your_project.git|
git commit Save changes to the repository. git commit -m "Some explanation"
git config git config --list (list all config parameters)<br>git config --list --show-origin (list all config parameters and their origins)<br>git config core.editor<br>git config user.name<br>git config user.email<br>git config --global user.email<br>git config --system user.email<br>git config --global credential.helper wincred<br>git config --global init.defaultBranch main
git diff Show the difference between the working directory and the staging area for a file. git diff file1
git init Create a local repository. git init
git log Show a list of previous commits.
git merge Integrate changes from one branch into another branch. git merge branch_name (integrates changes from branch_name into the current branch)
git mv Rename or move a file without losing its commit history. git mv old_file_name new_file_name
git push Update remote refs(?) git push origin branch_name <br>git push origin --delete branch_name (delete a remote branch!)
git pull git pull origin (updates the local repository) <br>git pull origin master (updates the local repository) <br>git pull origin some_branch (updates the local repository with a branch code)
git rebase Integrate changes from one branch into another branch.
git remote Display, include or update a repository's remote. git remote<br>git remote -v<br>git remote add origin https://github.com/your_username/your_project.git (add a remote repository)<br>git remote set-url origin https://github.com/your_username/your_project.git (update a remote repository) <br>git remote remove origin
git restore Get rid of changes on working area. git restore file1 file2 file3<br>git restore .
git status git status

Reminders


The default branch name for repositories created with the git init command is master. You can change the default using the git config command:

git config --global init.defaultBranch main

You can also rename existing "master" branches through the git branch command:

git branch -m master main

In Git, "origin" is a shorthand name for the remote repository a project was cloned from.

HEAD is the current commit a repository in on. Usually it is the latest commit, but when you checkout a branch at a specific commit HEAD points to that commit (in other words HEAD is not the tip of the branch anymore) and the HEAD is called detached.

Both git rebase and git merge integrate changes from one branch into another branch, but they do it in different ways. Use the golden rule of rebasing: never use it on public branches.

Untracked files are files that have been created within your repo's working directory but have not yet been added to the repository's tracking index using the git add command.

References

Merging vs. rebasing

Git Branching - Basic Branching and Merging

A successful Git branching model

Branching model

Git Clean

Rename Master to Main