A quick guide and list of commands related to Git & GitHub.
If you're new to Git and GitHub or want to deepen your knowledge about it, this repository is for you.
Feel free to email any doubts you may have at hi@bapspatil.com.
-
Configuring the username in Git
git config --global user.name "Firstname Lastname"
-
To display the username set
git config --global user.name
-
-
Configuring email in Git
git config --global user.email "youremail@example.com"
-
To display the email set
git config --global user.email
-
-
Create a new local Git repository
git init
-
Add files for commit
4.1 To add all files in the current folder/directory
git add .
4.2 To add a single file
git add <file-name>
4.3 To add multiple files
git add <file-1> <file-2>
-
Commiting files
git commit -m "Descriptive commit message"
-
Adding a remote, i.e. the GitHub URL for your repository
git remote add origin <repository-url>.git
-
Changing a remote URL for your repository
git remote set-url origin <repository-url>.git
-
Push - Update changes to repository on GitHub
8.1 To push to
master
branchgit push -u origin master
8.2 To force-push (Warning: This erases all previous commits)
git push -u origin master -f
8.3 To push to a different branch
git push -u origin <branch-name>
-
Clone a repository
git clone <repository-url>
-
Create a new branch from current working branch and switch to it
git checkout -b <new-branch-name>
-
Create new branch from an existing branch and stay in the current branch
git checkout -b <new-branch> <existing-branch>
-
Switch to a new branch
git branch <branch-name>
-
List all available branches
git branch
-
Delete a branch
git branch -d <branch-name>
-
Merge a branch from the current branch
git merge <branch-to-be-merged>
-
Check status of files
git status
-
Remove files for commit
git rm <file-name>
-
Pull - Update local repo with latest update from server
18.1 To pull from a single remote
git pull
18.2 To pull from all remotes
git pull --all
-
Stash - Save changes made in the current index and working directory for later
git stash
-
Stash Apply - Apply stashed changes
git stash apply
-
See most recent stash without applying it
git stash show -p
-
View the content of an arbitrary stash
git stash show -p stash@{1}
-
Get Stash List
git stash list
-
Modify the Previous Commit's Message
git commit --amend -m "New commit message"
-
View commits of a certain author
git log --author=<username>
-
View all commits
git log
-
View list of tags
git tag
-
Config value for color
color.status=auto color.interactive=auto color.diff=auto
-
Export Git repository as a ZIP file
git archive --format=zip -9 HEAD -o <file-name>
-
Search (say, "foobar")
git grep "foobar"
-
Revert to previous version of a particular file
31.1 Get the commit history of the file
git log path/to/file
31.2 Checkout to particular version using the commit hash obtained from the above command
git checkout <hash> path/to/file
-
Reapply commits on top of another base tip
git rebase <branch-name>
Bapusaheb Patil
This repository and its contents are licensed to Bapusaheb Patil, under the Creative Commons Attribution-ShareAlike 4.0 International (CC-BY-SA-4.0) license.