user2274644
user2274644

Reputation: 3

How to correctly add a change in Git

I become a little bit confused by how to add your change to Git safely. My procedure is as follow. Plz let me know if it is wrong or needs modification:

Do I need to create any branch after all? what about this: (1) update code in your local machine * git pull (2) do your changes and then add to branch staging * git add *.txt & git commit - m "XYZ" (3) as code in remote repo may have been change since your last pull and therefore your master is not up-to-date: * git pull (4) merge the up-to-date master with your branch * git merge (5) commit your change to remote repo * git pull

Upvotes: 0

Views: 91

Answers (2)

Austen Chongpison
Austen Chongpison

Reputation: 3974

Close but there are some commands you're doing unnecessarily -- like the resets. From the workflow you've described this is in general what you'll do.

// this part creates a new branch from the most updated master
git fetch origin
git checkout master
git pull origin master
git checkout -b XYZ // creates a new branch XYZ

// do your work

// this part adds your files and commits to your XYZ branch
git add *file* // I recommend that you add files one at a time
git commit -m "My Commit Message" // the message doesn't have to have a connection to branch XYZ

// before you push up to origin again make sure your XYZ branch has the latest master code
git fetch origin
git pull origin master // fix any merge conflicts after this step

// push your XYZ branch up to origin
git push origin XYZ

// at this point there is usually some type of peer code review or QA process before any new changes are merged into master.
// Assuming, the branch and its changes are approved then you would merge to master like so:

git fetch origin
git checkout master
git pull origin master //sync master again in case there have been new changes
git merge XYZ
git push origin master

Some may say that the git fetch origin commands are superfluous for this workflow, but I think it's always good to keep your remote tracking branches up to date.

Upvotes: 1

Holloway
Holloway

Reputation: 7367

That seems very excessive and I'm not sure why you're reseting every time.

The general steps I follow are:

  • checkout branch I want to work on (pull here to make sure it's up to date)
  • make the changes I want to make
  • if remote branch has changed while I was working, pull and merge any changes
  • add changes to local index
  • commit local changes locally
  • push changes to remote repo

Upvotes: 1

Related Questions