Here is a list of some basic Git commands that will help you get started with git.
-
Checkout a new branch
git clone git-repo-url eg:- git clone https://github.com/roymj88/git-helper.git
-
Create a new local repository
git init
-
Create a new branch from current working branch and switch to it
git checkout -b <new-branch-name>
-
Switch to a new branch
git branch <branch-name>
-
List all available branches
git branch
-
Check status of files
git status
-
Add files for commit
git add <file-name> OR git add <file-name-1> <file-name-2> <file-name3> OR git add *
-
Remove files for commit
git rm <file-name>
-
Commit
git commit -m "Message for commit"
-
Pull - Update local repo with latest update from server
git pull
-
Push - Update changes to repository
git push origin master
#Updates 'master' branch
git push origin <branch-name>
#Updates corresponding branch
- Stash - Save changes made in the current index and working directory for later
git stash
- Stash Apply - Apply stashed changes
git stash apply
- Merge
git merge <branch-name>
#Merges a different branch into your active branch
- Setting username in Git
git config --global user.name "user name"
#Set a new user name
git config --global user.name
#Displays the user name
- Setting email in Git
git config --global user.email "[email protected]"
- Modify the Previous Commit's Message
git commit --amend -m "New commit message"
-
View commits of a certain author
git log --author=user.name
-
View all commits
git log
-
List of tags
git tag
- Config value for color
#Edit Color scheme
color.status=auto
color.interactive=auto
color.diff=auto
- Export Git Repo
git archive --format=zip -9 HEAD -o <file-name>
- Search
#Search the working directory for foo()
git grep "foo()"
- Get Stash List
#view stashes currently on the stack
git stash list
- Revert to previous version of a particular file
#Get the commit history of the file
git log path/to/file
#Checkout to particular version using the commit hash obtained
git checkout <hash> path/to/file
- Create new branch from an existing branch
#Creates new_branch branch off existing_branch
git checkout -b new_branch existing_branch
- See what's in the most recent stash without applying stash
git stash show -p
- View the content of an arbitrary stash
git stash show -p stash@{1}