1.3 Demonstrate the use of Git for version control

Review git commands and how to commit deleted files.

Git Commands

Review common Git commands and how they affect the code base.

  • Git clone: Creates a local version of a remote repository with user write permissions to push code back to the remote repository.

  • Git fork: Creates a separate local version of a remote repository when the user does not have direct write permissions to push code back to the remote repository.

  • Git pull: This will update the local code base from the remote repository.

  • Git fetch: This will sync the local code base from the remote repository but not make any changes to the local code base.

  • Git commit: Submits files from the local repository to the staging environment.

  • Git push: This will submit code to the remote repository.

  • Git add: Updates the working index with any local changes to be staged for the next commit. The * will add all modifications, deleted files, and new files.

You can also chain commands.

  • `git add -A && git commit -m "Your Message"`

  • `git add -A` will add all changes to the repository and `git commit -m"message"` will add your message to the repository.

Git Rebase

With git-rebase you can push local commits together into one commit (without force). With rebase, you can commit more often and then merge your commits together before pushing back to the shared repo. For simplicity, we are only going to talk about rebasing locally. This is a little more complicated to explain, so it is easier to just show a simple example.

For example:

git rebase --i

returns

# p    pick = use commit   
# r    reword = use commit, but edit the commit message   
# e    edit = use commit, but stop for amending   
# s    squash = use commit, but meld into previous commit   
# f    fixup = like "squash", but discard this commit's log message   
# x    exec = run command (the rest of the line) using shell

you can created a text file called "git-article.txt" and committed this new file. Then you can add some new text to this file and made a new commit. Use git add -p, which shows your change. Use the command y to accept this change and committed this change to the git local history. As of right now, both commits touch the same file and really, You only need one commit to represent the changes. You can then use

git rebase --interactive HEAD~2

to see both commits. Pick one commit and use command f to amend changes but discard the commit's log message. Save these changes. Then you can us git log to show recent commits. This leaves a much cleaner history. Rebasing is useful if you forgot to add something to your first commit.

Git Workflow

Gitflow is a method and branching model for git that is geared towards accountability, team collaboration and scaling development teams.

Other Resources

Last updated