Git Commands for Beginners that will Increase Productivity
- git status => will show a status of the repo (good than git log)
- git remote => will show remote repositories
- git remote -v => will show remote repository URL
- git remote add MyRepoName => will create a new repository named MyRepoName
- git clone => will clone remote Repository into a local Repository
- git branch => will show branches
- git branch -a => will show branches on local and on remote (very important)
- git branch -r => will show branches on local and remote only tracking branches
- git branch -D yourBranchName => Will delete your branch
- 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.
- git checkout => allows you to change Branches
- git checkout . => will discard any uncommitted changes in the current directory (the dot means current directory)
- git log => will show the commit log and comments.
- git log --stat => will show files that have been changed
- git show [thenCommitHash] => will show description of the commit
Avoiding/Minimizing Merge Conflicts
- 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.
- git clean -df => gets rid of untracked files and cleans the branch.
- git show PartOfBranchHash => will show code in that branch commit
- git add -a => will add the modified file to the staging area for commit
- 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.
- git config --local user.name "YourUserName" => will set your username in git local
- git config --local user.email "[email protected]" => will set your email in git local
- git config -l => will print all the configuration you currently have on you local repo
- alias graph='git log --all --decorate --oneline --graph' => will create an alias for that command when you type "graph" will show logs
- git fetch origin => will go to remote Server and fetch changes and will not merge automatically
- 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)
- git log --author=jack => will show you commit history for jack
- git log --pretty=oneline => will show you commit history in one line formatted.
- git log --graph --oneline --decorate --all => will show you a tagged decorated graph one lined formatted
- git log --name-status => will show you only files that have been changed.
- git log --help => will bring help information about the log command
- 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)
- git pull origin => will pull remote changes and merge into existing brunch automatically. advice (keep them separately)
- git commit -a -m 'someTextAboutTheCommit' => will commit your changes to local brunch with comments
- git commit --amend -m "somecommentHere" => If you commit with the wrong message, this command will update the message in earlier commit.
- git merge branchName => will create a merge commit to the branch you are on.
- git push origin master => will prompt you for GitHub credentials and then push changes to the master branch on a remote server.
- git fetch MyRepoName => will fetch MyRepoName to your local repository including the history of commits
- 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.
- touch .gitignore => will create a gitIgnore file with files that git should ignore when pushing to a remote repo
- 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.
- 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)
- git reflog => is a life server that will restore changes to a branch that has been deleted.
- 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.
- git diff hasHere anotherHasHere => will show differences between two commits.
- folk => will duplicate repo that you don't have access to and gives you permission to commit and create branches
- Pull Request in GitHub (PR) => will send a Pull Request about the branch to repo owner for approval
- Clean => It is important to clean your local repo, deleting the branches you created.
- git stash list => will list the stashed commits on your working tree.
- 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
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