Git Commands that will increase your productivity

Git Commands for Beginners that will Increase Productivity


  1. git status => will show a status of the repo (good than git log)
  2. git remote => will show remote repositories
  3. git remote -v => will show remote repository URL
  4. git remote add MyRepoName => will create a new repository named MyRepoName
  5. git clone => will clone remote Repository into a local Repository
  6. git branch => will show branches
  7. git branch -a => will show branches on local and on remote (very important)
  8. git branch -r => will show branches on local and remote only tracking branches
  9. git branch -D yourBranchName => Will delete your branch
  10. git checkout -b features/new-feature => will automatically create a branch called features/new-features and switch you to that branch. Whenever you create a branch it references a code from a branch you are on.
  11. git checkout => allows you to change Branches
  12. git checkout . => will discard any uncommitted changes in the current directory (the dot means current directory)
  13. git log => will show the commit log and comments.
  14. git log --stat => will show files that have been changed
  15. git show [thenCommitHash] => will show description of the commit

    Avoiding/Minimizing Merge Conflicts
  16. git diff => will show the modified code [this command will help find differences between states of two files and comes in very handy to predict or spot merge conflicts. If another developer has worked on the same file, this will let you know that you might have a Merge Conflict when Merging your local and remote branches.]
    - When git fails to start to Merge, git checkout can be used to undo changes to the file or changing branches.
    - git reset --mixed can be used to undo the changes to working tree or staging area

    When Merge Conflicts Arises during a merge
    - git merge --abort will stop the merging process and take you back to the state that your branch was before the merge.

  17. git clean -df => gets rid of untracked files and cleans the branch.
  18. git show PartOfBranchHash => will show code in that branch commit
  19. git add -a => will add the modified file to the staging area for commit
  20. git stash => When you work on one branch and make a tone of changes and not ready to commit them, then you decide to switch to another Branch to make a change or fix a bug, git stash saves your changes on that Branch so when you come back you can continue working on your changes.
  21. git config --local user.name "YourUserName" => will set your username in git local
  22. git config --local user.email "[email protected]" => will set your email in git local
  23. git config -l => will print all the configuration you currently have on you local repo
  24. alias graph='git log --all --decorate --oneline --graph' => will create an alias for that command when you type "graph" will show logs
  25. git fetch origin => will go to remote Server and fetch changes and will not merge automatically
  26. git log -p HEAD..FETCH_HEAD =>(very handy: the range notation "HEAD..FETCH_HEAD" means "show everything that is reachable from the FETCH_HEAD but exclude anything that is HEAD)
  27. git log --author=jack => will show you commit history for jack
  28. git log --pretty=oneline => will show you commit history in one line formatted.
  29. git log --graph --oneline --decorate --all => will show you a tagged decorated graph one lined formatted
  30. git log --name-status => will show you only files that have been changed.
  31. git log --help => will bring help information about the log command
  32. gitk HEAD..FETCH_HEAD => shows you everything that a developer has done since they forked the repo (using three dot notation will show everything all developers who forked the repo have done. gitk HEAD...FETCH_HEAD)
  33. git pull origin => will pull remote changes and merge into existing brunch automatically. advice (keep them separately)
  34. git commit -a -m 'someTextAboutTheCommit' => will commit your changes to local brunch with comments
  35. git commit --amend -m "somecommentHere" => If you commit with the wrong message, this command will update the message in earlier commit.
  36. git merge branchName => will create a merge commit to the branch you are on.
  37. git push origin master => will prompt you for GitHub credentials and then push changes to the master branch on a remote server. 
  38. git fetch MyRepoName => will fetch MyRepoName to your local repository including the history of commits
  39. git pull --rebase => will pull changes that are made on the remote repository into your brunch and places your changes to that branch on top of the changes that have been pulled so when you commit and push to remote repo your changes appears on top of commit history.
  40. touch .gitignore => will create a gitIgnore file with files that git should ignore when pushing to a remote repo
  41. git cherry-pick => moves a commit that was done on another branch to another. If you realized you made a wrong commit on a master branch instead of committing on another branch. Copy the hash of the original commit with (git log) then (git checkout to go to another branch) and then (git cherry-peak) past the commit hash in.
  42. git reset (soft, mix, hard)=> will delete the commit wrongly made on a master branch (first you have to git checkout to change to master)
  43. git reflog => is a life server that will restore changes to a branch that has been deleted.
  44. git revert => If you commit and other people have pulled and you realized you have to revert the commit. It will revert changes in files that were committed and commit on top of the committed. you should never change git history, instead, use git revert. When you push and other people pull, their history is not going to be corrupted.
  45. git diff hasHere anotherHasHere => will show differences between two commits.
  46. folk => will duplicate repo that you don't have access to and gives you permission to commit and create branches
  47. Pull Request in GitHub (PR) => will send a Pull Request about the branch to repo owner for approval
  48. Clean => It is important to clean your local repo, deleting the branches you created.
    _________________________________
    Scenarios

    - If you were developing on a branch, then you decide to merge the branch back to Master Branch, maybe you want to create another branch to start solving a bug, then do this:

    git checkout master (this command will take you to master branch, while there, continual)
    git pull origin master (This command will pull any changes from the remote master branch and synchronize the master branch)
    git merge TheBranchYouWantToMergeToMaster (This command will merge the branch to master)
    [NB] You might run into an error that says: error: cannot stat 'somefolder/fileName : Permission denied
    When this error happens, just close all applications consuming the folder where the project is and try again.

    [NB] Before you merge your branch into the main branch (e.g. Develop Branch) do "Pull" first and look out for "Merge Conflicts" [GitFlow] [SourceTree to Manage GitFlow]

    If you are using a Git Bash Console to manage your Source Control, when merging a remote branch (Another Developer) into your own branch, first do: git fetch origin OtherDeveloperBranchName this will show you the changes before you actually pull the branch. If you want to automatically merge the changes from another developers branch into your local branch then do: git merge AnotherDeveloperBranch. Remember git merge will ask you to enter a merge message, in this case, type: i (for insert) then type your message then do: esc (escape key) then type: (:wq) indicating that you completed with the message.
    Other Resources to help get the job done: Link

    Scenarios
    If you happen to push the commit that you feel they weren't ready for other developers to see, you can do this:
    -revert the commit without modifying the git history: git revert (commitHash) you can find the commit hash on GitHub
    -If you don't want other developers to see that you commited and then pushed to the remote repo: git reset --hard commitHash
    -Then push your current commit: git push origin -f localBranch:RemoteBranch
    -You can also rollback your commit locally and the push back to the remote repo: git reset HEAD^ --hard  then git push REMOTE -f
    -If you were working in a working tree and decided that the changes you made are not worth committing, you can replace the local changes with git checkout --

    - git checkout