Git Useful Commands

Git Useful Commands

July 27, 2021 3 By Nam Vu

ntechdevelopers

Initialize
#Init new repository
git init [repository name]
#Setting account git
git config –global user.name “[name]”
git config –global user.email “[email address]”
#Clone source code
git clone [url]
#Get new changes to local
git pull

Display Information
#Show log  history commits
git log
git log –oneline
#Show information of a commit
git show [commit]

Commit
#Add a file to commit
git add [file]
#Add all file to commit
git add *
#Remove file in commit
git rm [file]
#Tag a commit
git tag [commitID]
#Commit file with message
git commit -m “[ Type in the commit message]”
#Commit all file after added
git commit -a

Branch
#Show all branch in local
git branch
#Create a new branch
git branch [branch name]
git checkout -b [branch name]
#Delete a branch in local
git branch -d [branch name]
#Checkout a branch
git checkout [branch name]
#Merge branch to current branch
git merge [branch name]
#Push to branch after commit
git push [variable name] [branch]
#Reset a commit after commited
git reset –hard <commit_id>

Saving
#Stash local
git stash
#Get data stash local
git stash pop
#Check list stash list in local
git stash list
#Remove stash in local
git stash drop

Others
#Compare index statging between 2 branch
git diff
git diff –staged
git diff [first branch] [second branch]
#Filter branch quickly and safely
git filter-branch — force — index-filter \
‘git rm — cached — ignore-unmatch logindaten.txt’ \
— prune-empty — tag-name-filter cat — — all
#Start bisecting session
git bisect start
#Mark the current revision as “bad”
git bisect bad
#Mark the last known good revision as “good”
git bisect good revision
#Undo the last commit, but don’t create a revert commit
git revert -n HEAD
#Commit anhand von Commit-ID rueckgaengig machen
git revert 453fsdu2
#Undo the penultimate commit
git revert HEAD ^
#Undo multiple commits
git revert feature ~ 4..feature ~ 2
#Change local commits before a push
git rebase –interactive origin branchName
# Start the standard text editor to edit the commit message
git commit –amend
# Sets the new message directly
git commit –amend -m “My new commit message
#Remove the file from Git without deleting it completely
git reset filename
echo filename >> .gitignore
 
# Undo the last four commits, keep changes
git reset HEAD ~ 4
# Undo the last four commits, discard changes
git reset –hard HEAD ~ 4
# Reset directory “myCode”
git checkout – myCode