Skip to content

Instantly share code, notes, and snippets.

@Sakib37
Created February 22, 2019 15:24
Show Gist options
  • Save Sakib37/417d910fdbd103f7d3026bbba21fdcad to your computer and use it in GitHub Desktop.
Save Sakib37/417d910fdbd103f7d3026bbba21fdcad to your computer and use it in GitHub Desktop.
useful command for git
################################################################################
**************** INITIALIGE AND MANAGE GIT ************************
################################################################################
source: https://git-scm.com/doc
# Create a git repo using git API
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'
# Here 'USER' is the username and 'REPO' is the repository name
# Configure git settings in the machine
git config --global user.name "sakib37"
git config --global user.email "[email protected]"
# Check for help in git
git help
git help config # Config related help
# Check global config
git config --list
You can also check what Git thinks a specific key’s value is by typing git config <key>:
git config user.name
All global git config is saved in ~/.gitconfig file
# To reset/replace a config done by 'git config' command
For example "git config --global user.name "sakib37" can be reset by
Reset: git config --global --unset-all user.name
Unset: git config --global --unset user.name
Replace: git config --global --replace-all user.name "New User Name"
# Go to the directory of the repo and initialize git
git init
# Define where to push the code
git remote add [shortname] [url]
git remote add origin https://github.com/Sakib37/Thesis.git
# Push local repo to github repository
git push origin master
# Add, commit and push to the repo
git add .
git rm filename # Removes file
git commit -m 'First Commit'
# To skip 'git add' use
git commit -a -m "First commit" # Autometically stage file
git push origin master
# To rename a file in git
git mv file_from file_to
# Commit on the same SHA of last commit
git commit --amend
# Check status(Modified or untracked files) of the repository
git status
git status -s # Short version of status
# Check the difference between working directory and staging area
git diff # compares change in working directory and staging area
git diff filename # Differece in a file(working and staging version)
git diff --staged # Compares stage changes to last commit
git diff --cached # What is staged after last commit
# Commit log can be seen by using the following command
source: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
git log
# -p, which shows the difference introduced in each commit.
git log -p
git log -p -2 # Only info about last 2 commit
####################################################################
############################## stash #############################
# Stash your current changes before switching branch
git stash
# Get all changes from the stash
git stash pop
################################################################################
############################## Branching in git #############################
# check the current branch
git branch
# More verbose branch info
git branch -vv
# Create a new branch
git branch new_branch
# Create branch and checkout at the same time
git branch -b new_branch
# Go to a branch
git checkout branch_name
# To see last commit on each branc
git branch -v
# To check merged and unmarged branches with the current branch
git branch --merged
git branch --no-merged
# While merging branch go the receiver branch and put
git merge name_of_branch_to_be_merged
# Conflict will occur while merging if the master has a new commit in a line and branch also has a new commit in the same line.
Remove all text because of merge conflict and add file to the staging area and commit
# Delete a branch
git commit -d branch_name
git commit -D branch_name # if there is commit in new branch
###############################################################################
################## Backtracking in GIT #######################################
# Most recent commit is the HEAD. To see HEAD
git show HEAD
# To reset the file as in HEAD commit
git checkout HEAD filename
# This command resets the file in the staging area to be the same as the HEAD commit. It does not discard file changes from the working directory, it just removes them from the staging area.
git reset HEAD filename
git reset SHA_OF_A_COMMIT
#########################################################################
############################## Team working##############################
# Cloning remote repo
source: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
git clone remote_URL_or_filepath clone_name
# see the list of remotes for the current project
git remote -v
# Inspecting remote
git remote show [remote-name]
# To rename a remote
git remote rename old_name new_name
# Delete a remote
git remote rm remote_name
# Fetching remote to keep up_to_date with the remote
git fetch
git fetch remote_name # Fetching specific remote
# This will not merge the update/changes rather put them in a remote branch(origin/master)
# To merge the updates in local master
git merge origin/master
# Workflow to work with remote repo
1. Fetch and merge changes from the remote
2. Create a branch to work on a new project feature
3. Develop the feature on your branch and commit your work
4. Fetch and merge from the remote again (in case new commits were made while you were working)
5. Push your branch up to the remote for review
# Push the local development branch to remote for review
git push [remote-name] [branch-name]
git push origin your_branch_name
###################################### TAG ##################################
##############################################################################
source: https://git-scm.com/book/en/v2/Git-Basics-Tagging
# Git has the ability to tag specific points in history as being important. Typically people use this functionality to mark release points (v1.0, and so on).
# list the tags
git tag
# Search for a tag pattern
git tag -l "v1.8.5*"
# Creating an annotated tag
git tag -a v1.4 -m "my version 1.4"
# Inspect a tag
git tag tag_number
# Tagging later using the SHA of a commit
git tag -a tag_name SHA_of_commit
################################################################################
####################################### Rebasing #############################
soruce: https://git-scm.com/book/en/v2/Git-Branching-Rebasing
# With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment