← ../

Git Cheat Sheet




command flow
git add [WORKING DIR] --> [STAGING]
git commit [STAGING AREA] --> [LOCAL REPO]
git push [LOCAL REPO] --> [REMOTE REPO]
git pull [LOCAL REPO] <-- [REMOTE REPO]
git checkout [WORKING DIR] <-- [LOCAL REPO]
git merge [WORKING DIR] <-- [LOCAL REPO]

make a local repository

git init --bare

create a repository called repo1 one that have local_training as a remote repository git clone local_training repo1

git clone local_training repo1

give you a list of added, removed of modified files and tell you what is staged (added)

git status

avoid pushing all branches (default behavior)

git config --global push.default simple

# add all files

git add *

check your current global config

git config --global -l

push the commited changes

git push

update your current HEAD branch with the latest changes from the remote server

git pull

really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files.

git fetch

gives you the remote repository url

git remote -v

list branches

git branch

see a log history of the commits

git log

create a branch called feature2 and switch to it

checkout -b feature2

push the current branch by default

git config --global push.default current

create the remote branch with the same name and track it git push -u

git push -u

add all files and commit with message "derp file3"

git commit -am "derp file3"

switch to master branch and merge branch feature2 in master branch

git checkout master && git merge feature2

Show changes between commits, commit and working tree

git diff

delete local branch

git branch -d branchname

force delete local branch

git branch -D branchname

delete remote branch

git push origin --delete branchname

throw away your local commits

git reset --hard origin/branchname

committed to the wrong branch?

1 - undo the last commit, but leave the changes available

2 - move to the correct branch

git reset HEAD~ --soft
git stash
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"

rollback to a previous commit

git reflog
git reset HEAD@{index}

I accidentally committed something to master that was indended for another branch?

1 - create a new branch from the current state of master

2 - remove the last commit from the master branch

git branch some-new-branch-name
git reset HEAD~ --hard
git checkout some-new-branch-name