Git Commands¶
Basic Commands¶
-
Initializes a new Git repository in the current directory.
git init -
Creates a copy of a remote repository in a local directory.
git clone [url] -
Creates a copy of a local directory in remote repository when we push code.
git remote add origin [url] -
Displays the state of the working directory and staging area.
git status -
Stages a file for the next commit.
git add [file] / . -
To unstage a file.
git restore --staged [file] -
To Discard a unstaged file.
git restore [file] -
Commits the staged changes with a descriptive message.
git commit -m "[message]" -
Fetches and integrates changes from a remote repository into the current branch.
git pull -
Uploads local commits to a remote repository.
git push -
Usage:
git push origin masterThis will push your local master branch to the master branch on the remote repository named origin.git push origin [branch-name] -
Lists branches & show current branch.
git branch -
Creates a new branch.
git branch [new-branch-name] -
Deletes a branch.
git branch -d [branch-name] -
Renames a branch.
git branch -M [new-branch-name] -
Switches to the specified branch.
git checkout [branch-name] -
Merges changes from the specified branch into the current branch.
git merge [branch-name] -
Displays changes between commits, the working directory, and the staging area.
git diff -
Temporarily saves changes that are not yet ready to be committed.
git stash -
stash with message/name.
git stash push -m "your message here" -
To see a list of stashes you’ve made.
outputgit stash list$ git stash list stash@{0}: WIP on Sachi: 2baf704 few changes in OldCabinPage stash@{1}: WIP on Sachi: 2baf704 few changes in OldCabinPage -
To apply the most recent stash and keep it in the stash list.
git stash apply -
To apply a specific stash and keep it in the stash list.
git stash apply stash@{1} -
To apply the most recent stash and remove it in the stash list.
git stash pop -
To apply a specific stash and remove it in the stash list.
git stash pop stash@{1} -
To remove a most recent stash from the list without applying it
git stash drop -
To remove a specific stash from the list without applying it.
git stash drop stash@{1} -
To remove all stashesgit stash clear
Configuration Commands¶
-
Sets the global username for commits.
git config --global user.name "[name]" -
Sets the global email for commits.
git config --global user.email "[email]" -
Sets the username for commits.
git config user.name "[name]" -
Sets the email for commits.
git config user.email "[email]" -
Lists all configuration settings.
git config --list
Remote Commands¶
-
Lists the remote repositories associated with the local repository.
git remote -v -
Adds a new remote repository.
git remote add [name] [url] -
Removes a remote repository.
git remote remove [name]
Advanced Commands¶
-
Shows the commit history.
git log -
Unstages a file from the staging area.
git reset [file] -
Resets the index and working directory to match the specified commit (destructive).
git reset --hard -
Creates a new commit that undoes the changes from a specified commit.
git revert [commit] -
Re-applies commits from the current branch onto another branch (often used to integrate changes from one branch into another).
git rebase [branch-name] -
Creates a new tag pointing to the current commit.
git tag [tag-name]
Other Commands¶
-
Shows Present Working Directory.
pwd -
Lists all the files & folders in the current directory.
ls -
Lists all the files & folders in the current directory including hidden files & folders.
ls -a