Most common Git Commands

Following section covers some of the most common in day to day life for a developer
git config
To configure common configuration values like user email id, name, console colors etc. For instance to set git to Foo use the following command.
 git config --global "Foo"
Initialize a repo
Create an empty git repo or reinitialize an existing one
git init
Clone a repo
Clone the foo repo into a new directory called foo on local workstation
git clone foo
Create new Branch
Now that we have a repo, we might want to create a branch and work on it
Checkout a different branch
Now that we have created our branch, lets say we want to checkout a different remote branch.
Switch branch
Switch between multiple branch
git switch -c NEW_BRANCHNAME
Staging Changes
Before we can push changes to remote repository we have to create commits. For creating commits we need stage these files by adding them. Using the following commands we can add a specific file "foo.js" or all the files modified since last commit.
git add foo.js
git add .
Unstaging Changes / Restoring Files
Maybe you accidentally staged some files that you don't want to commit.
git restore foo.js
git restore .
To commit the staged files execute the following commands.
 git commit -m " updated foo.js with mew controller"
Undoing Soft Commits
The following command will undo your most recent commit and put those changes back into staging, so you don't lose any work
git reset --soft HEAD~1
Undoing Hard Commits
git reset --hard HEAD~1
Squashing Commits
If we want to combine multiple commits to a single commit so that it is easy to review. Following command will combine 5 different commits to a single commit. With a single commit message.
git rebase -i HEAD~5
Pushing Commits
Push the commits from the branch to remote branch.
git push origin BRANCH_NAME
Undo Last Push
If we want to undo changes that were pushed using last commit, we can use the following command to undo it.
Download objects and refs from another repository.
git fetch
Suppose we want to get the latest from the remote branch named "REMOTE_BRANCH" to local working branch. Execute the following command.
git fetch origin
git merge origin/REMOTE_BRANCH
Git Pull is the way to get the latest changes from remote branch and apply on local branch. It might cause merge issues if the local branch is not clean.
git pull origin/REMOTE_BRANCH
Git rebasing is the way to
git fetch origin
git rebase origin/REMOTE_BRANCH
Stash the changes in the working directory away, so that later they can be taken back.
git stash
git stash pop
git stash apply
git stash list

No comments :

Post a Comment

Please leave your message queries or suggetions.