My brother started a great project. I’ll link to it when he finishes if he’ll approve.
But he told me that he lost a few days of work because he didn’t get any version control.
So I decided to write a “yet another git essential” post to make sure everything is clear to me and try and help others.
What is git
Git is a free and open-source distributed version control system [source]
How to use Git
- First we need a repository, we can build it by ourself
git init
or we can use GitHub or GitLab to init it for us and then clone it
- Checking that the Git is OK.
It’s healthy to run git status often. Sometimes things change, and you don’t notice it.
git status
- After any new file creation, we need to add it to the repository
Later we’ll see that we can ignore some files or folders using .gitignore
git add <file>
Or
git add *
- Run the commit command with a message describing what we’ve changed
git commit -m "description what we've changed."
- Git’s log is a journal that remembers all the changes we’ve committed so far, in the order we committed them
git log
- Add a remote name and a repository URL
git remote add origin <http link>
- The push command tells Git where to put our commits.
The name of our remote is the origin, and the default local branch name is master.
The -u tells Git to remember the parameters so that next time we can run git push
and Git will know what to do.
git push -u origin master
- Check for changes on our GitHub repository and pull down any new changes
git pull origin master
- what is different from our last commit by using the git diff
The HEAD is a pointer that holds your position within all your different commits. By default, HEAD points to your most recent commit.
git diff HEAD
run git diff with the staged option to see the changes you just staged
git diff staged
- You can unstaged files by using the git reset
git reset [file name]
Files can be changed back to how they were at the last commit by using the command:
git checkout <target>
git checkout [file name]
- You can create a copy (aka. branch) of the code so you can make separate commits to it.
git branch [branch name]
- You can switch branches using the git checkout
command git checkout [branch name]
13 You can remove files by using the git rm command, which will not only remove the actual files from the disk but will also stage the removal of the files
git rm *.txt
- After you finish the changes switch back to master and merge your changes from the branch into the master branch
git checkout master git merge [branch name]
- You can use git branch -d [branch name] to delete a branch
git branch -d [branch name]
- All that’s left for you to do now is to push everything you’ve been working on to your remote repository, and you’re done
git push
global commands :
- Set the user name across all repos
git config "global user.name =<user name?"
- Find who changed what in a file
git blame [file name]
- Set git dad
git config global alias.dad "!curl https://icanhazdadjoke.com/" && echo && git add