This is a list of useful git commands that been always used, or that need to keep in mind while managing the complex project for production.
Basic Snapshotting
Command | Description |
---|---|
git status |
Check status |
git add [file-name.txt] |
Add a file to the staging area |
git add -A |
Add all new and changed files to the staging area |
git commit -m "[commit message]" |
Commit changes |
git rm -r [file-name.txt] |
Remove a file (or folder) |
Reset
git reset --soft
It allows us to go back a commit. Keep the files on stage
.
git reset --mixed
It allows us to go back a commit. Remove the files on stage
and keep them in working directory
.
git reset --hard
Remove the commits and delete the files.
Branchs & Merging
Command | Description |
---|---|
git branch |
List branches (the asterisk denotes the current branch) |
git branch -a |
List all branches (local and remote) |
git branch [branch name] |
Create a new branch |
git branch -d [branch name] |
Delete a branch |
git push origin --delete [branch name] |
Delete a remote branch |
git checkout -b [branch name] |
Create a new branch and switch to it |
git checkout -b [branch name] origin/[branch name] |
Clone a remote branch and switch to it |
git branch -m [old branch name] [new branch name] |
Rename a local branch |
git checkout [branch name] |
Switch to a branch |
git checkout - |
Switch to the branch last checked out |
git checkout -- [file-name.txt] |
Discard changes to a file |
git merge [branch name] |
Merge a branch into the active branch |
git merge [source branch] [target branch] |
Merge a branch into a target branch |
git stash |
Stash changes in a dirty working directory |
git stash clear |
Remove all stashed entries |
Cherry-pick
If we want to merge a specific commit from one branch to another, we place ourselves in the branch from which we want to get the commit and make a git log.
Once the HASH of the commit has been identified, we copy it and move to the branch where we want to merge.
git cherry-pick hashFromCommit # @hashFromCommit - Hash ID generated after commit.
Inspection & Comparison
Command | Description |
---|---|
git log |
View changes |
git log --summary |
View changes (detailed) |
git log --oneline |
View changes (briefly) |
git diff [source branch] [target branch] |
Preview changes before merging |
Stash (temporary storage)
git stash
It save changes so that we can, change branches without committing.
git stash list
Show the files that are on stash.
git stash apply
Remove from temporary storage and return it for us to use.
git stash drop
Remove stash files from temporary storage.
Git Command Aliases
If you are interested in Git aliases, have a look at .bash_profile
,
found here:Link
Get More tutorials on Git