Git is an open source freely distributed version control system specifically designed to efficiently handle all aspects of your project. It’s relatively small learning curve combined with its tiny footprint and outstanding performance have made it a favorite among programmers for collaboratively developing source code during application development. Unlike most client-server systems, Git directories are full-fledged repositories with complete history and full version-tracking abilities, independent of network access or a central server.
Below are some handy git commands to help you stay on task when developing amazing applications!
Stash is place to hide modifications while you work on something else.
git stash - when you want to record the current state of a working directory and the index, but want to go back to a clean working directory. This command saves your modifications away and revers the working directory to match the HEAD commit.git stash save [<msg>] - save your local modifications to a new stash, and run git reset --hard to revert them with an optional <msg> to give a description to the stashed state. However -> For a quick snapshot, you can omit both 'save' and <msg>.git stash apply [<stash>] - move changes from the specified stash into the workspace with the latest stash always being the default. git stash pop - Applies any changes from the last (or specified) stash and then removes the given stash.git stash list - lists all stashes you currently have.git stash show [<stash>] - show the changes recorded in the stash as a diff between the stashed state and its original parent. When no <stash> is given, the latest stash is shown. git stash drop [<stash>] - removes the stash from the stash list. When no <stash> is given, the command will remove the latest one. git stash clear - removes all stashed states. However, this command subjects these stashed states to pruning which may make recovery impossible.git stash branch <branchname> [<stash>] - creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created and applies the changes recorded in the <stash> to the new working tree and index. This is a useful command if the branch you ran git stash save has changed enough that git stash apply fails due to conflicts.
The working directory is a single checkout of one version of a project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
git clone <repo> - download <repo> and checkout HEAD of the master branch.git pull <remote> - pull all changes from the <remote> into the current branch. git status - displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git and not ignored.git diff - Displays differences not added to the index. git diff <commit or branch> - view changes in the workspace relative to the named <commit>. Use HEAD to compare it with the latest commit or a branch ame to compare with the top of a different branch. git add <file or dir> - used to stage content for the next commit vs committing all changes. git reset --hard - match the workspace and index to the local tree. NOTE -> Any changes to tracked files since last commit are lost. This command is helpful when merging causes conflicts and you want to start over. git branch - List all existing branches. Option -r causes the remote-tracking branches to be listed, and option -a shows bothgit checkout <branch> - switches to <branch> by updating the index and workspace to reflect the specified <branch>.git checkout -b <branchname> - creates a new branch with name <branchname> and switches to that branch.git merge <branch> - merge changes from <branch> into current branch. Use --no-commit to leave changes uncommitted. git rebase <upstream> - reverts all commits since the current branch diverged from <upstream> then reapply them one-by-one on top of changes from the HEAD of <upstream>git cherry-pick <commit> - integrate changes in the given commit into the current branch you are working in. git revert <commit> - reverse commit specified by <commit> and commit the result. However for this command to work, you need to make sure your working tree is clean (i.e. no modifications from the HEAD commit).git reset --hard <remote>/<branch> - reset local repo and working tree to match a remote branch. Use reset --hard origin/master to throw away all commits to the local master branch. Great for starting over after a failed merge.
Before you commit or check in files, you first need to add them to the Index. Thus the Index can be thought of as files you want to commit. Other names you may hear for “index” and can be thought of as interchangeable are “current directory cache”, “staging area”, or even just “cache” or “staged files”.
git commit -m "message" - stored current contents of the index in a new commit along with a log message. git reset HEAD <file(s)> - this command removes the specified files from the next commit. This resets the index but not the working tree (i.e. the changed files remain preserved but they are no longer marked for commit) and reports what has not been updated. git reset --soft HEAD^ - undo the last commit, leaving the changes in the index and yes, ^ is part of the command. git diff --cached [<commit>] - this will allow you to view changes you've staged vs. the latest commit. include <commit> to see changes relative to a particular commit. git commit --amend - easily modify the last commit with the current index changes.
With Git being a distributed version control system, the local repository has exactly the same features and functionality as any other Git repository for your project.
git log - show recent commits with most recent commit at top. Options include:
--decorate -displays branch and tag names on appropriate commits.
--stat - stats (i.e. files changed, insertions, deletions, etc.).
--author=<author> - only shows commits from a certain <author>.
--after="MMM DD YYYY" - only display commits after specified date (ex. "Jul 04 2021").
--before="MMM DD YYYY" - only display commits before specified date (ex. "Jul 04 2021")
--merge - only display commits involved in current merge conflictsgit diff <commit> <commit> - view changes between two commits.git branch - List all existing branches. Option -r causes the remote-tracking branches to be listed, and option -a shows both.git -d <branch> - deletes <branch>. Use -D to force deletion. git branch --track <new> <remote/branch> - creates a new local branch that tracks a remote branch.git fetch <remote> - fetch all remote branches and store all references/objects. git push - update server with commits across all branches that are 'common' between your local copy and the server while ignoring local branches that were never pushed to the server. git push <remote> <branch> - push a new or existing branch to remote repository. git push <remote> <branch>:<branch> - push a new branch to a remote repository with a different name.
Upstream repositories are versions of your project hosted on the internet (GitHub, GitLab, etc.) or a network ensuring all changes are available for other developers.
git branch -r - lists remote branches.