amrk000 / git_commands-cheat_sheet

Git Commands (Cheat Sheet)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Command Description

git init

initialize current project folder for version control (creates .git files)

git status

show status of current branch: the changes that need to be staged (tracked) or committed

git add .

add all changed files to stage

git add file.extension

add file to stage

git commit -m "message"

commit current staged changes with message

git commit -am "message"

add all changed files then commit (Doesn't add new files just the tracked files)

git commit --amend

edit last commit

git checkout master

move pointer to master

git checkout commit_id

move pointer to selected commit

git revert commit_id

undo by creating a new commit with reverted changes from previous commit

git reset --flag commit_id

undo by removing changes from previous commit totally

flags:
--hard : remove from stage & directory
--mixed : remove from stage not directory
--soft : doesn't remove from stage or directory

git rm file.extension

remove file from stage & project repository

git rm --cached file.extension

remove file from stage

git log

show history of commits including author & date

git blame file.extension

show when each line was last modified in file & who did it

Branches commands

git branch

list all local branches

git branch -a

show all including remote branches

git branch new_branch_name

create new branch

git checkout branch_name

move pointer to branch

git checkout -b new_branch_name

create new branch and moves to it

git branch -d branch_name

delete branch

git merge branch_name

merge a branch to current branch

Remote commands

git remote add give_name repository_https_or_ssh

add a remote repository

git remote set-url remote_name new_link

change remote repository link

git remote -v

show all remote repositories

git clone repository_https_or_ssh

clone a remote repository

git pull

pull project from remote to local repository

git push

push commits from local to remote repository

git pull given_name branch_name

pull from a branch

git push given_name branch_name

push to a branch

git fetch given_name branch_name

pull from a branch but doesn't merge with local files

git push given_name --delete branch_name

delete remote branch

creating .gitignore

make a file .gitignore without extensions and add directories & files

Example:

# comment: Project exclude paths
/folder/
file.txt

updating .gitignore (untrack files)

add file path in .gitignore file then Open git bash and Run:

git rm -r --cached .

Https Auth using token

github website > Settings > Developer Settings > Generate New Token

copy token and put in this link & replace { } with your data

https://{PROJECTNAME}:{TOKEN}@github.com/{USER_OR_ORG}/{REPOSITORYNAME}.git

Use the link in git remote


⬇ Save Git Commands (Cheat Sheet): Download Image

About

Git Commands (Cheat Sheet)