Open a git bash shell.
Basic command set up:
git init - set up git in the folder
git status – what git is tracking
git add <filename> – add file to git, in other words ‘stage’ this version of the file to git
git status – to check this
git commit – commit file to the repository
To add a commit message to say what is being committed :
git status – no more pending changes!
git commit –m “change 3” – the –m adds the commit message using command line
git log – show all commit changes
git add . = stage every file
or git add *.html
touch .gitignore
The .gitignore file = a list of files that git should ignore eg. log files – they won’t show on
after creating it do a “git add .” and “git commit –m ‘adding ignore file’”
Branches
git branch MyBranch – create a new branch
git checkout MyBranch – switch to new branch
touch newfile.css
git add .
git commit –m “newfile.css”
git checkout master – switch to master branch
git status – make sure you’re in the master branch
To reset a branch to master
git reset --hard
To reset branch to a remote origin repo
git fetch origin
git reset --hard origin/master
git clean -f # possibly to remove local files
To see what files will be removed (without actually removing them):
git clean -n -f
Reverse / opposite of a 'git add' - remove from the list of files to commit
git reset <filename added>
Deleting a file from the repo or just locally
if you delete a file, it will still show up in "changes not staged for commit" when you do a "git status" eg.
rm test.php
rm -rf testfolder
There are two ways to deal with this, either officially remove it from git with a commit OR just ignore the file.
Method 1 (officially remove the file from git) :
git rm test.php
git commit -m "Deleted this file"
Method 2 (ignore the file locally) :
Add the file to .git/info/exclude (local exclusions) and then refresh the git index.
vi .git/info/exclude (the file will look something like this)
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
test.php
testfolder/*
git update-index --assume-unchanged info.php
To see differences between branches
git diff --name-status master MyBranch # displays a file list
git diff master..MyBranch # displays coding differences
Merging Branches
git merge MyBranch # will add newfile.css (this is an automatic merge)
git commit –a –m ‘change 6’ # this stages ALL files that are current
To do a non-automatic merge, that allows you to add a commit message :
git merge --no-commit --no-ff othersite
Merge conflicts:
After a git commit merge conflict – you’ll see markers in the editor showing the conflicts
Edit the file, remove the markers.
Tip - set 'winmerge' as the merge tool
git config merge.tool winmerge
git mergetool – this will launch winmerge
then run “git commit –a –m ‘merge from master’” to merge it all together
Stash files:
Stashing takes all the uncommitted files of a branch and saves them on a stack, that can be reapplied at any time.
eg. if you had half-edited some files but wanted to update your master branch with remote.
# stash any uncommitted files
git stash
# list what's in the stash
git stash list
# bring the stashed files back
git stash apply
# OR the best idea is to bring the stashed changes back to their own branch and then work from there, this command does all that!
git stash branch testchanges
Remote repositories:
you have to manually retrieve/push changes to the remote repo
git remote - list remote repos
Download a clone url from github or wherever:
git clone <URL> – will import repo
git remote
cd FOLDERNAME
git remote - will now show 'origin'
git remote –v - will now show 'origin' and the URL's
git fetch origin – go out and get any changes since last fetch but won’t merge
git pull origin – fetch and merge into current branch
git commit –a –m ‘readme changes’
git push origin master – push all changes to remote repo called 'origin' and commit to the master branch (you’ll have to login)
git remote add MyRepo <URL> – add an additional remote repo and use the name 'MyRepo'
git remote
git remote -v
git fetch myrepo
To set up a project using Gitlab:
Set up a new Project in Gitlab called “james-ui-test”
git config --global user.name "James R"
git config --global user.email "info@startnet.co.uk"
git clone https://gitlab.com/username/james-ui-test.git
cd james-ui-test/
git add .
git status
git commit -m "first commit"
git push -u origin master
git add .
git commit -m "another update of README file"
git push -u origin master - this will upload changes to repo
Once this is set up, any time afterwards, you can just go into the folder, open a git bash shell, edit files and :
git add .
git commit -m "another update of README file"
git push -u origin master - upload changes to repo
Mostly got this from the very good Learn Git in 20 Minutes video - https://www.youtube.com/watch?v=Y9XZQO1n_7c