Git has many commands but you can be productive with just a few. I used the script below to fetch my most git commands sorted by the number of their occurrences:
You can copy the script here:
history | awk '{print $2 $3}' | grep '^git' | sort | uniq -c | sort -nr | head -10
I’ve limited the list to just 10 commands but some of these can be extended with command options and arguments which make their uses different and making them somewhat versatile.
- Status
- Add
- Commit
- Branch
- Push
- Checkout
- Log
- Pull
- Diff
- Reset
1. Git Status
$ git status
- Displays the state of the repo.
- You should always use git status because knowing the status of the repo is a MUST.
2. Git Add
2.1. Add files
$ git add [file1] [file2]
- Adds modified files to git’s staging index.
2.2. Add all
$ git add .
- Period (.) - Alias to use when you want to stage all modified files.
2.3. Add specific changes
$ git add -p [file]
- The interactive mode that lets you select which parts of the file should be moved to the staging index.
3. Git Commit
3.1. With a message
$ git commit -m "[message]"
$ git commit -m "add login button"
- Commits the modified files from the staging index.
3.2. Without a message
$ git commit
- Opens a text editor so you can write longer commit messages.
3.3. Modify commits
$ git commit --amend
- Alter most recent commit.
- If the working directory is clean, opens a text editor so you can modify your recent commit message.
- Also used to add forgotten files in the recent commit.
4. Git Branch
4.1. Basic usage
$ git branch
- List all branches in the repo.
- Shows what your current branch is.
4.2. Create a branch
$ git branch [branch-name]
$ git branch feature/login-button
- Creates a new branch.
4.3. Delete a branch
$ git branch -d [branch-name]
- Deletes a branch.
- Move to another branch first.
4.4. Rename a branch
$ git branch -m [old-name] [new-name]
- Renames an existing branch.
5. Git Push
5.1. Basic usage
$ git push
- Pushes the local changes of the repo to the remote repo.
5.2. Upstream a branch
$ git push -u [remote] [local-branch]
$ git push -u origin feature/login-button
- Pushes the current local branch as a new branch of the remote repo if not existing.
5.3. Rename an upstream branch
$ git push -u [remote] [local-branch]:[remote-branch-rename]
$ git push -u origin feature/login-button:WIP/feature/login-button
- Pushes the local branch to remote but you can choose the name of the remote branch.
6. Git Checkout
6.1. Basic usage
$ git checkout [branch]
- Goes to that branch.
6.2. Create a branch
$ git checkout -b [new-branch]
- An alternative for git branch
- Create a new branch and checkout to a new branch.
6.3. Select commit
$ git checkout [commit-sha]
- Checkouts to a temporary branch that is based on the specified commit.
6.4. Undo changes
$ git checkout [file1] [file2]
- Undoes changes to an unstaged file.
6.5. Undo all changes
$ git checkout .
- Undo all modified files in the repo.
7. Git Log
7.1. Basic usage
$ git log
- Shows a list of the recent commits of a repo.
- Displays the SHA, author, date and message of the commits.
7.2. Short version
$ git log --oneline
- Displays shorter version.
- Only shows SHA and message of commits.
7.3. Search commit changes
$ git log -S"[code]"
$ git log -S"<button>Login</button>"
- Returns commits with file changes that matches the string specified with -S
8. Git Pull
8.1. Basic usage
$ git pull
- Pull changes from remote.
8.2. Specific branch
$ git pull [remote] [remote-branch]
$ git pull origin feature/login-button
- Pulls the specific branch from the remote repo.
9. Git Diff
9.1. Basic usage
$ git diff
- Displays the modified files that aren’t yet added to the staging index.
9.2. Specific file
$ git diff [file]
- Displays changes that that aren’t yet added to the staging index for a specific file.
9.3. See staged files
$ git diff --staged
- Displays the modified files that are already added to the staging index.
10. Git Reset
10.1. Unstage files
$ git reset [file1] [file2]
- Undoes git add.
10.2. Unstage all
$ git reset .
- Unstage all modified files.
10.3. Undo commits
$ git reset [commit-sha]
- Erase commits.
- Moves the current branch pointer to the referenced commit SHA.
Originally published at https://micogongob.com/how-to-see-your-most-used-git-commands