Git CLI Commands

Vishal Rashmika published on
2 min, 329 words

1. Initializing Git

git init .

2. Staging and Unstaging Files

git add .
git reset .

3. Committing

git commit -m "commit message" "commit description(optional)"

4. Setting Remote Repositories

  • Adding Remote Repositories
git remote add origin https://github.com/somewhere/something.git
  • Listing remote repositories
git remote -v
  • Setting the git upstream (default location to push commits)
git push -u origin master

OR

git push --set-upstream origin branch_name

5. Git Branches

  • Listing the branches
git branch
  • Switch between branches
git checkout -b branch_name
git checkout - (moves to the previous branch)
  • Deleting branches
git branch -d branch_name

6. Merging

  • Merging a branch to the master branch
git merge branch_name 

7. Squashing

Merge all the commits in a branch as a single commit

git merge branch_name --squash

8. Pulling

git pull origin master

9. Combining add & commit

git commit -am "commit message here"

10. Git Aliases

git config --global alias.command_name "command -am"

AND

git config --global alias.command !better-command.sh : execute a script

11. Amend

  • Add files to the last commit (--no-edit : keep the last commit message)
git commit --amend --no-edit
  • Change the last commit message
git commit --amend -m "new commit msg"

12. Force Push

overwrite history on remote

git push origin master --force

13. Revert

Undo a commit with a new commit

git revert undoed_better_commit

14. Stash

will remove the newly made changes from the current working directory and save them for later use without committing them to the repository

  • Stashing
git stash --all
  • Getting the stashed changes back
git stash pop
  • Stashing with a name
git stash save stashing_name
  • Listing the stash
git stash list
  • Getting the named stashed changes back
git stash apply index_number_from_the_stashed_list

15. Renaming the Master Branch

git branch -M mucho

16. Pretty Git Logs

git log --graph --oneline --decorate

17. Diffing

git diff branch_name/change_name

18. Patching

selecting only a few changes manually in a file without staging the whole file

git add -p change_name

19. Increasing the buffer size

git config --global http.postBuffer 1048576000