Git Guide


01 Feb 2019  Sergio Martin Rubio  3 mins read.

Git Areas

“Git has three main states that your files can reside in: committed, modified, and staged.”

All files can be found on your working tree area and you can see them by running git status. Git will start tracking files when they are staged or added to the staging area, and to do so, you need to run git add <file or directory name>.

Now if you track new files or make changes on the already tracked ones, you need to somehow move them to your local repository. The git command git commit -m "message" takes all changes in the Staging Area, wraps them together and puts them in your Local Repository.

Commands

  1. git init: creates an empty git repo, which is a .git directory with subdirectories for objects. HEAD is also created to point to the HEAD of the master branch.
  2. git add <file or directory name>: adds files to the staging area. To commit changes, firstly files need to be added to staging.
  3. git commit -m "message": moves tracked files from staging to your local repository.
  4. git status: non tracked and modified files will be displayed.
  5. git push: it will push your local repository to the configured remote repository (Github, Gitlab, Bitbucket…).
  6. git config --global user.name/use.email: configures the name and email to be used with your commits.
  7. git clone: clones a repository in a new directory.
  8. git fetch: downloads remote repository changes.
  9. git merge <branch_name>: merges branch into current branch.
  10. git pull: fetches and merge remote changes on your local repository (git fetch + git merge).
  11. git rebase <branch_name>: reapplies commits of a branch on top of your current branch.
  12. git stash: parks unfinished changes that you can reapply later.
  13. git stash list: shows stored stashes.
  14. git stash apply: applies most recent stash. Supply a stash id to apply a particular stash.
  15. git log: lists the commits made in your local repository. You can add options and filters (-p: difference in each commit; --stat: abbreviated stats for each commit; --pretty=<oneline/short/full/format:"%h - %an, %ar : %s">; --graph: graph with branch and merge history).
  16. git branch <branch_name>: creates branch with the given name.
  17. git checkout <branch_name>: switch to given branch name. If -b option is provided it will create the branch if it does not exist.
  18. git reflog: shows reference logs, so you can see if references of commits were updated.
  19. git reset <commit_id>: reverts a particular git commit.

.gitignore

“A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected”

You can specify patterns to ignore a particular folder or set of files.

Generate .gitignore

curl -s https://www.gitignore.io/api/{your_list} >> .gitignore

Replace {your_list} with a comma-separated list of technologies. e.g.

curl -s https://www.toptal.com/developers/gitignore/api/macos,intellij,java,maven >> .gitignore