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
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.git add <file or directory name>
: adds files to the staging area. To commit changes, firstly files need to be added to staging.git commit -m "message"
: moves tracked files from staging to your local repository.git status
: non tracked and modified files will be displayed.git push
: it will push your local repository to the configured remote repository (Github, Gitlab, Bitbucket…).git config --global user.name/use.email
: configures the name and email to be used with your commits.git clone
: clones a repository in a new directory.git fetch
: downloads remote repository changes.git merge <branch_name>
: merges branch into current branch.git pull
: fetches and merge remote changes on your local repository (git fetch
+git merge
).git rebase <branch_name>
: reapplies commits of a branch on top of your current branch.git stash
: parks unfinished changes that you can reapply later.git stash list
: shows stored stashes.git stash apply
: applies most recent stash. Supply a stash id to apply a particular stash.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).git branch <branch_name>
: creates branch with the given name.git checkout <branch_name>
: switch to given branch name. If-b
option is provided it will create the branch if it does not exist.git reflog
: shows reference logs, so you can see if references of commits were updated.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